Cythonize is special?

410 views
Skip to first unread message

Shriramana Sharma

unread,
May 19, 2013, 7:46:05 AM5/19/13
to cython...@googlegroups.com
Hello. How exactly is:

from distutils.core import setup
from Cython.Build import cythonize
setup(
name = "My hello app",
ext_modules = cythonize('hello.pyx'), # accepts a glob pattern
)

different from:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
setup (
ext_modules = [ Extension ( "hello", [ "hello.pyx" ] ) ],
cmdclass = { "build_ext" : build_ext }
)

Is it just that the former is somewhat shorter, especially seeing as
the docstring for cythonize says: ""Compile a set of source modules
into C/C++ files and return a list of distutils Extension objects for
them.""

Or is there any other essential difference that makes using Cythonize
preferable to manually specifying the Extension params?

Thanks.

--
Shriramana Sharma ஶ்ரீரமணஶர்மா श्रीरमणशर्मा

Robert Bradshaw

unread,
May 20, 2013, 1:42:25 PM5/20/13
to cython...@googlegroups.com
Being shorter is an advantage, especially using the glob pattern so
one does not have to manually list each module (which is more like
Python and becomes an issue when one has hundreds of extension
modules). But the primary advantages are

* cythonize() understands inter-module dependencies, e.g. if foo.pyx
cimports bar.pxd, it will need to be re-compiled when bar.pxd changes.
Standard distutils has no understanding of these dependencies, and
while one could add bar.pxd as a dependency explicitly, one would also
have to transitively add anything bar.pxd cimports and manually keep
these in sync.

* we don't force the user to use our hacked build_ext, which may
conflict with a custom or project build_ext that they might want to
use, and in particular the build_ext isn't required to know anything
about Cython or .pyx files.

Another small benefit is that cythonize() can compile modules in parallel.

- Robert

dbv

unread,
May 23, 2013, 6:33:46 AM5/23/13
to cython...@googlegroups.com
Would like to try the cythonize() method but still not sure what the correct syntax is.  My setup files are typically of the form:

from distutils.core import setup 
from Cython.Distutils.extension import Extension
from Cython.Distutils import build_ext 

ext_modules = [
    Extension("hello", ["hello.pyx"]), Extension("byebye", ["byebye.pyx"]))
    ]

setup(
    cmdclass = {'build_ext': build_ext},
    ext_modules = ext_modules
    )

What would a cythonize() version look like?

Robert Bradshaw

unread,
May 23, 2013, 11:48:39 AM5/23/13
to cython...@googlegroups.com
Even shorter:

from distutils.core import setup

ext_modules = cythonize("*.pyx")

setup(
ext_modules = ext_modules
)

You can use this same setup.py for all your cython files. You can also do

cythonize(["a.pyx", "foo/**/*.pyx"])

to compile a.pyx and all files under foo (recursively).

- Robert

dbv

unread,
May 25, 2013, 11:21:01 AM5/25/13
to cython...@googlegroups.com
Works, and is very quick to build the extensions.  Brill!
Reply all
Reply to author
Forward
0 new messages