example of on-the-fly compilation and module loading

422 views
Skip to first unread message

Allen Riddell

unread,
May 22, 2013, 11:27:45 AM5/22/13
to cython...@googlegroups.com
Hi cython-users,

Does anyone know of an example of a cython project that does run-time
compilation of generated cython code (along with c/c++ code) and then loading of
the module? I've seen something that uses ctypes but haven't come across
a cython example.

Thanks,

Allen

Robert Bradshaw

unread,
May 23, 2013, 5:43:24 PM5/23/13
to cython...@googlegroups.com
If you have cython installed, you can write

In [1]: import cython

In [2]: cython.inline("""
...: cdef int x = 2
...: print x
...: """)

Compiling /.../.cython/inline/_cython_inline_c02ff2feffdf73239d40335785cbb40b.pyx
because it changed.
Cythonizing /.../.cython/inline/_cython_inline_c02ff2feffdf73239d40335785cbb40b.pyx
2

There's also cython.compile, which compiles a method.

In [3]: @cython.compile
...: def foo(a, b, c):
...: return a + b * c
...:

In [4]: foo(1, 2, 3)
Compiling /.../.cython/inline/_cython_inline_1ca79cad3e887041fdcfd9b2023b4ab3.pyx
because it changed.
Cythonizing /.../.cython/inline/_cython_inline_1ca79cad3e887041fdcfd9b2023b4ab3.pyx
Out[4]: 7

In [5]: foo(1.1, 2, 3) # new input types
Compiling /.../.cython/inline/_cython_inline_5e9b3071b4d334ac6c7df73cef9e2edf.pyx
because it changed.
Cythonizing /.../.cython/inline/_cython_inline_5e9b3071b4d334ac6c7df73cef9e2edf.pyx
Out[5]: 7.1

In [6]: foo(1, 2, 3) # no re-compile
Out[6]: 7

Allen Riddell

unread,
May 23, 2013, 9:09:50 PM5/23/13
to cython...@googlegroups.com
Thanks for this.

What about something a bit more complex like dynamically generated C/C++ code
that also has some library dependencies.

Thanks,

Allen

I'll look into this --
> --
>
> ---
> You received this message because you are subscribed to the Google Groups "cython-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to cython-users...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Robert Bradshaw

unread,
May 23, 2013, 9:13:52 PM5/23/13
to cython...@googlegroups.com
On Thu, May 23, 2013 at 6:09 PM, Allen Riddell <a...@ariddell.org> wrote:
> Thanks for this.
>
> What about something a bit more complex like dynamically generated C/C++ code
> that also has some library dependencies.
>
> Thanks,
>
> Allen
>
> I'll look into this --

You can spit out whatever c files you want, and invoke your compiler
manually to get libraries to link to, if you want to get to that
level.

- Robert

Allen Riddell

unread,
May 24, 2013, 8:17:01 AM5/24/13
to cython...@googlegroups.com
Robert,

I see that cython.inline wraps cython_inline in Cython/Build/Inline.py, which
accepts a bunch of arguments that hook into the distutils building apparatus.
I think I can work from that code.

As an aside, it would be nice to have some docstrings. Is this something folks
can help out with?

Thanks,

Allen

On 05/23, Robert Bradshaw wrote:

Robert Bradshaw

unread,
May 24, 2013, 8:34:23 AM5/24/13
to cython...@googlegroups.com
On Fri, May 24, 2013 at 5:17 AM, Allen Riddell <a...@ariddell.org> wrote:
> Robert,
>
> I see that cython.inline wraps cython_inline in Cython/Build/Inline.py, which
> accepts a bunch of arguments that hook into the distutils building apparatus.
> I think I can work from that code.

Yep.

> As an aside, it would be nice to have some docstrings. Is this something folks
> can help out with?

Yes, for sure! Send a pull request our way.
Reply all
Reply to author
Forward
0 new messages