From e7d1eb153ed2e6bd2b4f80a0d67c479a38816320 Mon Sep 17 00:00:00 2001 From: Lisandro Dalcin Date: Mon, 22 Dec 2008 20:09:20 -0200 Subject: [PATCH] make GCC happy when string literals and const char* pointers are passed to many C-API calls or set in struct slots for Python 2.3/2.4 --- Cython/Compiler/Builtin.py | 16 +++++----- Cython/Compiler/Code.py | 2 +- Cython/Compiler/ExprNodes.py | 6 ++-- Cython/Compiler/ModuleNode.py | 57 ++++++++++++++++++++++++++--------- Cython/Compiler/Nodes.py | 4 +-- Cython/Compiler/TypeSlots.py | 2 +- 6 files changed, 58 insertions(+), 29 deletions(-) diff --git a/Cython/Compiler/Builtin.py b/Cython/Compiler/Builtin.py index 759e19f6..764f79d1 100644 --- a/Cython/Compiler/Builtin.py +++ b/Cython/Compiler/Builtin.py @@ -235,22 +235,22 @@ proto = """ PySequence_Contains((anyset), (key)) #define PySet_Pop(set) \\ - PyObject_CallMethod(set, "pop", NULL) + PyObject_CallMethod(set, (char *)"pop", NULL) static INLINE int PySet_Clear(PyObject *set) { - PyObject *ret = PyObject_CallMethod(set, "clear", NULL); + PyObject *ret = PyObject_CallMethod(set, (char *)"clear", NULL); if (!ret) return -1; Py_DECREF(ret); return 0; } static INLINE int PySet_Discard(PyObject *set, PyObject *key) { - PyObject *ret = PyObject_CallMethod(set, "discard", "O", key); + PyObject *ret = PyObject_CallMethod(set, (char *)"discard", (char *)"O", key); if (!ret) return -1; Py_DECREF(ret); return 0; } static INLINE int PySet_Add(PyObject *set, PyObject *key) { - PyObject *ret = PyObject_CallMethod(set, "add", "O", key); + PyObject *ret = PyObject_CallMethod(set, (char *)"add", (char *)"O", key); if (!ret) return -1; Py_DECREF(ret); return 0; } @@ -277,14 +277,14 @@ static PyTypeObject *__Pyx_PyFrozenSet_Type = NULL; static int __Pyx_Py23SetsImport(void) { PyObject *sets=0, *Set=0, *ImmutableSet=0; - sets = PyImport_ImportModule("sets"); + sets = PyImport_ImportModule((char *)"sets"); if (!sets) goto bad; - Set = PyObject_GetAttrString(sets, "Set"); + Set = PyObject_GetAttrString(sets, (char *)"Set"); if (!Set) goto bad; - ImmutableSet = PyObject_GetAttrString(sets, "ImmutableSet"); + ImmutableSet = PyObject_GetAttrString(sets, (char *)"ImmutableSet"); if (!ImmutableSet) goto bad; Py_DECREF(sets); - + __Pyx_PySet_Type = (PyTypeObject*) Set; __Pyx_PyFrozenSet_Type = (PyTypeObject*) ImmutableSet; diff --git a/Cython/Compiler/Code.py b/Cython/Compiler/Code.py index 0204e995..ea918f0c 100644 --- a/Cython/Compiler/Code.py +++ b/Cython/Compiler/Code.py @@ -805,7 +805,7 @@ class CCodeWriter(object): if entry.is_special: method_flags += [method_coexist] self.putln( - '{"%s", (PyCFunction)%s, %s, %s}%s' % ( + '{__Pyx_NAMESTR("%s"), (PyCFunction)%s, %s, __Pyx_DOCSTR(%s)}%s' % ( entry.name, entry.func_cname, "|".join(method_flags), diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index febfaeed..9f759ff8 100644 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -1369,7 +1369,7 @@ class NameNode(AtomicExprNode): error(self.pos, "Deletion of local or C global name not supported") return code.put_error_if_neg(self.pos, - 'PyObject_DelAttrString(%s, "%s")' % ( + '__Pyx_DelAttrString(%s, "%s")' % ( Naming.module_cname, self.entry.name)) @@ -5106,7 +5106,7 @@ static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list) { PyObject *global_dict = 0; PyObject *empty_dict = 0; PyObject *list; - __import__ = PyObject_GetAttrString(%(BUILTINS)s, "__import__"); + __import__ = __Pyx_GetAttrString(%(BUILTINS)s, "__import__"); if (!__import__) goto bad; if (from_list) @@ -5309,7 +5309,7 @@ static INLINE PyObject* __Pyx_PyObject_Append(PyObject* L, PyObject* x) { } else { PyObject *r, *m; - m = PyObject_GetAttrString(L, "append"); + m = __Pyx_GetAttrString(L, "append"); if (!m) return NULL; r = PyObject_CallFunctionObjArgs(m, x, NULL); Py_DECREF(m); diff --git a/Cython/Compiler/ModuleNode.py b/Cython/Compiler/ModuleNode.py index ba0bce5e..d4b40092 100644 --- a/Cython/Compiler/ModuleNode.py +++ b/Cython/Compiler/ModuleNode.py @@ -508,7 +508,25 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): code.putln("#else") code.putln(" #define _USE_MATH_DEFINES") code.putln("#endif") - + + code.putln("#if PY_VERSION_HEX < 0x02050000") + code.putln(" #define __Pyx_GetAttrString(o,n) PyObject_GetAttrString((o),((char *)(n)))") + code.putln(" #define __Pyx_SetAttrString(o,n,a) PyObject_SetAttrString((o),((char *)(n)),(a))") + code.putln(" #define __Pyx_DelAttrString(o,n) PyObject_DelAttrString((o),((char *)(n)))") + code.putln("#else") + code.putln(" #define __Pyx_GetAttrString(o,n) PyObject_GetAttrString((o),(n))") + code.putln(" #define __Pyx_SetAttrString(o,n,a) PyObject_SetAttrString((o),(n),(a))") + code.putln(" #define __Pyx_DelAttrString(o,n) PyObject_DelAttrString((o),(n))") + code.putln("#endif") + + code.putln("#if PY_VERSION_HEX < 0x02050000") + code.putln(" #define __Pyx_NAMESTR(n) ((char *)(n))") + code.putln(" #define __Pyx_DOCSTR(n) ((char *)(n))") + code.putln("#else") + code.putln(" #define __Pyx_NAMESTR(n) (n)") + code.putln(" #define __Pyx_DOCSTR(n) (n)") + code.putln("#endif") + self.generate_extern_c_macro_definition(code) code.putln("#include ") code.putln("#define %s" % Naming.api_guard_prefix + self.api_name(env)) @@ -1410,7 +1428,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): code.putln( "PyVarObject_HEAD_INIT(0, 0)") code.putln( - '"%s.%s", /*tp_name*/' % ( + '__Pyx_NAMESTR("%s.%s"), /*tp_name*/' % ( self.full_module_name, scope.class_name)) if type.typedef_flag: objstruct = type.objstruct_cname @@ -1702,7 +1720,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): doc = "0" code.putln("#if PY_MAJOR_VERSION < 3") code.putln( - '%s = Py_InitModule4("%s", %s, %s, 0, PYTHON_API_VERSION);' % ( + '%s = Py_InitModule4(__Pyx_NAMESTR("%s"), %s, %s, 0, PYTHON_API_VERSION);' % ( env.module_cname, env.module_name, env.method_table_cname, @@ -1723,20 +1741,20 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): env.module_cname) code.putln("#endif") code.putln( - '%s = PyImport_AddModule(__Pyx_BUILTIN_MODULE_NAME);' % + '%s = PyImport_AddModule(__Pyx_NAMESTR(__Pyx_BUILTIN_MODULE_NAME));' % Naming.builtins_cname) code.putln( "if (!%s) %s;" % ( Naming.builtins_cname, code.error_goto(self.pos))); code.putln( - 'if (PyObject_SetAttrString(%s, "__builtins__", %s) < 0) %s;' % ( + 'if (__Pyx_SetAttrString(%s, "__builtins__", %s) < 0) %s;' % ( env.module_cname, Naming.builtins_cname, code.error_goto(self.pos))) if Options.pre_import is not None: code.putln( - '%s = PyImport_AddModule("%s");' % ( + '%s = PyImport_AddModule(__Pyx_NAMESTR("%s"));' % ( Naming.preimport_cname, Options.pre_import)) code.putln( @@ -1893,7 +1911,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): code.error_goto(entry.pos))) env.use_utility_code(Nodes.set_vtable_utility_code) code.putln( - 'if (PyObject_SetAttrString(%s, "%s", (PyObject *)&%s) < 0) %s' % ( + 'if (__Pyx_SetAttrString(%s, "%s", (PyObject *)&%s) < 0) %s' % ( Naming.module_cname, scope.class_name, typeobj_cname, @@ -2063,16 +2081,22 @@ static int __Pyx_ExportFunction(const char *name, void *f, const char *sig); /*p """, impl = r""" static int __Pyx_ExportFunction(const char *name, void *f, const char *sig) { +#if PY_VERSION_HEX < 0x02050000 + char *api = (char *)"%(API)s"; +#else + const char *api = "%(API)s"; +#endif PyObject *d = 0; PyObject *p = 0; - d = PyObject_GetAttrString(%(MODULE)s, "%(API)s"); + + d = PyObject_GetAttrString(%(MODULE)s, api); if (!d) { PyErr_Clear(); d = PyDict_New(); if (!d) goto bad; Py_INCREF(d); - if (PyModule_AddObject(%(MODULE)s, "%(API)s", d) < 0) + if (PyModule_AddObject(%(MODULE)s, api, d) < 0) goto bad; } p = PyCObject_FromVoidPtrAndDesc(f, (void *)sig, 0); @@ -2100,11 +2124,16 @@ impl = """ #ifndef __PYX_HAVE_RT_ImportFunction #define __PYX_HAVE_RT_ImportFunction static int __Pyx_ImportFunction(PyObject *module, const char *funcname, void **f, const char *sig) { +#if PY_VERSION_HEX < 0x02050000 + char *api = (char *)"%(API)s"; +#else + const char *api = "%(API)s"; +#endif PyObject *d = 0; PyObject *cobj = 0; char *desc; - d = PyObject_GetAttrString(module, "%(API)s"); + d = PyObject_GetAttrString(module, api); if (!d) goto bad; cobj = PyDict_GetItemString(d, funcname); @@ -2138,7 +2167,7 @@ register_cleanup_utility_code = UtilityCode( proto = """ static int __Pyx_RegisterCleanup(void); /*proto*/ static PyObject* __pyx_module_cleanup(PyObject *self, PyObject *unused); /*proto*/ -static PyMethodDef cleanup_def = {"__cleanup", (PyCFunction)&__pyx_module_cleanup, METH_NOARGS, 0}; +static PyMethodDef cleanup_def = {__Pyx_NAMESTR("__cleanup"), (PyCFunction)&__pyx_module_cleanup, METH_NOARGS, 0}; """, impl = """ static int __Pyx_RegisterCleanup(void) { @@ -2163,7 +2192,7 @@ static int __Pyx_RegisterCleanup(void) { atexit = __Pyx_ImportModule("atexit"); if (!atexit) goto bad; - reg = PyObject_GetAttrString(atexit, "register"); + reg = __Pyx_GetAttrString(atexit, "register"); if (!reg) goto bad; res = PyObject_CallObject(reg, args); @@ -2187,7 +2216,7 @@ import_star_utility_code = """ static int __Pyx_import_all_from(PyObject *locals, PyObject *v) { - PyObject *all = PyObject_GetAttrString(v, "__all__"); + PyObject *all = __Pyx_GetAttrString(v, "__all__"); PyObject *dict, *name, *value; int skip_leading_underscores = 0; int pos, err; @@ -2196,7 +2225,7 @@ __Pyx_import_all_from(PyObject *locals, PyObject *v) if (!PyErr_ExceptionMatches(PyExc_AttributeError)) return -1; /* Unexpected error */ PyErr_Clear(); - dict = PyObject_GetAttrString(v, "__dict__"); + dict = __Pyx_GetAttrString(v, "__dict__"); if (dict == NULL) { if (!PyErr_ExceptionMatches(PyExc_AttributeError)) return -1; diff --git a/Cython/Compiler/Nodes.py b/Cython/Compiler/Nodes.py index 37511511..8cb8587e 100644 --- a/Cython/Compiler/Nodes.py +++ b/Cython/Compiler/Nodes.py @@ -861,7 +861,7 @@ class CEnumDefNode(StatNode): self.temp, item.cname, code.error_goto_if_null(self.temp, item.pos))) - code.putln('if (PyObject_SetAttrString(%s, "%s", %s) < 0) %s' % ( + code.putln('if (__Pyx_SetAttrString(%s, "%s", %s) < 0) %s' % ( Naming.module_cname, item.name, self.temp, @@ -4641,7 +4641,7 @@ static int __Pyx_Print(PyObject *arg_tuple, int newline) { PyObject* result = 0; PyObject* end_string; if (!%(PRINT_FUNCTION)s) { - %(PRINT_FUNCTION)s = PyObject_GetAttrString(%(BUILTINS)s, "print"); + %(PRINT_FUNCTION)s = __Pyx_GetAttrString(%(BUILTINS)s, "print"); if (!%(PRINT_FUNCTION)s) return -1; } diff --git a/Cython/Compiler/TypeSlots.py b/Cython/Compiler/TypeSlots.py index d62f1cc2..b33ae65d 100644 --- a/Cython/Compiler/TypeSlots.py +++ b/Cython/Compiler/TypeSlots.py @@ -314,7 +314,7 @@ class DocStringSlot(SlotDescriptor): doc = scope.doc.utf8encode() else: doc = scope.doc.byteencode() - return '"%s"' % StringEncoding.escape_byte_string(doc) + return '__Pyx_DOCSTR("%s")' % StringEncoding.escape_byte_string(doc) else: return "0" -- 2.26.2