Commits

Armin Rigo committed 7756db3

Remove _get_ct_long() and a fragile detail about sizeof(ffi_arg).

Comments (0)

Files changed (1)

c/_cffi_backend.c

     return ct_int;
 }
 
-static CTypeDescrObject *_get_ct_long(void)
-{
-    static CTypeDescrObject *ct_long = NULL;
-    if (ct_long == NULL) {
-        PyObject *args = Py_BuildValue("(s)", "long");
-        if (args == NULL)
-            return NULL;
-        ct_long = (CTypeDescrObject *)b_new_primitive_type(NULL, args);
-        Py_DECREF(args);
-    }
-    return ct_long;
-}
-
 static PyObject*
 cdata_call(CDataObject *cd, PyObject *args, PyObject *kwds)
 {
     if (ctype->ct_size < sizeof(ffi_arg)) {
         if ((ctype->ct_flags & (CT_PRIMITIVE_SIGNED | CT_IS_ENUM))
                 == CT_PRIMITIVE_SIGNED) {
+            PY_LONG_LONG value;
             /* It's probably fine to always zero-extend, but you never
                know: maybe some code somewhere expects a negative
                'short' result to be returned into EAX as a 32-bit
                conversion produces stuff that is otherwise ignored. */
             if (convert_from_object(result, ctype, pyobj) < 0)
                 return -1;
-            /* sign-extend the result to a whole 'ffi_arg' (which has the
-               size of a long).  This ensures that we write it in the whole
-               '*result' buffer independently of endianness. */
-            ctype = _get_ct_long();
-            if (ctype == NULL)
+            /* manual inlining and tweaking of convert_from_object()
+               in order to write a whole 'ffi_arg'. */
+            value = _my_PyLong_AsLongLong(pyobj);
+            if (value == -1 && PyErr_Occurred())
                 return -1;
-            assert(ctype->ct_size == sizeof(ffi_arg));
+            write_raw_integer_data(result, value, sizeof(ffi_arg));
+            return 0;
         }
         else if (ctype->ct_flags & (CT_PRIMITIVE_CHAR | CT_PRIMITIVE_SIGNED |
                                     CT_PRIMITIVE_UNSIGNED)) {