From fb7d630d0a105e3950c0851081cdd42dcfaaef15 Mon Sep 17 00:00:00 2001 From: Lisandro Dalcin Date: Tue, 3 Feb 2009 09:59:49 +0100 Subject: [PATCH] Avoid the need to load refnanny module using RTLD_GLOBAL --- Cython/Compiler/ModuleNode.py | 39 ++++++++++++++++++------------- Cython/Runtime/refnanny.pyx | 44 ++++++++++++++++++++++++++--------- 2 files changed, 56 insertions(+), 27 deletions(-) diff --git a/Cython/Compiler/ModuleNode.py b/Cython/Compiler/ModuleNode.py index fa297f15..190ad987 100644 --- a/Cython/Compiler/ModuleNode.py +++ b/Cython/Compiler/ModuleNode.py @@ -1585,7 +1585,12 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): code.putln("PyObject* %s;" % Naming.retval_cname) tempdecl_code = code.insertion_point() - code.put_setup_refcount_context(header3) + code.putln("#ifdef CYTHON_REFNANNY") + code.putln("void* __pyx_refchk = NULL;") + code.putln("__Pyx_Refnanny = (__Pyx_RefnannyAPIStruct*) PyCObject_Import((char *)\"refnanny\", (char *)\"RefnannyAPI\");") + code.putln("if (!__Pyx_Refnanny) Py_FatalError(\"failed to import refnanny module\");") + code.putln("__pyx_refchk = __Pyx_Refnanny->NewContext(\"%s\", __LINE__);"% header3) + code.putln("#endif") code.putln("%s = PyTuple_New(0); %s" % (Naming.empty_tuple, code.error_goto_if_null(Naming.empty_tuple, self.pos))); @@ -2323,27 +2328,29 @@ bad: refcount_utility_code = UtilityCode(proto=""" #ifdef CYTHON_REFNANNY -__PYX_EXTERN_C void __Pyx_Refnanny_INCREF(void*, PyObject*, int); -__PYX_EXTERN_C void __Pyx_Refnanny_GOTREF(void*, PyObject*, int); -__PYX_EXTERN_C void __Pyx_Refnanny_GIVEREF(void*, PyObject*, int); -__PYX_EXTERN_C void __Pyx_Refnanny_INCREF(void*, PyObject*, int); -__PYX_EXTERN_C void __Pyx_Refnanny_DECREF(void*, PyObject*, int); -__PYX_EXTERN_C void* __Pyx_Refnanny_NewContext(char*, int); -__PYX_EXTERN_C int __Pyx_Refnanny_FinishContext(void*); -#define __Pyx_INCREF(r) __Pyx_Refnanny_INCREF(__pyx_refchk, r, __LINE__) -#define __Pyx_GOTREF(r) __Pyx_Refnanny_GOTREF(__pyx_refchk, r, __LINE__) -#define __Pyx_GIVEREF(r) __Pyx_Refnanny_GIVEREF(__pyx_refchk, r, __LINE__) -#define __Pyx_DECREF(r) __Pyx_Refnanny_DECREF(__pyx_refchk, r, __LINE__) +typedef struct { + void (*INCREF)(void*, PyObject*, int); + void (*DECREF)(void*, PyObject*, int); + void (*GOTREF)(void*, PyObject*, int); + void (*GIVEREF)(void*, PyObject*, int); + void* (*NewContext)(const char*, int); + int (*FinishContext)(void*); +} __Pyx_RefnannyAPIStruct; +static __Pyx_RefnannyAPIStruct* __Pyx_Refnanny = NULL; +#define __Pyx_INCREF(r) __Pyx_Refnanny->INCREF(__pyx_refchk, (r), __LINE__) +#define __Pyx_DECREF(r) __Pyx_Refnanny->DECREF(__pyx_refchk, (r), __LINE__) #define __Pyx_XDECREF(r) if((r) == NULL) ; else __Pyx_DECREF(r) +#define __Pyx_GOTREF(r) __Pyx_Refnanny->GOTREF(__pyx_refchk, (r), __LINE__) +#define __Pyx_GIVEREF(r) __Pyx_Refnanny->GIVEREF(__pyx_refchk, (r), __LINE__) #define __Pyx_SetupRefcountContext(name) \ - void* __pyx_refchk = __Pyx_Refnanny_NewContext((char*)name, __LINE__) -#define __Pyx_FinishRefcountContext() __Pyx_Refnanny_FinishContext(__pyx_refchk) + void* __pyx_refchk = __Pyx_Refnanny->NewContext((name), __LINE__) +#define __Pyx_FinishRefcountContext() __Pyx_Refnanny->FinishContext(__pyx_refchk) #else #define __Pyx_INCREF(r) Py_INCREF(r) -#define __Pyx_GOTREF(r) -#define __Pyx_GIVEREF(r) #define __Pyx_DECREF(r) Py_DECREF(r) #define __Pyx_XDECREF(r) Py_XDECREF(r) +#define __Pyx_GOTREF(r) +#define __Pyx_GIVEREF(r) #define __Pyx_SetupRefcountContext(name) #define __Pyx_FinishRefcountContext() 0 #endif /* CYTHON_REFNANNY */ diff --git a/Cython/Runtime/refnanny.pyx b/Cython/Runtime/refnanny.pyx index b1d1321f..719cb37e 100644 --- a/Cython/Runtime/refnanny.pyx +++ b/Cython/Runtime/refnanny.pyx @@ -54,7 +54,7 @@ class RefnannyContext(object): # print self.errors raise RefnannyException("\n".join(self.errors)) -cdef public void* __Pyx_Refnanny_NewContext(char* funcname, int lineno) except NULL: +cdef void* Refnanny_NewContext(char* funcname, int lineno) except NULL: if exc.PyErr_Occurred() != NULL: print "error flag set on newcontext?" return NULL @@ -62,8 +62,8 @@ cdef public void* __Pyx_Refnanny_NewContext(char* funcname, int lineno) except N Py_INCREF(ctx) return ctx -cdef public void __Pyx_Refnanny_GOTREF(void* ctx, void* p_obj, int lineno): - cdef exc.PyObject* type, *value, *tb +cdef public void Refnanny_GOTREF(void* ctx, void* p_obj, int lineno): + cdef exc.PyObject* type = NULL, *value = NULL, *tb = NULL if ctx == NULL: return exc.PyErr_Fetch(&type, &value, &tb) try: @@ -78,8 +78,8 @@ cdef public void __Pyx_Refnanny_GOTREF(void* ctx, void* p_obj, int lineno): Py_XDECREF(tb) raise -cdef public void __Pyx_Refnanny_GIVEREF(void* ctx, void* p_obj, int lineno): - cdef exc.PyObject* type, *value, *tb +cdef public void Refnanny_GIVEREF(void* ctx, void* p_obj, int lineno): + cdef exc.PyObject* type = NULL, *value = NULL, *tb = NULL if ctx == NULL: return exc.PyErr_Fetch(&type, &value, &tb) try: @@ -94,18 +94,18 @@ cdef public void __Pyx_Refnanny_GIVEREF(void* ctx, void* p_obj, int lineno): Py_XDECREF(tb) raise -cdef public void __Pyx_Refnanny_INCREF(void* ctx, void* obj, int lineno): +cdef public void Refnanny_INCREF(void* ctx, void* obj, int lineno): if obj is not NULL: Py_INCREF(obj) - __Pyx_Refnanny_GOTREF(ctx, obj, lineno) + Refnanny_GOTREF(ctx, obj, lineno) -cdef public void __Pyx_Refnanny_DECREF(void* ctx, void* obj, int lineno): +cdef public void Refnanny_DECREF(void* ctx, void* obj, int lineno): # GIVEREF raises exception if we hit 0 # - __Pyx_Refnanny_GIVEREF(ctx, obj, lineno) + Refnanny_GIVEREF(ctx, obj, lineno) if obj is not NULL: Py_DECREF(obj) -cdef public int __Pyx_Refnanny_FinishContext(void* ctx) except -1: - cdef exc.PyObject* type, *value, *tb +cdef public int Refnanny_FinishContext(void* ctx) except -1: + cdef exc.PyObject* type = NULL, *value = NULL, *tb = NULL if ctx == NULL: assert False exc.PyErr_Fetch(&type, &value, &tb) @@ -123,3 +123,25 @@ cdef public int __Pyx_Refnanny_FinishContext(void* ctx) except -1: return 0 + + +cdef extern from "Python.h": + object PyCObject_FromVoidPtr(void *, void (*)(void*)) + +ctypedef struct RefnannyAPIStruct: + void (*INCREF)(void*, void*, int) + void (*DECREF)(void*, void*, int) + void (*GOTREF)(void*, void*, int) + void (*GIVEREF)(void*, void*, int) + void* (*NewContext)(char*, int) except NULL + int (*FinishContext)(void*) except -1 + +cdef RefnannyAPIStruct api +api.INCREF = Refnanny_INCREF +api.DECREF = Refnanny_DECREF +api.GOTREF = Refnanny_GOTREF +api.GIVEREF = Refnanny_GIVEREF +api.NewContext = Refnanny_NewContext +api.FinishContext = Refnanny_FinishContext + +RefnannyAPI = PyCObject_FromVoidPtr(&api, NULL) -- 2.26.2