Home | History | Annotate | Download | only in Utility
      1 /////////////// FetchCommonType.proto ///////////////
      2 
      3 static PyTypeObject* __Pyx_FetchCommonType(PyTypeObject* type);
      4 
      5 /////////////// FetchCommonType ///////////////
      6 
      7 static PyTypeObject* __Pyx_FetchCommonType(PyTypeObject* type) {
      8     PyObject* fake_module;
      9     PyTypeObject* cached_type = NULL;
     10 
     11     fake_module = PyImport_AddModule((char*) "_cython_" CYTHON_ABI);
     12     if (!fake_module) return NULL;
     13     Py_INCREF(fake_module);
     14 
     15     cached_type = (PyTypeObject*) PyObject_GetAttrString(fake_module, type->tp_name);
     16     if (cached_type) {
     17         if (!PyType_Check((PyObject*)cached_type)) {
     18             PyErr_Format(PyExc_TypeError,
     19                 "Shared Cython type %.200s is not a type object",
     20                 type->tp_name);
     21             goto bad;
     22         }
     23         if (cached_type->tp_basicsize != type->tp_basicsize) {
     24             PyErr_Format(PyExc_TypeError,
     25                 "Shared Cython type %.200s has the wrong size, try recompiling",
     26                 type->tp_name);
     27             goto bad;
     28         }
     29     } else {
     30         if (!PyErr_ExceptionMatches(PyExc_AttributeError)) goto bad;
     31         PyErr_Clear();
     32         if (PyType_Ready(type) < 0) goto bad;
     33         if (PyObject_SetAttrString(fake_module, type->tp_name, (PyObject*) type) < 0)
     34             goto bad;
     35         Py_INCREF(type);
     36         cached_type = type;
     37     }
     38 
     39 done:
     40     Py_DECREF(fake_module);
     41     // NOTE: always returns owned reference, or NULL on error
     42     return cached_type;
     43 
     44 bad:
     45     Py_XDECREF(cached_type);
     46     cached_type = NULL;
     47     goto done;
     48 }
     49