Hello Cython developers,
I would like to propose a new mechanism to implement functions in an
external .c file. The goal is to solve the problem I was having in
https://groups.google.com/forum/#!topic/cython-users/TsbBNyvwZn4
With the attached patch, you can do the following:
* in foo.pxd, declare the function as usual:
cdef void myfunc()
* in foo.c, actually implement the function:
static void myfunc()
{
/* ... */
}
* in foo.pyx, declare the function extern:
cdef extern from "foo.c":
void myfunc()
With my patch, the extern declaration in foo.pyx will be interpreted as
implementing the function. Therefore, myfunc() is now available in the
module foo, and it can also be cimported by other modules.
What do you think of this proposed feature?
Jeroen.
diff --git a/Cython/Compiler/Symtab.py b/Cython/Compiler/Symtab.py
index 339b509..1437f83 100644
--- a/Cython/Compiler/Symtab.py
+++ b/Cython/Compiler/Symtab.py
@@ -698,8 +698,15 @@ class Scope(object):
cname = self.mangle(Naming.func_prefix, name)
entry = self.lookup_here(name)
if entry:
+ if not in_pxd and visibility != entry.visibility and visibility == 'extern':
+ # Previously declared, but now extern => treat this
+ # as implementing the function, using the new cname
+ defining = True
+ visibility = entry.visibility
+ entry.cname = cname
+ entry.func_cname = cname
if visibility != 'private' and visibility != entry.visibility:
- warning(pos, "Function '%s' previously declared as '%s'" % (name, entry.visibility), 1)
+ warning(pos, "Function '%s' previously declared as '%s', now as '%s'" % (name, entry.visibility, visibility), 1)
if overridable != entry.is_overridable:
warning(pos, "Function '%s' previously declared as '%s'" % (
name, 'cpdef' if overridable else 'cdef'), 1)
_______________________________________________
cython-devel mailing list
cython-devel@python.org
https://mail.python.org/mailman/listinfo/cython-devel