[Cython] Proposal: extern implementations of functions

2015-03-18 Thread Jeroen Demeyer

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


[Cython] Cython magic annotate option is broken under IPython 3.0

2015-03-18 Thread Nathan Goldbaum
Hi all,

To reproduce this issue, be on cython 0.22 and IPython 3.0 and run the
following notebook:

http://nbviewer.ipython.org/gist/ngoldbaum/855a629d997aa7959254

Googling the error returns some several year old discussions in IPython
related to the autoreload magic and I'm not sure if that is a red herring
here:

http://mail.scipy.org/pipermail/ipython-dev/2012-May/009288.html

For how I'm working around this by calling cython at the command line in my
notebook and then loading the annotated HTML with an IFrame.

Thanks for your help!
___
cython-devel mailing list
cython-devel@python.org
https://mail.python.org/mailman/listinfo/cython-devel


[Cython] Generated function has invalid name when converting from python object to C struct defined in C++ namespace

2015-03-18 Thread Daniel Grunwald

Hello,

I have Cython code like this:
cdef extern from "cpp_library.h" namespace "CppLibrary":
struct SomeStruct:
int i

void do_with_struct(SomeStruct s)

def run():
do_with_struct(object())

With cython 0.21.1, invalid C++ code is generated:
   struct CppLibrary::SomeStruct;
   static struct CppLibrary::SomeStruct 
__pyx_convert__from_py_CppLibrary::SomeStruct(PyObject *);


With cython master, a compiler error occurs:
@cname("__pyx_convert__from_py_CppLibrary::SomeStruct")
cdef SomeStruct __pyx_convert__from_py_CppLibrary::SomeStruct(obj) 
except *:

 ^
FromPyStructUtility:11:50: Expected an identifier or literal

Cython should mangle the struct name to ensure the helper function has a 
valid name.



Regards,

--
Daniel Grunwald
Axivion GmbH
Nobelstr. 15
70569 Stuttgart
Deutschland
Tel: +49 711 6204378-33
Fax: +49 711 6204378-99

Geschaeftsfuehrung: Stefan Bellon, Thomas Eisenbarth, Sebastian Rummler
Sitz der Gesellschaft: Stuttgart
Registergericht: Amtsgericht Stuttgart, HRB 720590

___
cython-devel mailing list
cython-devel@python.org
https://mail.python.org/mailman/listinfo/cython-devel


Re: [Cython] PyPy3 fixes

2015-03-18 Thread Stefan Behnel
Lisandro Dalcin schrieb am 08.03.2015 um 20:25:
> 1) Stefan, I had to push this fix, just to be sure, please double check.
> 
> https://github.com/cython/cython/commit/6bd3f7b9e494d1259082aecfc0366da15fc105ec

Thanks - sorry for the bug.


> 2) PyPy3 defines some legacy (Py2) Py_TPFLAGS_XXX values. Do you like
> the following fix? IOW, I check if the value is #define'd, if not I
> #define it to 0 (zero):
> 
> diff --git a/Cython/Utility/ModuleSetupCode.c 
> b/Cython/Utility/ModuleSetupCode.c
> index 6477fb2..3ae8a0c 100644
> --- a/Cython/Utility/ModuleSetupCode.c
> +++ b/Cython/Utility/ModuleSetupCode.c
> @@ -59,13 +59,16 @@
>#define __Pyx_DefaultClassType PyType_Type
>  #endif
> 
> -#if PY_MAJOR_VERSION >= 3
> +#if !defined(Py_TPFLAGS_CHECKTYPES)
>#define Py_TPFLAGS_CHECKTYPES 0
> +#endif
> +#if !defined(Py_TPFLAGS_HAVE_INDEX)
>#define Py_TPFLAGS_HAVE_INDEX 0
> +#endif
> +#if !defined(Py_TPFLAGS_HAVE_NEWBUFFER)
>#define Py_TPFLAGS_HAVE_NEWBUFFER 0
>  #endif
> -
> -#if PY_VERSION_HEX < 0x030400a1 && !defined(Py_TPFLAGS_HAVE_FINALIZE)
> +#if !defined(Py_TPFLAGS_HAVE_FINALIZE)
>#define Py_TPFLAGS_HAVE_FINALIZE 0
>  #endif

LGTM. I tend to use the shorter "#ifndef" instead of "#if !defined()", though.

Stefan

___
cython-devel mailing list
cython-devel@python.org
https://mail.python.org/mailman/listinfo/cython-devel