Share common type object for Cython functions.
authorRobert Bradshaw <robertwb@gmail.com>
Sun, 11 Aug 2013 08:55:49 +0000 (01:55 -0700)
committerRobert Bradshaw <robertwb@gmail.com>
Sun, 11 Aug 2013 08:55:49 +0000 (01:55 -0700)
Cython/Utility/CommonTypes.c [new file with mode: 0644]
Cython/Utility/CythonFunction.c

diff --git a/Cython/Utility/CommonTypes.c b/Cython/Utility/CommonTypes.c
new file mode 100644 (file)
index 0000000..e6bcca8
--- /dev/null
@@ -0,0 +1,29 @@
+/////////////// FetchCommonType.proto ///////////////
+
+static PyTypeObject* __Pyx_FetchCommonType(PyTypeObject* type);
+
+/////////////// FetchCommonType ///////////////
+
+static PyTypeObject* __Pyx_FetchCommonType(PyTypeObject* type) {
+    static PyObject* fake_module = NULL;
+    PyTypeObject* cached_type;
+    if (fake_module == NULL) {
+        PyObject* sys = PyImport_ImportModule("sys");
+        PyObject* sys_modules = PyObject_GetAttrString(sys, "modules");
+        fake_module = PyDict_GetItemString(sys_modules, "_cython");
+        if (fake_module == NULL) {
+            PyObject* args = PyTuple_New(1);
+            PyTuple_SET_ITEM(args, 0, __Pyx_PyStr_FromString("_cython"));
+            fake_module = PyObject_Call((PyObject*) &PyModule_Type, args, NULL);
+            PyDict_SetItemString(sys_modules, "_cython", fake_module);
+        }
+    }
+    if (PyObject_HasAttrString(fake_module, type->tp_name)) {
+        cached_type = (PyTypeObject*) PyObject_GetAttrString(fake_module, type->tp_name);
+    } else {
+        PyType_Ready(type);
+        PyObject_SetAttrString(fake_module, type->tp_name, (PyObject*) type);
+        cached_type = type;
+    }
+    return cached_type;
+}
index 4d1b077..683999d 100644 (file)
@@ -67,6 +67,7 @@ static int __Pyx_CyFunction_init(void);
 
 //////////////////// CythonFunction ////////////////////
 //@substitute: naming
+//@requires: CommonTypes.c::FetchCommonType
 
 static PyObject *
 __Pyx_CyFunction_get_doc(__pyx_CyFunctionObject *op, CYTHON_UNUSED void *closure)
@@ -645,9 +646,15 @@ static int __Pyx_CyFunction_init(void) {
     // avoid a useless level of call indirection
     __pyx_CyFunctionType_type.tp_call = PyCFunction_Call;
 #endif
+#if 1
+    __pyx_CyFunctionType = __Pyx_FetchCommonType(&__pyx_CyFunctionType_type);
+    if (__pyx_CyFunctionType == NULL)
+        return -1;
+#else
     if (PyType_Ready(&__pyx_CyFunctionType_type) < 0)
         return -1;
     __pyx_CyFunctionType = &__pyx_CyFunctionType_type;
+#endif
     return 0;
 }