Author: Armin Rigo <[email protected]>
Branch: 
Changeset: r78057:d2d8c9799524
Date: 2015-06-12 19:28 +0200
http://bitbucket.org/pypy/pypy/changeset/d2d8c9799524/

Log:    import cffi/7cd6732be4e5

diff --git a/lib_pypy/cffi/cparser.py b/lib_pypy/cffi/cparser.py
--- a/lib_pypy/cffi/cparser.py
+++ b/lib_pypy/cffi/cparser.py
@@ -337,7 +337,7 @@
                 length = self._parse_constant(
                     typenode.dim, partial_length_ok=partial_length_ok)
             tp = self._get_type(typenode.type,
-                                partial_length_ok=(length == '...'))
+                                partial_length_ok=partial_length_ok)
             return model.ArrayType(tp, length)
         #
         if isinstance(typenode, pycparser.c_ast.PtrDecl):
diff --git a/lib_pypy/cffi/gc_weakref.py b/lib_pypy/cffi/gc_weakref.py
--- a/lib_pypy/cffi/gc_weakref.py
+++ b/lib_pypy/cffi/gc_weakref.py
@@ -5,7 +5,6 @@
     def __init__(self, ffi):
         self.ffi = ffi
         self.data = {}
-        self.nextindex = 0
 
     def build(self, cdata, destructor):
         # make a new cdata of the same type as the original one
@@ -18,7 +17,6 @@
             destructor(cdata)
         #
         key = ref(new_cdata, remove)
-        index = self.nextindex
-        self.nextindex = index + 1     # we're protected by the lock here
+        index = object()
         self.data[index] = key
         return new_cdata
diff --git a/lib_pypy/cffi/recompiler.py b/lib_pypy/cffi/recompiler.py
--- a/lib_pypy/cffi/recompiler.py
+++ b/lib_pypy/cffi/recompiler.py
@@ -749,10 +749,12 @@
     # named structs or unions
 
     def _field_type(self, tp_struct, field_name, tp_field):
-        if isinstance(tp_field, model.ArrayType) and tp_field.length == '...':
-            ptr_struct_name = tp_struct.get_c_name('*')
-            actual_length = '_cffi_array_len(((%s)0)->%s)' % (
-                ptr_struct_name, field_name)
+        if isinstance(tp_field, model.ArrayType):
+            actual_length = tp_field.length
+            if actual_length == '...':
+                ptr_struct_name = tp_struct.get_c_name('*')
+                actual_length = '_cffi_array_len(((%s)0)->%s)' % (
+                    ptr_struct_name, field_name)
             tp_item = self._field_type(tp_struct, '%s[0]' % field_name,
                                        tp_field.item)
             tp_field = model.ArrayType(tp_item, actual_length)
@@ -1055,8 +1057,10 @@
     # global variables
 
     def _global_type(self, tp, global_name):
-        if isinstance(tp, model.ArrayType) and tp.length == '...':
-            actual_length = '_cffi_array_len(%s)' % (global_name,)
+        if isinstance(tp, model.ArrayType):
+            actual_length = tp.length
+            if actual_length == '...':
+                actual_length = '_cffi_array_len(%s)' % (global_name,)
             tp_item = self._global_type(tp.item, '%s[0]' % global_name)
             tp = model.ArrayType(tp_item, actual_length)
         return tp
diff --git a/pypy/module/test_lib_pypy/cffi_tests/cffi0/test_zintegration.py 
b/pypy/module/test_lib_pypy/cffi_tests/cffi0/test_zintegration.py
--- a/pypy/module/test_lib_pypy/cffi_tests/cffi0/test_zintegration.py
+++ b/pypy/module/test_lib_pypy/cffi_tests/cffi0/test_zintegration.py
@@ -36,6 +36,8 @@
         paths = []
         for module in modules:
             target = __import__(module, None, None, [])
+            if not hasattr(target, '__file__'):   # for _cffi_backend on pypy
+                continue
             src = os.path.abspath(target.__file__)
             for end in ['__init__.pyc', '__init__.pyo', '__init__.py']:
                 if src.lower().endswith(end):
diff --git a/pypy/module/test_lib_pypy/cffi_tests/cffi1/test_recompiler.py 
b/pypy/module/test_lib_pypy/cffi_tests/cffi1/test_recompiler.py
--- a/pypy/module/test_lib_pypy/cffi_tests/cffi1/test_recompiler.py
+++ b/pypy/module/test_lib_pypy/cffi_tests/cffi1/test_recompiler.py
@@ -410,6 +410,11 @@
     # 'x' is another <built-in method> object on lib, made very indirectly
     x = type(lib).__dir__.__get__(lib)
     py.test.raises(TypeError, ffi.typeof, x)
+    #
+    # present on built-in functions on CPython; must be emulated on PyPy:
+    assert lib.sin.__name__ == 'sin'
+    assert lib.sin.__module__ == '_CFFI_test_math_sin_type'
+    assert lib.sin.__doc__ == 'direct call to the C function of the same name'
 
 def test_verify_anonymous_struct_with_typedef():
     ffi = FFI()
@@ -925,6 +930,18 @@
     assert ffi.typeof(s.a) == ffi.typeof("int[5][8]")
     assert ffi.typeof(s.a[0]) == ffi.typeof("int[8]")
 
+def test_struct_array_guess_length_3():
+    ffi = FFI()
+    ffi.cdef("struct foo_s { int a[][...]; };")
+    lib = verify(ffi, 'test_struct_array_guess_length_3',
+                 "struct foo_s { int x; int a[5][7]; int y; };")
+    assert ffi.sizeof('struct foo_s') == 37 * ffi.sizeof('int')
+    s = ffi.new("struct foo_s *")
+    assert ffi.typeof(s.a) == ffi.typeof("int(*)[7]")
+    assert s.a[4][6] == 0
+    py.test.raises(IndexError, 's.a[4][7]')
+    assert ffi.typeof(s.a[0]) == ffi.typeof("int[7]")
+
 def test_global_var_array_2():
     ffi = FFI()
     ffi.cdef("int a[...][...];")
@@ -936,6 +953,27 @@
     assert ffi.typeof(lib.a) == ffi.typeof("int[10][8]")
     assert ffi.typeof(lib.a[0]) == ffi.typeof("int[8]")
 
+def test_global_var_array_3():
+    ffi = FFI()
+    ffi.cdef("int a[][...];")
+    lib = verify(ffi, 'test_global_var_array_3', 'int a[10][8];')
+    lib.a[9][7] = 123456
+    assert lib.a[9][7] == 123456
+    py.test.raises(IndexError, 'lib.a[0][8]')
+    assert ffi.typeof(lib.a) == ffi.typeof("int(*)[8]")
+    assert ffi.typeof(lib.a[0]) == ffi.typeof("int[8]")
+
+def test_global_var_array_4():
+    ffi = FFI()
+    ffi.cdef("int a[10][...];")
+    lib = verify(ffi, 'test_global_var_array_4', 'int a[10][8];')
+    lib.a[9][7] = 123456
+    assert lib.a[9][7] == 123456
+    py.test.raises(IndexError, 'lib.a[0][8]')
+    py.test.raises(IndexError, 'lib.a[10][8]')
+    assert ffi.typeof(lib.a) == ffi.typeof("int[10][8]")
+    assert ffi.typeof(lib.a[0]) == ffi.typeof("int[8]")
+
 def test_some_integer_type():
     ffi = FFI()
     ffi.cdef("""
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to