[pypy-commit] cffi cffi-1.0: Windows-friendlification

arigo noreply at buildbot.pypy.org
Thu Apr 30 00:46:36 CEST 2015


Author: Armin Rigo <arigo at tunes.org>
Branch: cffi-1.0
Changeset: r1885:68e7f03a93d1
Date: 2015-04-30 00:47 +0200
http://bitbucket.org/cffi/cffi/changeset/68e7f03a93d1/

Log:	Windows-friendlification

diff --git a/_cffi1/cffi1_module.c b/_cffi1/cffi1_module.c
--- a/_cffi1/cffi1_module.c
+++ b/_cffi1/cffi1_module.c
@@ -102,22 +102,22 @@
                                    const struct _cffi_type_context_s *ctx)
 {
     PyObject *m;
+    FFIObject *ffi;
+    LibObject *lib;
 
 #if PY_MAJOR_VERSION >= 3
     /* note: the module_def leaks, but anyway the C extension module cannot
        be unloaded */
-    struct PyModuleDef *module_def;
-    module_def = PyObject_Malloc(sizeof(struct PyModuleDef));
-    if (module_def == NULL)
-        return PyErr_NoMemory();
-
-    struct PyModuleDef local_module_def = {
+    struct PyModuleDef *module_def, local_module_def = {
         PyModuleDef_HEAD_INIT,
         module_name,
         NULL,
         -1,
         NULL, NULL, NULL, NULL, NULL
     };
+    module_def = PyMem_Malloc(sizeof(struct PyModuleDef));
+    if (module_def == NULL)
+        return PyErr_NoMemory();
     *module_def = local_module_def;
     m = PyModule_Create(module_def);
 #else
@@ -126,12 +126,12 @@
     if (m == NULL)
         return NULL;
 
-    FFIObject *ffi = ffi_internal_new(&FFI_Type, ctx);
+    ffi = ffi_internal_new(&FFI_Type, ctx);
     Py_XINCREF(ffi);    /* make the ffi object really immortal */
     if (ffi == NULL || PyModule_AddObject(m, "ffi", (PyObject *)ffi) < 0)
         return NULL;
 
-    LibObject *lib = lib_internal_new(ffi->types_builder, module_name);
+    lib = lib_internal_new(ffi->types_builder, module_name);
     if (lib == NULL || PyModule_AddObject(m, "lib", (PyObject *)lib) < 0)
         return NULL;
 
diff --git a/_cffi1/ffi_obj.c b/_cffi1/ffi_obj.c
--- a/_cffi1/ffi_obj.c
+++ b/_cffi1/ffi_obj.c
@@ -110,6 +110,9 @@
        Does not return a new reference!
     */
     if ((accept & ACCEPT_STRING) && PyText_Check(arg)) {
+        int index, err;
+        char *input_text;
+        CTypeDescrObject *ct;
         PyObject *types_dict = ffi->types_builder->types_dict;
         PyObject *x = PyDict_GetItem(types_dict, arg);
         if (x != NULL) {
@@ -117,8 +120,8 @@
             return (CTypeDescrObject *)x;
         }
 
-        char *input_text = PyText_AS_UTF8(arg);
-        int index = parse_c_type(&ffi->info, input_text);
+        input_text = PyText_AS_UTF8(arg);
+        index = parse_c_type(&ffi->info, input_text);
         if (index < 0) {
             size_t num_spaces = ffi->info.error_location;
             char spaces[num_spaces + 1];
@@ -128,7 +131,6 @@
                          input_text, spaces);
             return NULL;
         }
-        CTypeDescrObject *ct;
         if (accept & CONSIDER_FN_AS_FNPTR) {
             ct = realize_c_type_fn_as_fnptr(ffi->types_builder,
                                             ffi->info.output, index);
@@ -147,7 +149,7 @@
            sure that in any case the next _ffi_type() with the same
            'arg' will succeed early, in PyDict_GetItem() above.
         */
-        int err = PyDict_SetItem(types_dict, arg, (PyObject *)ct);
+        err = PyDict_SetItem(types_dict, arg, (PyObject *)ct);
         Py_DECREF(ct);   /* we know it was written in types_dict (unless we got
                      out of memory), so there is at least this reference left */
         if (err < 0)
@@ -822,6 +824,8 @@
 _fetch_external_struct_or_union(const struct _cffi_struct_union_s *s,
                                 PyObject *included_ffis, int recursion)
 {
+    Py_ssize_t i;
+
     if (included_ffis == NULL)
         return NULL;
 
@@ -831,7 +835,6 @@
         return NULL;
     }
 
-    Py_ssize_t i;
     for (i = 0; i < PyTuple_GET_SIZE(included_ffis); i++) {
         FFIObject *ffi1;
         const struct _cffi_struct_union_s *s1;
diff --git a/_cffi1/lib_obj.c b/_cffi1/lib_obj.c
--- a/_cffi1/lib_obj.c
+++ b/_cffi1/lib_obj.c
@@ -31,6 +31,9 @@
 static PyObject *_cpyextfunc_type_index(PyObject *x)
 {
     struct CPyExtFunc_s *exf;
+    LibObject *lib;
+    PyObject *tuple, *result;
+
     assert(PyErr_Occurred());
 
     if (!PyCFunction_Check(x))
@@ -44,9 +47,7 @@
 
     PyErr_Clear();
 
-    LibObject *lib = (LibObject *)PyCFunction_GET_SELF(x);
-    PyObject *tuple, *result;
-
+    lib = (LibObject *)PyCFunction_GET_SELF(x);
     tuple = _realize_c_type_or_func(lib->l_types_builder,
                                     lib->l_types_builder->ctx.types,
                                     exf->type_index);
@@ -84,7 +85,8 @@
        by calling _cffi_type().
     */
     CTypeDescrObject *ct;
-    int type_index = _CFFI_GETARG(g->type_op);
+    struct CPyExtFunc_s *xfunc;
+    int i, type_index = _CFFI_GETARG(g->type_op);
     _cffi_opcode_t *opcodes = lib->l_types_builder->ctx.types;
     assert(_CFFI_GETOP(opcodes[type_index]) == _CFFI_OP_FUNCTION);
 
@@ -96,7 +98,7 @@
     Py_DECREF(ct);
 
     /* argument types: */
-    int i = type_index + 1;
+    i = type_index + 1;
     while (_CFFI_GETOP(opcodes[i]) != _CFFI_OP_FUNCTION_END) {
         ct = realize_c_type(lib->l_types_builder, opcodes, i);
         if (ct == NULL)
@@ -110,10 +112,11 @@
        There is one per real C function in a CFFI C extension module.
        CPython never unloads its C extension modules anyway.
     */
-    struct CPyExtFunc_s *xfunc = calloc(1, sizeof(struct CPyExtFunc_s));
+    xfunc = PyMem_Malloc(sizeof(struct CPyExtFunc_s));
     if (xfunc == NULL)
         goto no_memory;
 
+    memset((char *)xfunc, 0, sizeof(struct CPyExtFunc_s));
     xfunc->md.ml_meth = (PyCFunction)g->address;
     xfunc->md.ml_flags = flags;
     xfunc->md.ml_name = g->name;
@@ -135,15 +138,18 @@
 {
     /* does not return a new reference! */
     PyObject *x;
-
+    int index;
+    const struct _cffi_global_s *g;
+    CTypeDescrObject *ct;
     char *s = PyText_AsUTF8(name);
     if (s == NULL)
         return NULL;
 
-    int index = search_in_globals(&lib->l_types_builder->ctx, s, strlen(s));
+    index = search_in_globals(&lib->l_types_builder->ctx, s, strlen(s));
     if (index < 0) {
 
         if (lib->l_includes != NULL) {
+            Py_ssize_t i;
 
             if (recursion > 100) {
                 PyErr_SetString(PyExc_RuntimeError,
@@ -151,7 +157,6 @@
                 return NULL;
             }
 
-            Py_ssize_t i;
             for (i = 0; i < PyTuple_GET_SIZE(lib->l_includes); i++) {
                 LibObject *lib1;
                 lib1 = (LibObject *)PyTuple_GET_ITEM(lib->l_includes, i);
@@ -180,8 +185,7 @@
         return NULL;
     }
 
-    const struct _cffi_global_s *g = &lib->l_types_builder->ctx.globals[index];
-    CTypeDescrObject *ct;
+    g = &lib->l_types_builder->ctx.globals[index];
 
     switch (_CFFI_GETOP(g->type_op)) {
 
@@ -303,13 +307,11 @@
 static PyObject *lib_dir(LibObject *lib, PyObject *noarg)
 {
     const struct _cffi_global_s *g = lib->l_types_builder->ctx.globals;
-    int total = lib->l_types_builder->ctx.num_globals;
-
+    int i, total = lib->l_types_builder->ctx.num_globals;
     PyObject *lst = PyList_New(total);
     if (lst == NULL)
         return NULL;
 
-    int i;
     for (i = 0; i < total; i++) {
         PyObject *s = PyText_FromString(g[i].name);
         if (s == NULL) {
diff --git a/_cffi1/parse_c_type.c b/_cffi1/parse_c_type.c
--- a/_cffi1/parse_c_type.c
+++ b/_cffi1/parse_c_type.c
@@ -225,6 +225,8 @@
        type).  The 'outer' argument is the index of the opcode outside
        this "sequel".
      */
+    int check_for_grouping;
+    _cffi_opcode_t result, *p_current;
 
  header:
     switch (tok->kind) {
@@ -244,14 +246,14 @@
         break;
     }
 
-    int check_for_grouping = 1;
+    check_for_grouping = 1;
     if (tok->kind == TOK_IDENTIFIER) {
         next_token(tok);    /* skip a potential variable name */
         check_for_grouping = 0;
     }
 
-    _cffi_opcode_t result = 0;
-    _cffi_opcode_t *p_current = &result;
+    result = 0;
+    p_current = &result;
 
     while (tok->kind == TOK_OPEN_PAREN) {
         next_token(tok);
@@ -298,13 +300,15 @@
 
             if (tok->kind != TOK_CLOSE_PAREN) {
                 while (1) {
+                    int arg;
+                    _cffi_opcode_t oarg;
+
                     if (tok->kind == TOK_DOTDOTDOT) {
                         has_ellipsis = 1;
                         next_token(tok);
                         break;
                     }
-                    int arg = parse_complete(tok);
-                    _cffi_opcode_t oarg;
+                    arg = parse_complete(tok);
                     switch (_CFFI_GETOP(tok->output[arg])) {
                     case _CFFI_OP_ARRAY:
                     case _CFFI_OP_OPEN_ARRAY:
@@ -543,6 +547,10 @@
 
 static int parse_complete(token_t *tok)
 {
+    unsigned int t0;
+    _cffi_opcode_t t1;
+    int modifiers_length, modifiers_sign;
+
  qualifiers:
     switch (tok->kind) {
     case TOK_CONST:
@@ -557,10 +565,8 @@
         ;
     }
 
-    unsigned int t0;
-    _cffi_opcode_t t1;
-    int modifiers_length = 0;
-    int modifiers_sign = 0;
+    modifiers_length = 0;
+    modifiers_sign = 0;
  modifiers:
     switch (tok->kind) {
 
@@ -682,12 +688,12 @@
         case TOK_STRUCT:
         case TOK_UNION:
         {
-            int kind = tok->kind;
+            int n, kind = tok->kind;
             next_token(tok);
             if (tok->kind != TOK_IDENTIFIER)
                 return parse_error(tok, "struct or union name expected");
 
-            int n = search_in_struct_unions(tok->info->ctx, tok->p, tok->size);
+            n = search_in_struct_unions(tok->info->ctx, tok->p, tok->size);
             if (n < 0)
                 return parse_error(tok, "undefined struct/union name");
             if (((tok->info->ctx->struct_unions[n].flags & _CFFI_F_UNION) != 0)
@@ -699,11 +705,12 @@
         }
         case TOK_ENUM:
         {
+            int n;
             next_token(tok);
             if (tok->kind != TOK_IDENTIFIER)
                 return parse_error(tok, "enum name expected");
 
-            int n = search_in_enums(tok->info->ctx, tok->p, tok->size);
+            n = search_in_enums(tok->info->ctx, tok->p, tok->size);
             if (n < 0)
                 return parse_error(tok, "undefined enum name");
 
diff --git a/_cffi1/realize_c_type.c b/_cffi1/realize_c_type.c
--- a/_cffi1/realize_c_type.c
+++ b/_cffi1/realize_c_type.c
@@ -89,11 +89,12 @@
 
 static builder_c_t *new_builder_c(const struct _cffi_type_context_s *ctx)
 {
+    builder_c_t *builder;
     PyObject *ldict = PyDict_New();
     if (ldict == NULL)
         return NULL;
 
-    builder_c_t *builder = PyMem_Malloc(sizeof(builder_c_t));
+    builder = PyMem_Malloc(sizeof(builder_c_t));
     if (builder == NULL) {
         Py_DECREF(ldict);
         PyErr_NoMemory();
@@ -186,6 +187,7 @@
 
 static PyObject *realize_global_int(const struct _cffi_global_s *g)
 {
+    char got[64];
     unsigned long long value;
     /* note: we cast g->address to this function type; we do the same
        in parse_c_type:parse_sequel() too */
@@ -208,8 +210,6 @@
     default:
         break;
     }
-
-    char got[64];
     if (neg == 2)
         sprintf(got, "%llu (0x%llx)", value, value);
     else
@@ -236,11 +236,12 @@
         return (CTypeDescrObject *)x;
     }
     else {
+        char *text1, *text2;
         PyObject *y;
         assert(PyTuple_Check(x));
         y = PyTuple_GET_ITEM(x, 0);
-        char *text1 = ((CTypeDescrObject *)y)->ct_name;
-        char *text2 = text1 + ((CTypeDescrObject *)y)->ct_name_position + 1;
+        text1 = ((CTypeDescrObject *)y)->ct_name;
+        text2 = text1 + ((CTypeDescrObject *)y)->ct_name_position + 1;
         assert(text2[-3] == '(');
         text2[-3] = '\0';
         PyErr_Format(FFIError, "the type '%s%s' is a function type, not a "
@@ -444,15 +445,15 @@
             Py_INCREF(x);
         }
         else {
-            PyObject *basetd = get_primitive_type(e->type_prim);
-            if (basetd == NULL)
-                return NULL;
-
             PyObject *enumerators = NULL, *enumvalues = NULL, *tmp;
             Py_ssize_t i, j, n = 0;
             const char *p;
             const struct _cffi_global_s *g;
             int gindex;
+            PyObject *args;
+            PyObject *basetd = get_primitive_type(e->type_prim);
+            if (basetd == NULL)
+                return NULL;
 
             if (*e->enumerators != '\0') {
                 n++;
@@ -492,7 +493,7 @@
                 p += j + 1;
             }
 
-            PyObject *args = NULL;
+            args = NULL;
             if (!PyErr_Occurred()) {
                 char *name = alloca(6 + strlen(e->name));
                 _realize_name(name, "enum ", e->name);
@@ -603,30 +604,35 @@
     assert(ct->ct_flags & (CT_STRUCT | CT_UNION));
 
     if (ct->ct_flags & CT_LAZY_FIELD_LIST) {
+        builder_c_t *builder;
+        char *p;
+        int n, i, sflags;
+        const struct _cffi_struct_union_s *s;
+        const struct _cffi_field_s *fld;
+        PyObject *fields, *args, *res;
+
         assert(!(ct->ct_flags & CT_IS_OPAQUE));
 
-        builder_c_t *builder = ct->ct_extra;
+        builder = ct->ct_extra;
         assert(builder != NULL);
 
-        char *p = alloca(2 + strlen(ct->ct_name));
+        p = alloca(2 + strlen(ct->ct_name));
         _unrealize_name(p, ct->ct_name);
 
-        int n = search_in_struct_unions(&builder->ctx, p, strlen(p));
+        n = search_in_struct_unions(&builder->ctx, p, strlen(p));
         if (n < 0)
             Py_FatalError("lost a struct/union!");
 
-        const struct _cffi_struct_union_s *s = &builder->ctx.struct_unions[n];
-        const struct _cffi_field_s *fld =
-            &builder->ctx.fields[s->first_field_index];
+        s = &builder->ctx.struct_unions[n];
+        fld = &builder->ctx.fields[s->first_field_index];
 
         /* XXX painfully build all the Python objects that are the args
            to b_complete_struct_or_union() */
 
-        PyObject *fields = PyList_New(s->num_fields);
+        fields = PyList_New(s->num_fields);
         if (fields == NULL)
             return -1;
 
-        int i;
         for (i = 0; i < s->num_fields; i++, fld++) {
             _cffi_opcode_t op = fld->field_type_op;
             int fbitsize = -1;
@@ -674,24 +680,23 @@
             PyList_SET_ITEM(fields, i, f);
         }
 
-        int sflags = 0;
+        sflags = 0;
         if (s->flags & _CFFI_F_CHECK_FIELDS)
             sflags |= SF_STD_FIELD_POS;
         if (s->flags & _CFFI_F_PACKED)
             sflags |= SF_PACKED;
 
-        PyObject *args = Py_BuildValue("(OOOnni)", ct, fields,
-                                       Py_None,
-                                       (Py_ssize_t)s->size,
-                                       (Py_ssize_t)s->alignment,
-                                       sflags);
+        args = Py_BuildValue("(OOOnni)", ct, fields, Py_None,
+                             (Py_ssize_t)s->size,
+                             (Py_ssize_t)s->alignment,
+                             sflags);
         Py_DECREF(fields);
         if (args == NULL)
             return -1;
 
         ct->ct_extra = NULL;
         ct->ct_flags |= CT_IS_OPAQUE;
-        PyObject *res = b_complete_struct_or_union(NULL, args);
+        res = b_complete_struct_or_union(NULL, args);
         ct->ct_flags &= ~CT_IS_OPAQUE;
         Py_DECREF(args);
 


More information about the pypy-commit mailing list