From 2fa32b3c17bdc2882f63bdaa970e08625d08378e Mon Sep 17 00:00:00 2001 From: Robert Bradshaw Date: Sun, 11 Aug 2013 01:55:49 -0700 Subject: [PATCH] Share common type object for Cython functions. --- Cython/Utility/CommonTypes.c | 29 +++++++++++++++++++++++++++++ Cython/Utility/CythonFunction.c | 7 +++++++ 2 files changed, 36 insertions(+) create mode 100644 Cython/Utility/CommonTypes.c diff --git a/Cython/Utility/CommonTypes.c b/Cython/Utility/CommonTypes.c new file mode 100644 index 0000000..e6bcca8 --- /dev/null +++ b/Cython/Utility/CommonTypes.c @@ -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; +} diff --git a/Cython/Utility/CythonFunction.c b/Cython/Utility/CythonFunction.c index 4d1b077..683999d 100644 --- a/Cython/Utility/CythonFunction.c +++ b/Cython/Utility/CythonFunction.c @@ -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; } -- 2.7.4