[Cython] star-imports in Cython/Includes/cpython considered harmful

Stefan Behnel stefan_ml at behnel.de
Mon Feb 20 09:45:35 CET 2012


Hi,

I just noticed that the star-imports in the cpython package have serious
side effects. The cpython/__init__.pxd file has this in it:

"""
from cpython.version cimport *
from cpython.ref cimport *
from cpython.exc cimport *
from cpython.module cimport *
from cpython.mem cimport *
...
"""

This means that a direct cimport of any of those recursively cimported
names from the cpython package namespace will always trigger a complete
cimport of all cpython.* modules, thus

1) wasting compile time

2) polluting the cpython package namespace

3) polluting the internal state of Cython's list of cimported modules

4) leading to things being defined in the generated C code that don't need
to be there, specifically the object structs of PyBoolObject and
PyComplexObject, which we provide definitions for in cpython.bool and
cpython.complex.

The point where I noticed this is when I got test errors in PyPy because it
doesn't expose the bool/complex structs. That wasn't because the test
actually used them, it was purely because it had this cimport in it:

  from cpython cimport PyObject

If it had used this instead:

  from cpython.object cimport PyObject

Cython would still have parsed all of those .pxd files, but at least it
wouldn't have used those declarations to do any harm.

Now, I'm 100% certain that there is user code out there that does this as
well. Fixing this bug by removing all the star-imports will break that
code, but keeping the cimports in the package will trap the average lazy
user into cimporting stuff from there directly.

I'll add a big warning to the package for now, but I wonder if we shouldn't
accept breaking this code at some point (and sooner is always better than
later).

Stefan


More information about the cython-devel mailing list