Fix C API namespace pollution
authorStefan Behnel <scoder@users.berlios.de>
Thu, 18 Oct 2007 06:22:31 +0000 (08:22 +0200)
committerStefan Behnel <scoder@users.berlios.de>
Thu, 18 Oct 2007 06:22:31 +0000 (08:22 +0200)
Cython/Compiler/ModuleNode.py
Cython/Compiler/Naming.py

index fdbffa84d18c57ab525aeb9e021171533b4316a8..d93bbbc1a77e19f0d61c5db64bf20b72264fa232 100644 (file)
@@ -1732,18 +1732,31 @@ function_export_utility_code = [
 static int __Pyx_ExportFunction(char *n, void *f, char *s); /*proto*/
 """,r"""
 static int __Pyx_ExportFunction(char *n, void *f, char *s) {
+    PyObject *d = 0;
     PyObject *p = 0;
+    d = PyObject_GetAttrString(%(MODULE)s, "%(API)s");
+    if (!d) {
+        PyErr_Clear();
+        d = PyDict_New();
+        if (!d)
+            goto bad;
+        Py_INCREF(d);
+        if (PyModule_AddObject(%(MODULE)s, "%(API)s", d) < 0)
+            goto bad;
+    }
     p = PyCObject_FromVoidPtrAndDesc(f, s, 0);
     if (!p)
         goto bad;
-    if (PyModule_AddObject(%(MODULE)s, n, p) < 0)
+    if (PyDict_SetItemString(d, n, p) < 0)
         goto bad;
+    Py_DECREF(d);
     return 0;
 bad:
     Py_XDECREF(p);
+    Py_XDECREF(d);
     return -1;
 }
-""" % {'MODULE': Naming.module_cname}]
+""" % {'MODULE': Naming.module_cname, 'API': Naming.api_name}]
 
 #------------------------------------------------------------------------------------
 
@@ -1752,13 +1765,17 @@ function_import_utility_code = [
 static int __Pyx_ImportFunction(PyObject *module, char *funcname, void **f, char *sig); /*proto*/
 ""","""
 static int __Pyx_ImportFunction(PyObject *module, char *funcname, void **f, char *sig) {
+    PyObject *d = 0;
     PyObject *cobj = 0;
     char *desc;
     
-    cobj = PyObject_GetAttrString(module, funcname);
+    d = PyObject_GetAttrString(module, "%(API)s");
+    if (!d)
+        goto bad;
+    cobj = PyDict_GetItemString(d, funcname);
     if (!cobj) {
         PyErr_Format(PyExc_ImportError,
-            "%s does not export expected C function %s",
+            "%%s does not export expected C function %%s",
                 PyModule_GetName(module), funcname);
         goto bad;
     }
@@ -1767,15 +1784,17 @@ static int __Pyx_ImportFunction(PyObject *module, char *funcname, void **f, char
         goto bad;
     if (strcmp(desc, sig) != 0) {
         PyErr_Format(PyExc_TypeError,
-            "C function %s.%s has wrong signature (expected %s, got %s)",
+            "C function %%s.%%s has wrong signature (expected %%s, got %%s)",
                 PyModule_GetName(module), funcname, sig, desc);
         goto bad;
     }
     *f = PyCObject_AsVoidPtr(cobj);
     Py_DECREF(cobj);
+    Py_DECREF(d);
     return 0;
 bad:
     Py_XDECREF(cobj);
+    Py_XDECREF(d);
     return -1;
 }
-"""]
+""" % dict(API = Naming.api_name)]
index 0202ec7dae57a80c8baeba5198d7c434ef1eff20..48deb60d4ec506f748ec22b3d1a86148806670ae 100644 (file)
@@ -67,6 +67,8 @@ exc_lineno_name = pyrex_prefix + "exc_lineno"
 
 exc_vars = (exc_type_name, exc_value_name, exc_tb_name)
 
+api_name        = pyrex_prefix + "capi__"
+
 h_guard_prefix   = "__PYX_HAVE__"
 api_guard_prefix = "__PYX_HAVE_API__"
 api_func_guard   = "__PYX_HAVE_API_FUNC_"