Commits

Armin Rigo committed d7270f8

Found a way to be compatible with Python 3.2 in the test syntax.

Comments (0)

Files changed (4)

     readbuf = str
     bufchar = lambda x: x
     bytechr = chr
+    class U(object):
+        def __add__(self, other):
+            return eval('u'+repr(other).replace(r'\\u', r'\u')
+                                       .replace(r'\\U', r'\U'))
+    u = U()
 else:
     type_or_class = "class"
     long = int
     readbuf = lambda buf: buf.tobytes()
     bufchar = ord
     bytechr = lambda n: bytes([n])
+    u = ""
 
 def size_of_int():
     BInt = new_primitive_type("int")
         py.test.raises(TypeError, cast, p, None)
         assert long(cast(p, min - 1)) == max
         assert int(cast(p, b'\x08')) == 8
-        assert int(cast(p, u'\x08')) == 8
+        assert int(cast(p, u+'\x08')) == 8
     for name in ['char', 'short', 'int', 'long', 'long long']:
         p = new_primitive_type('unsigned ' + name)
         size = sizeof(p)
         assert int(cast(p, max + 1)) == 0
         assert long(cast(p, -1)) == max
         assert int(cast(p, b'\xFE')) == 254
-        assert int(cast(p, u'\xFE')) == 254
+        assert int(cast(p, u+'\xFE')) == 254
 
 def test_no_float_on_int_types():
     p = new_primitive_type('long')
         assert cast(p, -1.1) != cast(p, -1.1)
         assert repr(float(cast(p, -0.0))) == '-0.0'
         assert float(cast(p, b'\x09')) == 9.0
-        assert float(cast(p, u'\x09')) == 9.0
+        assert float(cast(p, u+'\x09')) == 9.0
         assert float(cast(p, True)) == 1.0
         py.test.raises(TypeError, cast, p, None)
 
     assert p[0] == b'A'
     py.test.raises(TypeError, newp, BPtr, 65)
     py.test.raises(TypeError, newp, BPtr, b"foo")
-    py.test.raises(TypeError, newp, BPtr, u"foo")
+    py.test.raises(TypeError, newp, BPtr, u+"foo")
     c = cast(BChar, b'A')
     assert str(c) == repr(c)
     assert int(c) == ord(b'A')
     py.test.raises(TypeError, cast, BChar, b'foo')
-    py.test.raises(TypeError, cast, BChar, u'foo')
+    py.test.raises(TypeError, cast, BChar, u+'foo')
 
 def test_reading_pointer_to_pointer():
     BVoidP = new_pointer_type(new_void_type())
     #
     py.test.raises(TypeError, f, 123456)
     py.test.raises(TypeError, f, "foo")
-    py.test.raises(TypeError, f, u"bar")
+    py.test.raises(TypeError, f, u+"bar")
 
 def test_call_function_7():
     BChar = new_primitive_type("char")
     assert f(255) == b'\xFF'
 
 def _hacked_pypy_uni4():
-    pyuni4 = {1: True, 2: False}[len(u'\U00012345')]
+    pyuni4 = {1: True, 2: False}[len(u+'\U00012345')]
     return 'PY_DOT_PY' in globals() and not pyuni4
 
 def test_callback_returning_wchar_t():
     BWChar = new_primitive_type("wchar_t")
     def cb(n):
         if n == -1:
-            return u'\U00012345'
+            return u+'\U00012345'
         if n == -2:
             raise ValueError
         return unichr(n)
     f = callback(BFunc, cb)
     assert f(0) == unichr(0)
     assert f(255) == unichr(255)
-    assert f(0x1234) == u'\u1234'
+    assert f(0x1234) == u+'\u1234'
     if sizeof(BWChar) == 4 and not _hacked_pypy_uni4():
-        assert f(-1) == u'\U00012345'
-    assert f(-2) == u'\x00'   # and an exception printed to stderr
+        assert f(-1) == u+'\U00012345'
+    assert f(-2) == u+'\x00'   # and an exception printed to stderr
 
 def test_struct_with_bitfields():
     BLong = new_primitive_type("long")
 
 def test_string_wchar():
     BWChar = new_primitive_type("wchar_t")
-    assert string(cast(BWChar, 42)) == u'*'
-    assert string(cast(BWChar, 0x4253)) == u'\u4253'
-    assert string(cast(BWChar, 0)) == u'\x00'
+    assert string(cast(BWChar, 42)) == u+'*'
+    assert string(cast(BWChar, 0x4253)) == u+'\u4253'
+    assert string(cast(BWChar, 0)) == u+'\x00'
     BArray = new_array_type(new_pointer_type(BWChar), None)
-    a = newp(BArray, [u'A', u'B', u'C'])
-    assert type(string(a)) is unicode and string(a) == u'ABC'
+    a = newp(BArray, [u+'A', u+'B', u+'C'])
+    assert type(string(a)) is unicode and string(a) == u+'ABC'
     if 'PY_DOT_PY' not in globals() and sys.version_info < (3,):
-        assert string(a, 8).startswith(u'ABC') # may contain additional garbage
+        assert string(a, 8).startswith(u+'ABC') # may contain additional garbage
 
 def test_string_typeerror():
     BShort = new_primitive_type("short")
 def test_wchar():
     BWChar = new_primitive_type("wchar_t")
     BInt = new_primitive_type("int")
-    pyuni4 = {1: True, 2: False}[len(u'\U00012345')]
+    pyuni4 = {1: True, 2: False}[len(u+'\U00012345')]
     wchar4 = {2: False, 4: True}[sizeof(BWChar)]
     assert str(cast(BWChar, 0x45)) == "<cdata 'wchar_t' %s'E'>" % (
         mandatory_u_prefix,)
     complete_struct_or_union(BStruct, [('a1', BWChar, -1),
                                        ('a2', BWCharP, -1)])
     s = newp(BStructPtr)
-    s.a1 = u'\x00'
-    assert s.a1 == u'\x00'
+    s.a1 = u+'\x00'
+    assert s.a1 == u+'\x00'
     py.test.raises(TypeError, "s.a1 = b'a'")
     py.test.raises(TypeError, "s.a1 = bytechr(0xFF)")
-    s.a1 = u'\u1234'
-    assert s.a1 == u'\u1234'
+    s.a1 = u+'\u1234'
+    assert s.a1 == u+'\u1234'
     if pyuni4:
         assert wchar4
-        s.a1 = u'\U00012345'
-        assert s.a1 == u'\U00012345'
+        s.a1 = u+'\U00012345'
+        assert s.a1 == u+'\U00012345'
     elif wchar4:
         if not _hacked_pypy_uni4():
             s.a1 = cast(BWChar, 0x12345)
-            assert s.a1 == u'\ud808\udf45'
-            s.a1 = u'\ud807\udf44'
-            assert s.a1 == u'\U00011f44'
+            assert s.a1 == u+'\ud808\udf45'
+            s.a1 = u+'\ud807\udf44'
+            assert s.a1 == u+'\U00011f44'
     else:
-        py.test.raises(TypeError, "s.a1 = u'\U00012345'")
+        py.test.raises(TypeError, "s.a1 = u+'\U00012345'")
     #
     BWCharArray = new_array_type(BWCharP, None)
-    a = newp(BWCharArray, u'hello \u1234 world')
+    a = newp(BWCharArray, u+'hello \u1234 world')
     assert len(a) == 14   # including the final null
-    assert string(a) == u'hello \u1234 world'
-    a[13] = u'!'
-    assert string(a) == u'hello \u1234 world!'
+    assert string(a) == u+'hello \u1234 world'
+    a[13] = u+'!'
+    assert string(a) == u+'hello \u1234 world!'
     assert str(a) == repr(a)
-    assert a[6] == u'\u1234'
-    a[6] = u'-'
-    assert string(a) == u'hello - world!'
+    assert a[6] == u+'\u1234'
+    a[6] = u+'-'
+    assert string(a) == u+'hello - world!'
     assert str(a) == repr(a)
     #
     if wchar4 and not _hacked_pypy_uni4():
-        u = u'\U00012345\U00012346\U00012347'
-        a = newp(BWCharArray, u)
+        u1 = u+'\U00012345\U00012346\U00012347'
+        a = newp(BWCharArray, u1)
         assert len(a) == 4
-        assert string(a) == u
+        assert string(a) == u1
         assert len(list(a)) == 4
-        expected = [u'\U00012345', u'\U00012346', u'\U00012347', unichr(0)]
+        expected = [u+'\U00012345', u+'\U00012346', u+'\U00012347', unichr(0)]
         assert list(a) == expected
         got = [a[i] for i in range(4)]
         assert got == expected
     w = cast(BWChar, 'a')
     assert repr(w) == "<cdata 'wchar_t' %s'a'>" % mandatory_u_prefix
     assert str(w) == repr(w)
-    assert string(w) == u'a'
+    assert string(w) == u+'a'
     assert int(w) == ord('a')
     w = cast(BWChar, 0x1234)
     assert repr(w) == "<cdata 'wchar_t' %s'\u1234'>" % mandatory_u_prefix
     assert str(w) == repr(w)
-    assert string(w) == u'\u1234'
+    assert string(w) == u+'\u1234'
     assert int(w) == 0x1234
-    w = cast(BWChar, u'\u8234')
+    w = cast(BWChar, u+'\u8234')
     assert repr(w) == "<cdata 'wchar_t' %s'\u8234'>" % mandatory_u_prefix
     assert str(w) == repr(w)
-    assert string(w) == u'\u8234'
+    assert string(w) == u+'\u8234'
     assert int(w) == 0x8234
-    w = cast(BInt, u'\u1234')
+    w = cast(BInt, u+'\u1234')
     assert repr(w) == "<cdata 'int' 4660>"
     if wchar4 and not _hacked_pypy_uni4():
-        w = cast(BWChar, u'\U00012345')
+        w = cast(BWChar, u+'\U00012345')
         assert repr(w) == "<cdata 'wchar_t' %s'\U00012345'>" % (
             mandatory_u_prefix,)
         assert str(w) == repr(w)
-        assert string(w) == u'\U00012345'
+        assert string(w) == u+'\U00012345'
         assert int(w) == 0x12345
-        w = cast(BInt, u'\U00012345')
+        w = cast(BInt, u+'\U00012345')
         assert repr(w) == "<cdata 'int' 74565>"
-    py.test.raises(TypeError, cast, BInt, u'')
-    py.test.raises(TypeError, cast, BInt, u'XX')
-    assert int(cast(BInt, u'a')) == ord('a')
+    py.test.raises(TypeError, cast, BInt, u+'')
+    py.test.raises(TypeError, cast, BInt, u+'XX')
+    assert int(cast(BInt, u+'a')) == ord('a')
     #
-    a = newp(BWCharArray, u'hello - world')
+    a = newp(BWCharArray, u+'hello - world')
     p = cast(BWCharP, a)
-    assert string(p) == u'hello - world'
-    p[6] = u'\u2345'
-    assert string(p) == u'hello \u2345 world'
+    assert string(p) == u+'hello - world'
+    p[6] = u+'\u2345'
+    assert string(p) == u+'hello \u2345 world'
     #
-    s = newp(BStructPtr, [u'\u1234', p])
-    assert s.a1 == u'\u1234'
+    s = newp(BStructPtr, [u+'\u1234', p])
+    assert s.a1 == u+'\u1234'
     assert s.a2 == p
     assert str(s.a2) == repr(s.a2)
-    assert string(s.a2) == u'hello \u2345 world'
+    assert string(s.a2) == u+'hello \u2345 world'
     #
     q = cast(BWCharP, 0)
     assert str(q) == repr(q)
         return len(string(p))
     BFunc = new_function_type((BWCharP,), BInt, False)
     f = callback(BFunc, cb, -42)
-    assert f(u'a\u1234b') == 3
+    assert f(u+'a\u1234b') == 3
     #
     if wchar4 and not pyuni4 and not _hacked_pypy_uni4():
         # try out-of-range wchar_t values

testing/backend_tests.py

 import py
 import sys, ctypes
 from cffi import FFI, CDefError
+from testing.support import *
 
 SIZE_OF_INT   = ctypes.sizeof(ctypes.c_int)
 SIZE_OF_LONG  = ctypes.sizeof(ctypes.c_long)
 SIZE_OF_PTR   = ctypes.sizeof(ctypes.c_void_p)
 SIZE_OF_WCHAR = ctypes.sizeof(ctypes.c_wchar)
 
-if sys.version_info >= (3,):
-    unicode = str
-    long = int
-
 
 class BackendTests:
 
         assert int(ffi.cast("char", 300)) == 300 - 256
         assert bool(ffi.cast("char", 0))
         py.test.raises(TypeError, ffi.new, "char*", 32)
-        py.test.raises(TypeError, ffi.new, "char*", u"x")
+        py.test.raises(TypeError, ffi.new, "char*", u+"x")
         py.test.raises(TypeError, ffi.new, "char*", b"foo")
         #
         p = ffi.new("char[]", [b'a', b'b', b'\x9c'])
     def test_wchar_t(self):
         ffi = FFI(backend=self.Backend())
         self.check_wchar_t(ffi)
-        assert ffi.new("wchar_t*", u'x')[0] == u'x'
-        assert ffi.new("wchar_t*", u'\u1234')[0] == u'\u1234'
+        assert ffi.new("wchar_t*", u+'x')[0] == u+'x'
+        assert ffi.new("wchar_t*", u+'\u1234')[0] == u+'\u1234'
         if SIZE_OF_WCHAR > 2:
-            assert ffi.new("wchar_t*", u'\U00012345')[0] == u'\U00012345'
+            assert ffi.new("wchar_t*", u+'\U00012345')[0] == u+'\U00012345'
         else:
-            py.test.raises(TypeError, ffi.new, "wchar_t*", u'\U00012345')
-        assert ffi.new("wchar_t*")[0] == u'\x00'
+            py.test.raises(TypeError, ffi.new, "wchar_t*", u+'\U00012345')
+        assert ffi.new("wchar_t*")[0] == u+'\x00'
         assert int(ffi.cast("wchar_t", 300)) == 300
         assert bool(ffi.cast("wchar_t", 0))
         py.test.raises(TypeError, ffi.new, "wchar_t*", 32)
         py.test.raises(TypeError, ffi.new, "wchar_t*", "foo")
         #
-        p = ffi.new("wchar_t[]", [u'a', u'b', u'\u1234'])
+        p = ffi.new("wchar_t[]", [u+'a', u+'b', u+'\u1234'])
         assert len(p) == 3
-        assert p[0] == u'a'
-        assert p[1] == u'b' and type(p[1]) is type(u'')
-        assert p[2] == u'\u1234'
-        p[0] = u'x'
-        assert p[0] == u'x' and type(p[0]) is type(u'')
-        p[1] = u'\u1357'
-        assert p[1] == u'\u1357'
-        p = ffi.new("wchar_t[]", u"abcd")
+        assert p[0] == u+'a'
+        assert p[1] == u+'b' and type(p[1]) is unicode
+        assert p[2] == u+'\u1234'
+        p[0] = u+'x'
+        assert p[0] == u+'x' and type(p[0]) is unicode
+        p[1] = u+'\u1357'
+        assert p[1] == u+'\u1357'
+        p = ffi.new("wchar_t[]", u+"abcd")
         assert len(p) == 5
-        assert p[4] == u'\x00'
-        p = ffi.new("wchar_t[]", u"a\u1234b")
+        assert p[4] == u+'\x00'
+        p = ffi.new("wchar_t[]", u+"a\u1234b")
         assert len(p) == 4
-        assert p[1] == u'\u1234'
+        assert p[1] == u+'\u1234'
         #
-        p = ffi.new("wchar_t[]", u'\U00023456')
+        p = ffi.new("wchar_t[]", u+'\U00023456')
         if SIZE_OF_WCHAR == 2:
             assert sys.maxunicode == 0xffff
             assert len(p) == 3
-            assert p[0] == u'\ud84d'
-            assert p[1] == u'\udc56'
-            assert p[2] == u'\x00'
+            assert p[0] == u+'\ud84d'
+            assert p[1] == u+'\udc56'
+            assert p[2] == u+'\x00'
         else:
             assert len(p) == 2
-            assert p[0] == u'\U00023456'
-            assert p[1] == u'\x00'
+            assert p[0] == u+'\U00023456'
+            assert p[1] == u+'\x00'
         #
-        p = ffi.new("wchar_t[4]", u"ab")
+        p = ffi.new("wchar_t[4]", u+"ab")
         assert len(p) == 4
-        assert [p[i] for i in range(4)] == [u'a', u'b', u'\x00', u'\x00']
-        p = ffi.new("wchar_t[2]", u"ab")
+        assert [p[i] for i in range(4)] == [u+'a', u+'b', u+'\x00', u+'\x00']
+        p = ffi.new("wchar_t[2]", u+"ab")
         assert len(p) == 2
-        assert [p[i] for i in range(2)] == [u'a', u'b']
-        py.test.raises(IndexError, ffi.new, "wchar_t[2]", u"abc")
+        assert [p[i] for i in range(2)] == [u+'a', u+'b']
+        py.test.raises(IndexError, ffi.new, "wchar_t[2]", u+"abc")
 
     def test_none_as_null_doesnt_work(self):
         ffi = FFI(backend=self.Backend())
     def test_unicode_from_wchar_pointer(self):
         ffi = FFI(backend=self.Backend())
         self.check_wchar_t(ffi)
-        x = ffi.new("wchar_t*", u"x")
+        x = ffi.new("wchar_t*", u+"x")
         assert unicode(x) == unicode(repr(x))
-        assert ffi.string(x) == u"x"
-        assert ffi.string(ffi.new("wchar_t*", u"\x00")) == u""
+        assert ffi.string(x) == u+"x"
+        assert ffi.string(ffi.new("wchar_t*", u+"\x00")) == u+""
 
     def test_string_from_char_array(self):
         ffi = FFI(backend=self.Backend())
     def test_string_from_wchar_array(self):
         ffi = FFI(backend=self.Backend())
         self.check_wchar_t(ffi)
-        assert ffi.string(ffi.cast("wchar_t", "x")) == u"x"
-        assert ffi.string(ffi.cast("wchar_t", u"x")) == u"x"
+        assert ffi.string(ffi.cast("wchar_t", "x")) == u+"x"
+        assert ffi.string(ffi.cast("wchar_t", u+"x")) == u+"x"
         x = ffi.cast("wchar_t", "x")
         assert str(x) == repr(x)
-        assert ffi.string(x) == u"x"
+        assert ffi.string(x) == u+"x"
         #
-        p = ffi.new("wchar_t[]", u"hello.")
-        p[5] = u'!'
-        assert ffi.string(p) == u"hello!"
-        p[6] = u'\u04d2'
-        assert ffi.string(p) == u"hello!\u04d2"
-        p[3] = u'\x00'
-        assert ffi.string(p) == u"hel"
-        assert ffi.string(p, 123) == u"hel"
-        py.test.raises(IndexError, "p[7] = u'X'")
+        p = ffi.new("wchar_t[]", u+"hello.")
+        p[5] = u+'!'
+        assert ffi.string(p) == u+"hello!"
+        p[6] = u+'\u04d2'
+        assert ffi.string(p) == u+"hello!\u04d2"
+        p[3] = u+'\x00'
+        assert ffi.string(p) == u+"hel"
+        assert ffi.string(p, 123) == u+"hel"
+        py.test.raises(IndexError, "p[7] = u+'X'")
         #
-        a = ffi.new("wchar_t[]", u"hello\x00world")
+        a = ffi.new("wchar_t[]", u+"hello\x00world")
         assert len(a) == 12
         p = ffi.cast("wchar_t *", a)
-        assert ffi.string(p) == u'hello'
-        assert ffi.string(p, 123) == u'hello'
-        assert ffi.string(p, 5) == u'hello'
-        assert ffi.string(p, 2) == u'he'
+        assert ffi.string(p) == u+'hello'
+        assert ffi.string(p, 123) == u+'hello'
+        assert ffi.string(p, 5) == u+'hello'
+        assert ffi.string(p, 2) == u+'he'
 
     def test_fetch_const_char_p_field(self):
         # 'const' is ignored so far
         ffi = FFI(backend=self.Backend())
         self.check_wchar_t(ffi)
         ffi.cdef("struct foo { const wchar_t *name; };")
-        t = ffi.new("const wchar_t[]", u"testing")
+        t = ffi.new("const wchar_t[]", u+"testing")
         s = ffi.new("struct foo*", [t])
         assert type(s.name) not in (bytes, str, unicode)
-        assert ffi.string(s.name) == u"testing"
+        assert ffi.string(s.name) == u+"testing"
         s.name = ffi.NULL
         assert s.name == ffi.NULL
 
     def test_wchar_cast(self):
         ffi = FFI(backend=self.Backend())
         self.check_wchar_t(ffi)
-        p = ffi.cast("int", ffi.cast("wchar_t", u'\u1234'))
+        p = ffi.cast("int", ffi.cast("wchar_t", u+'\u1234'))
         assert int(p) == 0x1234
         p = ffi.cast("long long", ffi.cast("wchar_t", -1))
         if SIZE_OF_WCHAR == 2:      # 2 bytes, unsigned
             assert int(p) == 0xffff
         else:                       # 4 bytes, signed
             assert int(p) == -1
-        p = ffi.cast("int", u'\u1234')
+        p = ffi.cast("int", u+'\u1234')
         assert int(p) == 0x1234
 
     def test_cast_array_to_charp(self):

testing/support.py

+import sys
+
+if sys.version_info < (3,):
+    __all__ = ['u']
+
+    class U(object):
+        def __add__(self, other):
+            return eval('u'+repr(other).replace(r'\\u', r'\u')
+                                       .replace(r'\\U', r'\U'))
+    u = U()
+    assert u+'a\x00b' == eval(r"u'a\x00b'")
+    assert u+'a\u1234b' == eval(r"u'a\u1234b'")
+    assert u+'a\U00012345b' == eval(r"u'a\U00012345b'")
+
+else:
+    __all__ = ['u', 'unicode', 'long']
+    u = ""
+    unicode = str
+    long = int

testing/test_verify.py

 import py
 import sys, math
 from cffi import FFI, VerificationError, VerificationMissing, model
+from testing.support import *
 
 
 if sys.platform != 'win32':
 def test_wchar_type():
     ffi = FFI()
     if ffi.sizeof('wchar_t') == 2:
-        uniexample1 = u'\u1234'
-        uniexample2 = u'\u1235'
+        uniexample1 = u+'\u1234'
+        uniexample2 = u+'\u1235'
     else:
-        uniexample1 = u'\U00012345'
-        uniexample2 = u'\U00012346'
+        uniexample1 = u+'\U00012345'
+        uniexample2 = u+'\U00012346'
     #
     ffi.cdef("wchar_t foo(wchar_t);")
     lib = ffi.verify("wchar_t foo(wchar_t x) { return x+1; }")