From d1e5d1f6a851712c751209e111cf45e98a347901 Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Tue, 12 Feb 2013 21:57:58 +0100 Subject: [PATCH] replace lots of dynamically created string constants in utility code by static Python constants --- Cython/Utility/Builtins.c | 6 +++--- Cython/Utility/CythonFunction.c | 2 +- Cython/Utility/Generator.c | 6 +++--- Cython/Utility/ObjectHandling.c | 14 +++++++------- Cython/Utility/Optimize.c | 24 ++++++++++++++++++++---- Cython/Utility/Printing.c | 8 ++++---- Cython/Utility/TypeConversion.c | 2 +- 7 files changed, 39 insertions(+), 23 deletions(-) diff --git a/Cython/Utility/Builtins.c b/Cython/Utility/Builtins.c index b10f996..c8ec2f8 100644 --- a/Cython/Utility/Builtins.c +++ b/Cython/Utility/Builtins.c @@ -15,7 +15,7 @@ static PyObject* __Pyx_Globals(void) { Py_ssize_t i; //PyObject *d; PyObject *names = NULL; - PyObject *globals = PyObject_GetAttrString($module_cname, "__dict__"); + PyObject *globals = PyObject_GetAttr($module_cname, PYIDENT("__dict__")); if (!globals) { PyErr_SetString(PyExc_TypeError, "current module must have __dict__ attribute"); @@ -111,8 +111,8 @@ static PyObject* __Pyx_PyExec3(PyObject* o, PyObject* globals, PyObject* locals) locals = globals; } - if (PyDict_GetItemString(globals, "__builtins__") == NULL) { - if (PyDict_SetItemString(globals, "__builtins__", PyEval_GetBuiltins()) < 0) + if (PyDict_GetItem(globals, PYIDENT("__builtins__")) == NULL) { + if (PyDict_SetItem(globals, PYIDENT("__builtins__"), PyEval_GetBuiltins()) < 0) goto bad; } diff --git a/Cython/Utility/CythonFunction.c b/Cython/Utility/CythonFunction.c index 43fa613..81d0c38 100644 --- a/Cython/Utility/CythonFunction.c +++ b/Cython/Utility/CythonFunction.c @@ -701,7 +701,7 @@ static PyObject * _obj_to_str(PyObject *obj) { if (PyType_Check(obj)) - return PyObject_GetAttrString(obj, "__name__"); + return PyObject_GetAttr(obj, PYIDENT("__name__")); else return PyObject_Str(obj); } diff --git a/Cython/Utility/Generator.c b/Cython/Utility/Generator.c index 87f272f..2a66e98 100644 --- a/Cython/Utility/Generator.c +++ b/Cython/Utility/Generator.c @@ -123,7 +123,7 @@ static int __Pyx_PyGen_FetchStopIterationValue(PyObject **pvalue) { Py_DECREF(ev); #else { - PyObject* args = PyObject_GetAttrString(ev, "args"); + PyObject* args = PyObject_GetAttr(ev, PYIDENT("args")); Py_DECREF(ev); if (likely(args)) { value = PyObject_GetItem(args, 0); @@ -310,7 +310,7 @@ static int __Pyx_Generator_CloseIter(__pyx_GeneratorObject *gen, PyObject *yf) { } else { PyObject *meth; gen->is_running = 1; - meth = PyObject_GetAttrString(yf, "close"); + meth = PyObject_GetAttr(yf, PYIDENT("close")); if (unlikely(!meth)) { if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { PyErr_WriteUnraisable(yf); @@ -402,7 +402,7 @@ static PyObject *__Pyx_Generator_Throw(PyObject *self, PyObject *args) { if (__Pyx_Generator_CheckExact(yf)) { ret = __Pyx_Generator_Throw(yf, args); } else { - PyObject *meth = PyObject_GetAttrString(yf, "throw"); + PyObject *meth = PyObject_GetAttr(yf, PYIDENT("throw")); if (unlikely(!meth)) { Py_DECREF(yf); if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { diff --git a/Cython/Utility/ObjectHandling.c b/Cython/Utility/ObjectHandling.c index 2da2148..7a78e51 100644 --- a/Cython/Utility/ObjectHandling.c +++ b/Cython/Utility/ObjectHandling.c @@ -400,7 +400,7 @@ static PyObject *__Pyx_FindPy2Metaclass(PyObject *bases) { #if PY_MAJOR_VERSION < 3 if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) { PyObject *base = PyTuple_GET_ITEM(bases, 0); - metaclass = PyObject_GetAttrString(base, (char *)"__class__"); + metaclass = PyObject_GetAttr(base, PYIDENT("__class__")); if (!metaclass) { PyErr_Clear(); metaclass = (PyObject*) Py_TYPE(base); @@ -428,10 +428,10 @@ static PyObject *__Pyx_Py3MetaclassGet(PyObject *bases, PyObject *mkw); /*proto* //@requires: FindPy2Metaclass static PyObject *__Pyx_Py3MetaclassGet(PyObject *bases, PyObject *mkw) { - PyObject *metaclass = PyDict_GetItemString(mkw, "metaclass"); + PyObject *metaclass = PyDict_GetItem(mkw, PYIDENT("metaclass")); if (metaclass) { Py_INCREF(metaclass); - if (PyDict_DelItemString(mkw, "metaclass") < 0) { + if (PyDict_DelItem(mkw, PYIDENT("metaclass")) < 0) { Py_DECREF(metaclass); return NULL; } @@ -453,13 +453,13 @@ static PyObject *__Pyx_CreateClass(PyObject *bases, PyObject *dict, PyObject *na PyObject *result; PyObject *metaclass; - if (PyDict_SetItemString(dict, "__module__", modname) < 0) + if (PyDict_SetItem(dict, PYIDENT("__module__"), modname) < 0) return NULL; - if (PyDict_SetItemString(dict, "__qualname__", qualname) < 0) + if (PyDict_SetItem(dict, PYIDENT("__qualname__"), qualname) < 0) return NULL; /* Python2 __metaclass__ */ - metaclass = PyDict_GetItemString(dict, "__metaclass__"); + metaclass = PyDict_GetItem(dict, PYIDENT("__metaclass__")); if (metaclass) { Py_INCREF(metaclass); } else { @@ -484,7 +484,7 @@ static PyObject *__Pyx_Py3MetaclassPrepare(PyObject *metaclass, PyObject *bases, PyObject *ns; PyObject *str; - prep = PyObject_GetAttrString(metaclass, (char *)"__prepare__"); + prep = PyObject_GetAttr(metaclass, PYIDENT("__prepare__")); if (!prep) { if (!PyErr_ExceptionMatches(PyExc_AttributeError)) return NULL; diff --git a/Cython/Utility/Optimize.c b/Cython/Utility/Optimize.c index c1dbb26..5c26fe5 100644 --- a/Cython/Utility/Optimize.c +++ b/Cython/Utility/Optimize.c @@ -39,6 +39,10 @@ static CYTHON_INLINE int __Pyx_PyList_Append(PyObject* list, PyObject* x) { /////////////// pop.proto /////////////// +static CYTHON_INLINE PyObject* __Pyx_PyObject_Pop(PyObject* L); /*proto*/ + +/////////////// pop /////////////// + static CYTHON_INLINE PyObject* __Pyx_PyObject_Pop(PyObject* L) { #if CYTHON_COMPILING_IN_CPYTHON && PY_VERSION_HEX >= 0x02040000 if (likely(PyList_CheckExact(L)) @@ -53,12 +57,16 @@ static CYTHON_INLINE PyObject* __Pyx_PyObject_Pop(PyObject* L) { } #endif #endif - return PyObject_CallMethod(L, (char*)"pop", NULL); + return PyObject_CallMethodObjArgs(L, PYIDENT("pop"), NULL); } /////////////// pop_index.proto /////////////// +static PyObject* __Pyx_PyObject_PopIndex(PyObject* L, Py_ssize_t ix); /*proto*/ + +/////////////// pop_index /////////////// + static PyObject* __Pyx_PyObject_PopIndex(PyObject* L, Py_ssize_t ix) { PyObject *r, *m, *t, *py_ix; #if CYTHON_COMPILING_IN_CPYTHON && PY_VERSION_HEX >= 0x02040000 @@ -79,7 +87,7 @@ static PyObject* __Pyx_PyObject_PopIndex(PyObject* L, Py_ssize_t ix) { } #endif py_ix = t = NULL; - m = __Pyx_GetAttrString(L, "pop"); + m = PyObject_GetAttr(L, PYIDENT("pop")); if (!m) goto bad; py_ix = PyInt_FromSsize_t(ix); if (!py_ix) goto bad; @@ -258,6 +266,10 @@ static CYTHON_INLINE char __Pyx_PyBytes_GetItemInt(PyObject* bytes, Py_ssize_t i /////////////// dict_getitem_default.proto /////////////// +static PyObject* __Pyx_PyDict_GetItemDefault(PyObject* d, PyObject* key, PyObject* default_value); /*proto*/ + +/////////////// dict_getitem_default /////////////// + static PyObject* __Pyx_PyDict_GetItemDefault(PyObject* d, PyObject* key, PyObject* default_value) { PyObject* value; #if PY_MAJOR_VERSION >= 3 @@ -278,7 +290,7 @@ static PyObject* __Pyx_PyDict_GetItemDefault(PyObject* d, PyObject* key, PyObjec Py_INCREF(value); } else { PyObject *m; - m = __Pyx_GetAttrString(d, "get"); + m = PyObject_GetAttr(d, PYIDENT("get")); if (!m) return NULL; value = PyObject_CallFunctionObjArgs(m, key, (default_value == Py_None) ? NULL : default_value, NULL); @@ -291,6 +303,10 @@ static PyObject* __Pyx_PyDict_GetItemDefault(PyObject* d, PyObject* key, PyObjec /////////////// dict_setdefault.proto /////////////// +static PyObject *__Pyx_PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *default_value); /*proto*/ + +/////////////// dict_setdefault /////////////// + static PyObject *__Pyx_PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *default_value) { PyObject* value; #if PY_MAJOR_VERSION >= 3 @@ -315,7 +331,7 @@ static PyObject *__Pyx_PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *d Py_INCREF(value); } else { PyObject *m; - m = __Pyx_GetAttrString(d, "setdefault"); + m = PyObject_GetAttr(d, PYIDENT("setdefault")); if (!m) return NULL; value = PyObject_CallFunctionObjArgs(m, key, default_value, NULL); Py_DECREF(m); diff --git a/Cython/Utility/Printing.c b/Cython/Utility/Printing.c index 0507826..3f58bd6 100644 --- a/Cython/Utility/Printing.c +++ b/Cython/Utility/Printing.c @@ -72,7 +72,7 @@ static int __Pyx_Print(PyObject* stream, PyObject *arg_tuple, int newline) { PyObject* result = 0; PyObject* end_string; if (unlikely(!$print_function)) { - $print_function = __Pyx_GetAttrString($builtins_cname, "print"); + $print_function = PyObject_GetAttr($builtins_cname, PYIDENT("print")); if (!$print_function) return -1; } @@ -80,13 +80,13 @@ static int __Pyx_Print(PyObject* stream, PyObject *arg_tuple, int newline) { kwargs = PyDict_New(); if (unlikely(!kwargs)) return -1; - if (unlikely(PyDict_SetItemString(kwargs, "file", stream) < 0)) + if (unlikely(PyDict_SetItem(kwargs, PYIDENT("file"), stream) < 0)) goto bad; if (!newline) { end_string = PyUnicode_FromStringAndSize(" ", 1); if (unlikely(!end_string)) goto bad; - if (PyDict_SetItemString(kwargs, "end", end_string) < 0) { + if (PyDict_SetItem(kwargs, PYIDENT("end"), end_string) < 0) { Py_DECREF(end_string); goto bad; } @@ -100,7 +100,7 @@ static int __Pyx_Print(PyObject* stream, PyObject *arg_tuple, int newline) { end_string = PyUnicode_FromStringAndSize(" ", 1); if (unlikely(!end_string)) return -1; - if (PyDict_SetItemString($print_function_kwargs, "end", end_string) < 0) { + if (PyDict_SetItem($print_function_kwargs, PYIDENT("end"), end_string) < 0) { Py_DECREF(end_string); return -1; } diff --git a/Cython/Utility/TypeConversion.c b/Cython/Utility/TypeConversion.c index d392412..74d6600 100644 --- a/Cython/Utility/TypeConversion.c +++ b/Cython/Utility/TypeConversion.c @@ -130,7 +130,7 @@ static {{struct_type_decl}} {{funcname}}(PyObject * o) { {{for member in var_entries:}} {{py:attr = "result." + member.cname}} - value = PyMapping_GetItemString(o, (char *) "{{member.name}}"); + value = PyObject_GetItem(o, PYIDENT("{{member.name}}")); if (!value) { PyErr_SetString(PyExc_ValueError, "No value specified for struct " "attribute '{{member.name}}'"); -- 2.7.4