[pypy-svn] r76000 - in pypy/branch/fast-forward/pypy: module/struct module/struct/test rlib/rstruct

benjamin at codespeak.net benjamin at codespeak.net
Wed Jul 7 21:13:58 CEST 2010


Author: benjamin
Date: Wed Jul  7 21:13:33 2010
New Revision: 76000

Modified:
   pypy/branch/fast-forward/pypy/module/struct/formatiterator.py
   pypy/branch/fast-forward/pypy/module/struct/test/test_struct.py
   pypy/branch/fast-forward/pypy/rlib/rstruct/standardfmttable.py
Log:
support the bool format

Modified: pypy/branch/fast-forward/pypy/module/struct/formatiterator.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/struct/formatiterator.py	(original)
+++ pypy/branch/fast-forward/pypy/module/struct/formatiterator.py	Wed Jul  7 21:13:33 2010
@@ -98,6 +98,10 @@
             w_obj = self.accept_obj_arg()
             return self.space.r_ulonglong_w(w_obj)
 
+    def accept_bool_arg(self):
+        w_obj = self.accept_obj_arg()
+        return self.space.is_true(w_obj)
+
     def accept_str_arg(self):
         w_obj = self.accept_obj_arg()
         return self.space.str_w(w_obj)

Modified: pypy/branch/fast-forward/pypy/module/struct/test/test_struct.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/struct/test/test_struct.py	(original)
+++ pypy/branch/fast-forward/pypy/module/struct/test/test_struct.py	Wed Jul  7 21:13:33 2010
@@ -262,6 +262,14 @@
         assert unpack("<f", '\x00\x00H\xc1') == (-12.5,)
         raises(self.struct.error, pack, "<f", 10e100)
 
+    def test_bool(self):
+        pack = self.struct.pack
+        unpack = self.struct.unpack
+        assert pack("!?", True) == '\x01'
+        assert pack(">?", True) == '\x01'
+        assert pack("!?", False) == '\x00'
+        assert pack(">?", False) == '\x00'
+
     def test_struct_error(self):
         """
         Check the various ways to get a struct.error.  Note that CPython

Modified: pypy/branch/fast-forward/pypy/rlib/rstruct/standardfmttable.py
==============================================================================
--- pypy/branch/fast-forward/pypy/rlib/rstruct/standardfmttable.py	(original)
+++ pypy/branch/fast-forward/pypy/rlib/rstruct/standardfmttable.py	Wed Jul  7 21:13:33 2010
@@ -31,6 +31,10 @@
     c = string[0]   # string->char conversion for the annotator
     fmtiter.result.append(c)
 
+def pack_bool(fmtiter):
+    c = '\x01' if fmtiter.accept_bool_arg() else '\x00'
+    fmtiter.result.append(c)
+
 def pack_string(fmtiter, count):
     string = fmtiter.accept_str_arg()
     if len(string) < count:
@@ -145,6 +149,10 @@
     fmtiter.appendobj(fmtiter.read(1))
 
 @specialize.argtype(0)
+def unpack_bool(fmtiter):
+    fmtiter.appendobj(bool(int(fmtiter.read(1))))
+
+ at specialize.argtype(0)
 def unpack_string(fmtiter, count):
     fmtiter.appendobj(fmtiter.read(count))
 
@@ -225,7 +233,8 @@
                     'unpack' : make_float_unpacker(4)},
     'd':{ 'size' : 8, 'pack' : make_float_packer(8),
                     'unpack' : make_float_unpacker(8)},
-    }    
+    '?':{ 'size' : 1, 'pack' : pack_bool, 'unpack' : unpack_bool},
+    }
 
 for c, size in [('b', 1), ('h', 2), ('i', 4), ('l', 4), ('q', 8)]:
     standard_fmttable[c] = {'size': size,



More information about the Pypy-commit mailing list