[kupfer] Add doctests for ConservativeUnpickler and BasicUnpickler
- From: Ulrik Sverdrup <usverdrup src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [kupfer] Add doctests for ConservativeUnpickler and BasicUnpickler
- Date: Thu, 11 Feb 2010 16:58:04 +0000 (UTC)
commit 0aa0ba0be292a9f9282a1f85ca38ff115b4c08e3
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date: Wed Feb 10 23:37:56 2010 +0100
Add doctests for ConservativeUnpickler and BasicUnpickler
kupfer/conspickle.py | 38 ++++++++++++++++++++++++++++++++++++--
1 files changed, 36 insertions(+), 2 deletions(-)
---
diff --git a/kupfer/conspickle.py b/kupfer/conspickle.py
index 066f631..e8c4c31 100644
--- a/kupfer/conspickle.py
+++ b/kupfer/conspickle.py
@@ -8,7 +8,28 @@ class universalset (object):
return True
class ConservativeUnpickler (pickle.Unpickler):
- "An Unpickler that refuses to import new modules"
+ """An Unpickler that refuses to import new modules
+
+ >>> import pickle
+
+ >>> import kupfer.objects
+ >>> ConservativeUnpickler.loads(pickle.dumps(kupfer.objects.FileLeaf("A")))
+ <builtin.FileLeaf A>
+
+ >>> ConservativeUnpickler.loads(pickle.dumps(eval))
+ Traceback (most recent call last):
+ ...
+ UnpicklingError: Refusing unsafe __builtin__.eval
+
+ >>> import sys
+ >>> import kupfer.obj.base
+ >>> pdata = pickle.dumps(kupfer.obj.base.Leaf(1, "A"))
+ >>> del sys.modules["kupfer.obj.base"]
+ >>> ConservativeUnpickler.loads(pdata)
+ Traceback (most recent call last):
+ ...
+ UnpicklingError: Refusing to load module kupfer.obj.base
+ """
safe_modules = {
"__builtin__" : set(["set", "sum", "object"]),
"copy_reg" : set(["_reconstructor"]),
@@ -34,10 +55,23 @@ class ConservativeUnpickler (pickle.Unpickler):
return unpickler.load()
class BasicUnpickler (ConservativeUnpickler):
- "An Unpickler that can only unpickle persistend ids and select builtins"
+ """An Unpickler that can only unpickle persistend ids and select builtins
+
+ >>> import pickle
+
+ >>> import kupfer.objects
+ >>> BasicUnpickler.loads(pickle.dumps(kupfer.objects.FileLeaf("A")))
+ Traceback (most recent call last):
+ ...
+ UnpicklingError: Refusing unsafe kupfer.obj.objects.FileLeaf
+ """
+
safe_modules = {
"__builtin__" : set(["object"]),
"copy_reg" : set(["_reconstructor"]),
"kupfer.puid" : set(["SerializedObject"]),
}
+if __name__ == '__main__':
+ import doctest
+ doctest.testmod()
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]