From e5b1dccbd866d27fcd8d5ad3ae03beb08df5c2f1 Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Thu, 7 Mar 2013 00:30:37 +0100 Subject: [PATCH] factor out builtin access code --- Cython/Compiler/Code.py | 7 +++---- Cython/Compiler/ExprNodes.py | 10 +++------- Cython/Utility/ObjectHandling.c | 37 +++++++++++-------------------------- 3 files changed, 17 insertions(+), 37 deletions(-) diff --git a/Cython/Compiler/Code.py b/Cython/Compiler/Code.py index 6cb1edd..4019227 100644 --- a/Cython/Compiler/Code.py +++ b/Cython/Compiler/Code.py @@ -1107,11 +1107,10 @@ class GlobalState(object): def put_cached_builtin_init(self, pos, name, cname): w = self.parts['cached_builtins'] interned_cname = self.get_interned_identifier(name).cname - from ExprNodes import get_name_interned_utility_code - self.use_utility_code(get_name_interned_utility_code) - w.putln('%s = __Pyx_GetName(%s, %s); if (!%s) %s' % ( + self.use_utility_code( + UtilityCode.load_cached("GetBuiltinName", "ObjectHandling.c")) + w.putln('%s = __Pyx_GetBuiltinName(%s); if (!%s) %s' % ( cname, - Naming.builtins_cname, interned_cname, cname, w.error_goto(pos))) diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index 029841a..4410820 100755 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -1767,11 +1767,11 @@ class NameNode(AtomicExprNode): elif entry.is_builtin: assert entry.type.is_pyobject, "Python global or builtin not a Python object" interned_cname = code.intern_identifier(self.entry.name) - code.globalstate.use_utility_code(get_name_interned_utility_code) + code.globalstate.use_utility_code( + UtilityCode.load_cached("GetBuiltinName", "ObjectHandling.c")) code.putln( - '%s = __Pyx_GetName(%s, %s); %s' % ( + '%s = __Pyx_GetBuiltinName(%s); %s' % ( self.result(), - Naming.builtins_cname, interned_cname, code.error_goto_if_null(self.result(), self.pos))) code.put_gotref(self.py_result()) @@ -10425,10 +10425,6 @@ class DocstringRefNode(ExprNode): # #------------------------------------------------------------------------------------ -get_name_interned_utility_code = UtilityCode.load_cached("GetGlobalName", "ObjectHandling.c") - -#------------------------------------------------------------------------------------ - pyerr_occurred_withgil_utility_code= UtilityCode( proto = """ static CYTHON_INLINE int __Pyx_ErrOccurredWithGIL(void); /* proto */ diff --git a/Cython/Utility/ObjectHandling.c b/Cython/Utility/ObjectHandling.c index a5dc3a3..56b8388 100644 --- a/Cython/Utility/ObjectHandling.c +++ b/Cython/Utility/ObjectHandling.c @@ -578,30 +578,23 @@ static CYTHON_INLINE PyObject* __Pyx_PyBoolOrNull_FromLong(long b) { return unlikely(b < 0) ? NULL : __Pyx_PyBool_FromLong(b); } -/////////////// GetGlobalName.proto /////////////// +/////////////// GetBuiltinName.proto /////////////// -static PyObject *__Pyx_GetName(PyObject *dict, PyObject *name); /*proto*/ +static PyObject *__Pyx_GetBuiltinName(PyObject *name); /*proto*/ -/////////////// GetGlobalName /////////////// +/////////////// GetBuiltinName /////////////// //@requires: PyObjectGetAttrStr //@substitute: naming -static PyObject *__Pyx_GetName(PyObject *dict, PyObject *name) { - PyObject *result; - result = __Pyx_PyObject_GetAttrStr(dict, name); - if (!result) { - if (dict != $builtins_cname) { - PyErr_Clear(); - result = __Pyx_PyObject_GetAttrStr($builtins_cname, name); - } - if (!result) { - PyErr_Format(PyExc_NameError, +static PyObject *__Pyx_GetBuiltinName(PyObject *name) { + PyObject* result = __Pyx_PyObject_GetAttrStr($builtins_cname, name); + if (unlikely(!result)) { + PyErr_Format(PyExc_NameError, #if PY_MAJOR_VERSION >= 3 - "name '%U' is not defined", name); + "name '%U' is not defined", name); #else - "name '%s' is not defined", PyString_AS_STRING(name)); + "name '%s' is not defined", PyString_AS_STRING(name)); #endif - } } return result; } @@ -611,7 +604,7 @@ static PyObject *__Pyx_GetName(PyObject *dict, PyObject *name) { static CYTHON_INLINE PyObject *__Pyx_GetModuleGlobalName(PyObject *name); /*proto*/ /////////////// GetModuleGlobalName /////////////// -//@requires: PyObjectGetAttrStr +//@requires: GetBuiltinName //@substitute: naming static CYTHON_INLINE PyObject *__Pyx_GetModuleGlobalName(PyObject *name) { @@ -626,15 +619,7 @@ static CYTHON_INLINE PyObject *__Pyx_GetModuleGlobalName(PyObject *name) { if (!result) { PyErr_Clear(); #endif - result = __Pyx_PyObject_GetAttrStr($builtins_cname, name); - if (unlikely(!result)) { - PyErr_Format(PyExc_NameError, -#if PY_MAJOR_VERSION >= 3 - "name '%U' is not defined", name); -#else - "name '%s' is not defined", PyString_AS_STRING(name)); -#endif - } + result = __Pyx_GetBuiltinName(name); } return result; } -- 2.7.4