Author: Armin Rigo <ar...@tunes.org>
Branch: cffi-1.0
Changeset: r77235:e3cd5b3b4c2f
Date: 2015-05-08 21:24 +0200
http://bitbucket.org/pypy/pypy/changeset/e3cd5b3b4c2f/

Log:    Accept unicode literals in type declarations

diff --git a/pypy/module/_cffi_backend/ffi_obj.py 
b/pypy/module/_cffi_backend/ffi_obj.py
--- a/pypy/module/_cffi_backend/ffi_obj.py
+++ b/pypy/module/_cffi_backend/ffi_obj.py
@@ -79,7 +79,8 @@
 
     def ffi_type(self, w_x, accept):
         space = self.space
-        if (accept & ACCEPT_STRING) and space.isinstance_w(w_x, space.w_str):
+        if (accept & ACCEPT_STRING) and (
+                space.isinstance_w(w_x, space.w_basestring)):
             string = space.str_w(w_x)
             consider_fn_as_fnptr = (accept & CONSIDER_FN_AS_FNPTR) != 0
             if jit.isconstant(string):
diff --git a/pypy/module/_cffi_backend/test/test_recompiler.py 
b/pypy/module/_cffi_backend/test/test_recompiler.py
--- a/pypy/module/_cffi_backend/test/test_recompiler.py
+++ b/pypy/module/_cffi_backend/test/test_recompiler.py
@@ -273,6 +273,7 @@
         #
         assert ffi.offsetof("struct foo_s", "a") == 0
         assert ffi.offsetof("struct foo_s", "b") == 4
+        assert ffi.offsetof(u"struct foo_s", u"b") == 4
         #
         raises(TypeError, ffi.addressof, p)
         assert ffi.addressof(p[0]) == p
diff --git a/pypy/module/_cffi_backend/test/test_unicode_literals.py 
b/pypy/module/_cffi_backend/test/test_unicode_literals.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/_cffi_backend/test/test_unicode_literals.py
@@ -0,0 +1,56 @@
+#
+# ----------------------------------------------
+# WARNING, ALL LITERALS IN THIS FILE ARE UNICODE
+# ----------------------------------------------
+#
+from __future__ import unicode_literals
+#
+#
+#
+from pypy.module._cffi_backend.newtype import _clean_cache
+
+
+class AppTestUnicodeLiterals:
+    spaceconfig = dict(usemodules=('_cffi_backend', ))
+
+    def teardown_method(self, meth):
+        _clean_cache(self.space)
+
+    def test_cast(self):
+        from _cffi_backend import FFI
+        ffi = FFI()
+        assert int(ffi.cast("int", 3.14)) == 3        # unicode literal
+
+    def test_new(self):
+        from _cffi_backend import FFI
+        ffi = FFI()
+        assert ffi.new("int[]", [3, 4, 5])[2] == 5    # unicode literal
+
+    def test_typeof(self):
+        from _cffi_backend import FFI
+        ffi = FFI()
+        tp = ffi.typeof("int[51]")                    # unicode literal
+        assert tp.length == 51
+
+    def test_sizeof(self):
+        from _cffi_backend import FFI
+        ffi = FFI()
+        assert ffi.sizeof("int[51]") == 51 * 4        # unicode literal
+
+    def test_alignof(self):
+        from _cffi_backend import FFI
+        ffi = FFI()
+        assert ffi.alignof("int[51]") == 4            # unicode literal
+
+    def test_getctype(self):
+        from _cffi_backend import FFI
+        ffi = FFI()
+        assert ffi.getctype("int**") == "int * *"     # unicode literal
+        assert type(ffi.getctype("int**")) is str
+
+    def test_callback(self):
+        from _cffi_backend import FFI
+        ffi = FFI()
+        cb = ffi.callback("int(int)",                 # unicode literal
+                          lambda x: x + 42)
+        assert cb(5) == 47
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to