[pypy-commit] cffi define-integer-constant: "010" is not valid oct in python3. Fix it.

mozbugbox noreply at buildbot.pypy.org
Fri Apr 4 16:52:45 CEST 2014


Author: mozbugbox <mozbugbox at yahoo.com.au>
Branch: define-integer-constant
Changeset: r1492:a9bac7f13149
Date: 2014-04-03 18:59 +0800
http://bitbucket.org/cffi/cffi/changeset/a9bac7f13149/

Log:	"010" is not valid oct in python3. Fix it.

diff --git a/cffi/cparser.py b/cffi/cparser.py
--- a/cffi/cparser.py
+++ b/cffi/cparser.py
@@ -212,7 +212,15 @@
             value = value.strip()
             match = _r_int_literal.search(value)
             if match is not None:
-                pyvalue = int(match.group(0).rstrip("ULul"), 0)
+                int_str = match.group(0).lower().rstrip("ul")
+
+                # "010" is not valid oct in py3
+                if (int_str.startswith("0") and
+                        int_str != "0" and
+                        not int_str.startswith("0x")):
+                    int_str = "0o" + int_str[1:]
+
+                pyvalue = int(int_str, 0)
                 if key not in self._int_constants:
                     self._int_constants[key] = pyvalue
                 else:
diff --git a/testing/backend_tests.py b/testing/backend_tests.py
--- a/testing/backend_tests.py
+++ b/testing/backend_tests.py
@@ -1585,6 +1585,7 @@
     def test_define_integer_constant(self):
         ffi = FFI(backend=self.Backend())
         ffi.cdef("""
+            #define DOT_0 0
             #define DOT 100
             #define DOT_OCT 0100l
             #define DOT_HEX 0x100u
@@ -1593,6 +1594,7 @@
         """)
         lib = ffi.dlopen(None)
         assert ffi.string(ffi.cast("enum foo", 100)) == "BB"
+        assert lib.DOT_0 == 0
         assert lib.DOT == 100
         assert lib.DOT_OCT == 0o100
         assert lib.DOT_HEX == 0x100


More information about the pypy-commit mailing list