Commits

Armin Rigo committed 69cddb8

Fixes for Python 3.

Comments (0)

Files changed (9)

     assert "must be a str or int, not NoneType" in str(e.value)
 
 def test_enum_overflow():
-    for ovf in (sys.maxint+1, -sys.maxint-2, 2**31, -2**31-1):
+    for ovf in (2**63, -2**63-1, 2**31, -2**31-1):
         e = py.test.raises(OverflowError, new_enum_type, "foo", ('a', 'b'),
                            (5, ovf))
         assert str(e.value) == (
     py.test.raises(OverflowError, newp, BBoolP, 2)
     py.test.raises(OverflowError, newp, BBoolP, -1)
     BCharP = new_pointer_type(new_primitive_type("char"))
-    p = newp(BCharP, 'X')
+    p = newp(BCharP, b'X')
     q = cast(BBoolP, p)
-    assert q[0] == ord('X')
+    assert q[0] == ord(b'X')
     py.test.raises(TypeError, string, cast(BBool, False))
     BDouble = new_primitive_type("double")
     assert int(cast(BBool, cast(BDouble, 0.1))) == 1
         csource = '\n'.join(csourcelines)
         try:
             ast = _get_parser().parse(csource)
-        except pycparser.c_parser.ParseError, e:
+        except pycparser.c_parser.ParseError as e:
             self.convert_pycparser_error(e, csource)
         return ast, macros
 
             type(ffi._backend).__typecache = weakref.WeakValueDictionary()
     try:
         res = getattr(ffi._backend, funcname)(*args)
-    except NotImplementedError, e:
+    except NotImplementedError as e:
         raise NotImplementedError("%r: %s" % (srctype, e))
     ffi._backend.__typecache[args] = res
     return res

cffi/vengine_cpy.py

                 try:
                     prnt('  { %s = &p->%s; (void)tmp; }' % (
                         ftype.get_c_name('(*tmp)', 'field %r'%fname), fname))
-                except ffiplatform.VerificationError, e:
+                except ffiplatform.VerificationError as e:
                     prnt('  /* %s */' % str(e))   # cannot verify it, ignore
         prnt('}')
         prnt('static PyObject *')

cffi/vengine_gen.py

                 try:
                     prnt('  { %s = &p->%s; (void)tmp; }' % (
                         ftype.get_c_name('(*tmp)', 'field %r'%fname), fname))
-                except ffiplatform.VerificationError, e:
+                except ffiplatform.VerificationError as e:
                     prnt('  /* %s */' % str(e))   # cannot verify it, ignore
         prnt('}')
         self.export_symbols.append(layoutfuncname)

testing/backend_tests.py

             return a - b
         #
         assert cb(-100, -10) == -90
-        assert cb(sys.maxint, -10) == 42
+        sz = ffi.sizeof("long")
+        assert cb((1 << (sz*8-1)) - 1, -10) == 42
 
     def test_unique_types(self):
         ffi1 = FFI(backend=self.Backend())
         assert not isinstance(ffi.typeof("int"), ffi.CData)
         assert not isinstance(ffi.cast("int", 0), ffi.CType)
         assert not isinstance(ffi.new("int *"), ffi.CType)
+
+    def test_CData_CType_2(self):
+        ffi = FFI(backend=self.Backend())
         assert isinstance(ffi.typeof("int"), ffi.CType)
 
     def test_bool(self):
         ffi = FFI(backend=self.Backend())
         assert int(ffi.cast("_Bool", 0.1)) == 1
         assert int(ffi.cast("_Bool", -0.0)) == 0
-        assert int(ffi.cast("_Bool", '\x02')) == 1
-        assert int(ffi.cast("_Bool", '\x00')) == 0
-        assert int(ffi.cast("_Bool", '\x80')) == 1
+        assert int(ffi.cast("_Bool", b'\x02')) == 1
+        assert int(ffi.cast("_Bool", b'\x00')) == 0
+        assert int(ffi.cast("_Bool", b'\x80')) == 1
         assert ffi.new("_Bool *", False)[0] == 0
         assert ffi.new("_Bool *", 1)[0] == 1
         py.test.raises(OverflowError, ffi.new, "_Bool *", 2)

testing/callback_in_thread.py

         assert count > 0, "timeout"
     assert seen == [(10, 10), (12, 15)]
 
-print 'STARTING'
+print('STARTING')
 _run_callback_in_thread()
-print 'DONE'
+print('DONE')

testing/test_ctypes.py

-import py
+import py, sys
 from testing import backend_tests
 from cffi.backend_ctypes import CTypesBackend
 
 
     def test_nested_anonymous_union(self):
         py.test.skip("ctypes backend: not supported: nested anonymous union")
+
+    def test_CData_CType_2(self):
+        if sys.version_info >= (3,):
+            py.test.skip("ctypes backend: not supported in Python 3: CType")
+        backend_tests.BackendTests.test_CData_CType_2(self)

testing/test_verify.py

     p = ffi.new("struct foo_s *")
     assert ffi.sizeof(p[0]) == 3 * ffi.sizeof("int")    # with alignment
     p.a = 1234567
-    p.b = 'X'
-    p.c = 'Y'
+    p.b = b'X'
+    p.c = b'Y'
     assert p.a == 1234567
-    assert p.b == 'X'
-    assert p.c == 'Y'
-    assert p.d == 'Y'
+    assert p.b == b'X'
+    assert p.c == b'Y'
+    assert p.d == b'Y'
 
 def test_nested_anonymous_struct_exact_error():
     if sys.platform == 'win32':