Driven mad by cimport in Cython

39 views
Skip to first unread message

Nathann Cohen

unread,
Feb 18, 2010, 8:22:37 AM2/18/10
to Sage devel
Hello everybody !!!!

I have been spending hours on the same Cython problem, which I hope
you can solve instantly :-)

I have a Cython file, which happens to be sage/numerical/mip_coin.pyx
which defines the following function :

cdef float osi_solve(self,c_OsiSolverInterface * si,bool log,bool
objective_only)

I would like to use this function outside of mip_coin, so I wrote in
this other file :

cimport sage.numerical.mip_coin
from mip_coin cimport osi_solve

Well, everything seems to be fine in this other module, though when
Sage compiles mip_coin.pyx, it looks at its .pxd file and notices the
following line :

cdef float osi_solve(self,c_OsiSolverInterface * si,bool log,bool
objective_only)

Well, this is (I hope) perfectly normal. As I want to be able to use
this function outside, I have to declare it in the .pxd... But then
Cython tell me the following thing :

/usr/local/sage/devel/sage-cplex/sage/numerical/mip_coin.pxd:88:20:
Non-extern C function 'osi_solve' declared but not defined

So it sounds like Sage does not find the declaration of osi_solve even
though it is made in mip_coin.pyx

Please, tell me that you know where it comes from :-)

Nathann

David Roe

unread,
Feb 18, 2010, 12:45:09 PM2/18/10
to sage-...@googlegroups.com
Maybe try

cimport sage.numerical.mip_coin
from sage.numerical.mip_coin cimport osi_solve
David


--
To post to this group, send an email to sage-...@googlegroups.com
To unsubscribe from this group, send an email to sage-devel+...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org

William Stein

unread,
Feb 18, 2010, 12:54:00 PM2/18/10
to sage-...@googlegroups.com

Whenever I've run into the above shortcoming in how Python extension
modules worked, I've made a class that provides that method as a
cdef'd method, then instantiated the class.

You should email cython-devel about this, by the way.

-- William

Nathann Cohen

unread,
Feb 20, 2010, 7:46:31 AM2/20/10
to sage-...@googlegroups.com
But when I instanciate the class I can only use its methods as Python functions... My problem is that some arguments are to be C types :-/

Nathann

William Stein

unread,
Feb 20, 2010, 5:26:13 PM2/20/10
to sage-...@googlegroups.com
On Sat, Feb 20, 2010 at 4:46 AM, Nathann Cohen <nathan...@gmail.com> wrote:
> But when I instanciate the class I can only use its methods as Python
> functions... My problem is that some arguments are to be C types :-/

cimport the module and cdef the class. Then you can call C methods.
This sort of thing is used in many places
in Sage, e.g., devel/sage/sage/modular/modsym/p1llist.pyx.

> Nathann
>
> --
> To post to this group, send an email to sage-...@googlegroups.com
> To unsubscribe from this group, send an email to
> sage-devel+...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/sage-devel
> URL: http://www.sagemath.org
>

--
William Stein
Associate Professor of Mathematics
University of Washington
http://wstein.org

David Roe

unread,
Feb 21, 2010, 11:47:10 AM2/21/10
to sage-...@googlegroups.com
If you're calling the functions from cython code, and your variables are cdef'd properly (so that the cython compiler knows the type of the object), and the function you're calling is cdef'd, it will use C function calls and you can pass in C types.
David

On Sat, Feb 20, 2010 at 7:46 AM, Nathann Cohen <nathan...@gmail.com> wrote:
But when I instanciate the class I can only use its methods as Python functions... My problem is that some arguments are to be C types :-/

Nathann

Nathann Cohen

unread,
Feb 23, 2010, 3:06:27 AM2/23/10
to sage-...@googlegroups.com
In the end it was just this, plus several nasty bugs that took me some time. Without help, I'd still be there !

Here it is : http://trac.sagemath.org/sage_trac/ticket/8172

Thank youuuuuuuuuuuuuu very much !!!!

Nathann
Reply all
Reply to author
Forward
0 new messages