[Twisted-Python] BooleanType support for jelly

Jp Calderone exarkun at intarweb.us
Tue Dec 31 21:30:57 EST 2002


  Support for jellying <type 'bool'>, new in Python 2.3.  The approach and
actual values used were taken straight from TwistedJava.  This will be
Python 2.3 fix #3 if someone tells me it's alright :)

 Jp

-- 
"There is no reason for any individual to have a computer in their
home."
                -- Ken Olson, President of DEC, World Future Society
                   Convention, 1977
--
 12:00am up 14 days, 9:45, 3 users, load average: 0.15, 0.25, 0.16
-------------- next part --------------
Index: twisted/spread/jelly.py
===================================================================
RCS file: /cvs/Twisted/twisted/spread/jelly.py,v
retrieving revision 1.38
diff -u -r1.38 jelly.py
--- twisted/spread/jelly.py	1 Jan 2003 00:29:13 -0000	1.38
+++ twisted/spread/jelly.py	1 Jan 2003 02:23:14 -0000
@@ -35,6 +35,8 @@
 
 This is how Jelly converts various objects to s-expressions:
 
+Boolean: True --> ['boolean', 'true']
+
 Integer: 1 --> 1
 
 List: [1, 2] --> ['list', 1, 2]
@@ -89,6 +91,11 @@
 from types import ClassType
 import copy
 
+try:
+    from types import BooleanType
+except ImportError:
+    BooleanType = None
+
 from new import instance
 from new import instancemethod
 
@@ -399,6 +406,8 @@
                         name]
             elif objType is ModuleType:
                 return ['module', obj.__name__]
+            elif objType is BooleanType:
+                return ['boolean', obj and 'true' or 'false']
             elif objType is ClassType:
                 return ['class', qual(obj)]
             else:
@@ -533,6 +542,13 @@
         else:
             return Unpersistable(exp[0])
 
+    def _unjelly_boolean(self, exp):
+        if BooleanType:
+            assert exp[0] in ('true', 'false')
+            return exp[0] == 'true'
+        else:
+            return Unpersistable(exp[0])
+
     def unjellyInto(self, obj, loc, jel):
         o = self.unjelly(jel)
         if isinstance(o, NotKnown):
@@ -726,6 +742,7 @@
         # I don't believe any of these types can ever pose a security hazard,
         # except perhaps "reference"...
         self.allowedTypes = {"None": 1,
+                             "boolean": 1,
                              "string": 1,
                              "str": 1,
                              "int": 1,
Index: twisted/test/test_jelly.py
===================================================================
RCS file: /cvs/Twisted/twisted/test/test_jelly.py,v
retrieving revision 1.11
diff -u -r1.11 test_jelly.py
--- twisted/test/test_jelly.py	12 Dec 2002 08:43:53 -0000	1.11
+++ twisted/test/test_jelly.py	1 Jan 2003 02:23:14 -0000
@@ -179,7 +179,7 @@
         a = A()
         jelly.unjelly(jelly.jelly(a))
         jelly.unjelly(jelly.jelly(a.amethod))
-        items = [afunc, [1, 2, 3], 'test', 20.3, (1,2,3), None, A, unittest, {'a':1}, A.amethod]
+        items = [afunc, [1, 2, 3], not 1, not not 1, 'test', 20.3, (1,2,3), None, A, unittest, {'a':1}, A.amethod]
         for i in items:
             self.assertEquals(i, jelly.unjelly(jelly.jelly(i)))
     
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://twistedmatrix.com/pipermail/twisted-python/attachments/20021231/6b4c6b31/attachment.pgp 


More information about the Twisted-Python mailing list