[pypy-commit] cffi default: Tweaks and minor fixes

arigo noreply at buildbot.pypy.org
Sat May 30 13:57:19 CEST 2015


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r2132:3c02643ecb69
Date: 2015-05-30 13:52 +0200
http://bitbucket.org/cffi/cffi/changeset/3c02643ecb69/

Log:	Tweaks and minor fixes

diff --git a/c/realize_c_type.c b/c/realize_c_type.c
--- a/c/realize_c_type.c
+++ b/c/realize_c_type.c
@@ -14,9 +14,10 @@
 
 static PyObject *build_primitive_type(int num);   /* forward */
 
+#define primitive_in_range(num)   ((num) >= 0 && (num) < _CFFI__NUM_PRIM)
 #define get_primitive_type(num)                                 \
-    (all_primitives[num] != NULL ? all_primitives[num]          \
-                                 : build_primitive_type(num))
+    ((primitive_in_range(num) && all_primitives[num] != NULL) ? \
+        all_primitives[num] : build_primitive_type(num))
 
 static int init_global_types_dict(PyObject *ffi_type_dict)
 {
@@ -153,14 +154,18 @@
     };
     PyObject *x;
 
+    assert(sizeof(primitive_name) == sizeof(*primitive_name) * _CFFI__NUM_PRIM);
     if (num == _CFFI_PRIM_VOID) {
         x = new_void_type();
     }
-    else if (0 <= num &&
-             num < sizeof(primitive_name) / sizeof(*primitive_name) &&
-             primitive_name[num] != NULL) {
+    else if (primitive_in_range(num) && primitive_name[num] != NULL) {
         x = new_primitive_type(primitive_name[num]);
     }
+    else if (num == _CFFI__UNKNOWN_PRIM) {
+        PyErr_SetString(FFIError, "primitive integer type with an unexpected "
+                        "size (or not an integer type at all)");
+        return NULL;
+    }
     else {
         PyErr_Format(PyExc_NotImplementedError, "prim=%d", num);
         return NULL;
diff --git a/cffi/_cffi_include.h b/cffi/_cffi_include.h
--- a/cffi/_cffi_include.h
+++ b/cffi/_cffi_include.h
@@ -212,7 +212,7 @@
      (size) == 2 ? ((sign) ? _CFFI_PRIM_INT16 : _CFFI_PRIM_UINT16) :    \
      (size) == 4 ? ((sign) ? _CFFI_PRIM_INT32 : _CFFI_PRIM_UINT32) :    \
      (size) == 8 ? ((sign) ? _CFFI_PRIM_INT64 : _CFFI_PRIM_UINT64) :    \
-     0)
+     _CFFI__UNKNOWN_PRIM)
 
 #define _cffi_check_int(got, got_nonpos, expected)      \
     ((got_nonpos) == (expected <= 0) &&                 \
diff --git a/cffi/cffi_opcode.py b/cffi/cffi_opcode.py
--- a/cffi/cffi_opcode.py
+++ b/cffi/cffi_opcode.py
@@ -12,13 +12,13 @@
         return '_CFFI_OP(_CFFI_OP_%s, %s)' % (classname, self.arg)
 
     def as_python_bytes(self):
-        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)
+        if self.op is None and 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)
+        if isinstance(self.arg, str):
             from .ffiplatform import VerificationError
             raise VerificationError("cannot emit to Python: %r" % (self.arg,))
         return format_four_bytes((self.arg << 8) | self.op)
@@ -105,6 +105,7 @@
 PRIM_UINTMAX       = 47
 
 _NUM_PRIM          = 48
+_UNKNOWN_PRIM      = -1
 
 PRIMITIVE_TO_INDEX = {
     'char':               PRIM_CHAR,
diff --git a/cffi/parse_c_type.h b/cffi/parse_c_type.h
--- a/cffi/parse_c_type.h
+++ b/cffi/parse_c_type.h
@@ -78,6 +78,7 @@
 #define _CFFI_PRIM_UINTMAX      47
 
 #define _CFFI__NUM_PRIM         48
+#define _CFFI__UNKNOWN_PRIM    (-1)
 
 
 struct _cffi_global_s {


More information about the pypy-commit mailing list