Author: Armin Rigo <[email protected]>
Branch: cffi-1.0
Changeset: r2010:28556dc7fa95
Date: 2015-05-16 11:44 +0200
http://bitbucket.org/cffi/cffi/changeset/28556dc7fa95/

Log:    Array lengths

diff --git a/cffi/cffi_opcode.py b/cffi/cffi_opcode.py
--- a/cffi/cffi_opcode.py
+++ b/cffi/cffi_opcode.py
@@ -12,7 +12,15 @@
         return '_CFFI_OP(_CFFI_OP_%s, %d)' % (classname, self.arg)
 
     def as_python_bytes(self):
-        assert self.op is not None
+        if self.op is None:
+            if self.arg.isdigit():
+                value = int(self.arg)     # non-negative: '-' not in self.arg
+                if value >= 2**31:
+                    raise OverflowError("cannot emit %r: limited to 2**31-1"
+                                        % (self.arg,))
+                return format_four_bytes(value)
+            from .ffiplatform import VerificationError
+            raise VerificationError("cannot emit to Python: %r" % (self.arg,))
         return format_four_bytes((self.arg << 8) | self.op)
 
     def __str__(self):
diff --git a/cffi/recompiler.py b/cffi/recompiler.py
--- a/cffi/recompiler.py
+++ b/cffi/recompiler.py
@@ -1,5 +1,5 @@
 import os, sys, io
-from cffi import ffiplatform, model
+from . import ffiplatform, model
 from .cffi_opcode import *
 
 try:
diff --git a/testing/cffi1/test_dlopen.py b/testing/cffi1/test_dlopen.py
--- a/testing/cffi1/test_dlopen.py
+++ b/testing/cffi1/test_dlopen.py
@@ -155,3 +155,24 @@
     target = udir.join('test_no_cross_include.py')
     py.test.raises(VerificationError, make_py_source,
                    ffi, 'test_no_cross_include', str(target))
+
+def test_array():
+    ffi = FFI()
+    ffi.cdef("typedef int32_t my_array_t[42];")
+    target = udir.join('test_array.py')
+    assert make_py_source(ffi, 'test_array', str(target))
+    assert target.read() == r"""# auto-generated file
+import _cffi_backend
+
+ffi = _cffi_backend.FFI(b'test_array',
+    _types = b'\x00\x00\x15\x01\x00\x00\x00\x05\x00\x00\x00\x2A',
+    _typenames = (b'\x00\x00\x00\x01my_array_t',),
+)
+"""
+
+def test_array_overflow():
+    ffi = FFI()
+    ffi.cdef("typedef int32_t my_array_t[3000000000];")
+    target = udir.join('test_array_overflow.py')
+    py.test.raises(OverflowError, make_py_source,
+                   ffi, 'test_array_overflow', str(target))
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to