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

benjamin at codespeak.net benjamin at codespeak.net
Wed Jul 7 22:03:17 CEST 2010


Author: benjamin
Date: Wed Jul  7 22:03:16 2010
New Revision: 76002

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

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 22:03:16 2010
@@ -269,6 +269,8 @@
         assert pack(">?", True) == '\x01'
         assert pack("!?", False) == '\x00'
         assert pack(">?", False) == '\x00'
+        assert pack("@?", True) == '\x01'
+        assert pack("@?", False) == '\x00'
 
     def test_struct_error(self):
         """

Modified: pypy/branch/fast-forward/pypy/rlib/rstruct/nativefmttable.py
==============================================================================
--- pypy/branch/fast-forward/pypy/rlib/rstruct/nativefmttable.py	(original)
+++ pypy/branch/fast-forward/pypy/rlib/rstruct/nativefmttable.py	Wed Jul  7 22:03:16 2010
@@ -59,6 +59,13 @@
     doubleval = float(floatval)
     fmtiter.appendobj(doubleval)
 
+def pack_bool(fmtiter):
+    truth = fmtiter.accept_bool_arg()
+    fmtiter.result.append('\x01' if truth else '\x00')
+
+def unpack_bool(fmtiter):
+    fmtiter.appendobj(bool(ord(fmtiter.read(1))))
+
 # ____________________________________________________________
 #
 # Use rffi_platform to get the native sizes and alignments from the C compiler
@@ -81,13 +88,16 @@
                }
 
     pre_include_bits = []
-    for fmtchar, ctype in INSPECT.items():
+    field_names = dict.fromkeys(INSPECT)
+    for fmtchar, ctype in INSPECT.iteritems():
+        field_name = ctype.replace(" ", "_").replace("*", "star")
+        field_names[fmtchar] = field_name
         pre_include_bits.append("""
             struct about_%s {
                 char pad;
                 %s field;
             };
-        """ % (fmtchar, ctype))
+        """ % (field_name, ctype))
 
     class CConfig:
         _compilation_info_ = ExternalCompilationInfo(
@@ -95,14 +105,14 @@
         )
 
     for fmtchar, ctype in INSPECT.items():
-        setattr(CConfig, fmtchar, rffi_platform.Struct(
-            "struct about_%s" % (fmtchar,),
+        setattr(CConfig, field_names[fmtchar], rffi_platform.Struct(
+            "struct about_%s" % (field_names[fmtchar],),
             [('field', lltype.FixedSizeArray(rffi.CHAR, 1))]))
 
     cConfig = rffi_platform.configure(CConfig)
 
     for fmtchar, ctype in INSPECT.items():
-        S = cConfig[fmtchar]
+        S = cConfig[field_names[fmtchar]]
         alignment = rffi.offsetof(S, 'c_field')
         size = rffi.sizeof(S.c_field)
         signed = 'a' <= fmtchar <= 'z'
@@ -113,6 +123,9 @@
         elif fmtchar == 'd':
             pack = pack_double
             unpack = unpack_double
+        elif fmtchar == '?':
+            pack = pack_bool
+            unpack = unpack_bool
         else:
             cpython_checks_range = fmtchar in 'bBhH'
             pack = std.make_int_packer(size, signed, cpython_checks_range)

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 22:03:16 2010
@@ -150,7 +150,7 @@
 
 @specialize.argtype(0)
 def unpack_bool(fmtiter):
-    fmtiter.appendobj(bool(int(fmtiter.read(1))))
+    fmtiter.appendobj(bool(ord(fmtiter.read(1))))
 
 @specialize.argtype(0)
 def unpack_string(fmtiter, count):



More information about the Pypy-commit mailing list