Commit 2796ac2e authored by Jeroen Demeyer's avatar Jeroen Demeyer

Do not search sys.path for .pxd file if no explicit "cimport" is given

parent 49d47ab2
...@@ -91,6 +91,9 @@ Bugs fixed ...@@ -91,6 +91,9 @@ Bugs fixed
* C unions use a saner way to coerce from and to Python dicts. * C unions use a saner way to coerce from and to Python dicts.
* When compiling a module ``foo.pyx``, the directories in ``sys.path``
are no longer searched when looking for ``foo.pxd``.
Other changes Other changes
------------- -------------
......
...@@ -187,20 +187,25 @@ class Context(object): ...@@ -187,20 +187,25 @@ class Context(object):
if not scope.pxd_file_loaded: if not scope.pxd_file_loaded:
if debug_find_module: if debug_find_module:
print("...pxd not loaded") print("...pxd not loaded")
scope.pxd_file_loaded = 1
if not pxd_pathname: if not pxd_pathname:
if debug_find_module: if debug_find_module:
print("...looking for pxd file") print("...looking for pxd file")
pxd_pathname = self.find_pxd_file(qualified_name, pos) # Only look in sys.path if we are explicitly looking
# for a .pxd file.
pxd_pathname = self.find_pxd_file(qualified_name, pos, sys_path=need_pxd)
if debug_find_module: if debug_find_module:
print("......found %s" % pxd_pathname) print("......found %s" % pxd_pathname)
if not pxd_pathname and need_pxd: if not pxd_pathname and need_pxd:
# Set pxd_file_loaded such that we don't need to
# look for the non-existing pxd file next time.
scope.pxd_file_loaded = True
package_pathname = self.search_include_directories(qualified_name, ".py", pos) package_pathname = self.search_include_directories(qualified_name, ".py", pos)
if package_pathname and package_pathname.endswith('__init__.py'): if package_pathname and package_pathname.endswith('__init__.py'):
pass pass
else: else:
error(pos, "'%s.pxd' not found" % qualified_name.replace('.', os.sep)) error(pos, "'%s.pxd' not found" % qualified_name.replace('.', os.sep))
if pxd_pathname: if pxd_pathname:
scope.pxd_file_loaded = True
try: try:
if debug_find_module: if debug_find_module:
print("Context.find_module: Parsing %s" % pxd_pathname) print("Context.find_module: Parsing %s" % pxd_pathname)
...@@ -217,15 +222,16 @@ class Context(object): ...@@ -217,15 +222,16 @@ class Context(object):
pass pass
return scope return scope
def find_pxd_file(self, qualified_name, pos): def find_pxd_file(self, qualified_name, pos, sys_path=True):
# Search include path for the .pxd file corresponding to the # Search include path (and sys.path if sys_path is True) for
# given fully-qualified module name. # the .pxd file corresponding to the given fully-qualified
# module name.
# Will find either a dotted filename or a file in a # Will find either a dotted filename or a file in a
# package directory. If a source file position is given, # package directory. If a source file position is given,
# the directory containing the source file is searched first # the directory containing the source file is searched first
# for a dotted filename, and its containing package root # for a dotted filename, and its containing package root
# directory is searched first for a non-dotted filename. # directory is searched first for a non-dotted filename.
pxd = self.search_include_directories(qualified_name, ".pxd", pos, sys_path=True) pxd = self.search_include_directories(qualified_name, ".pxd", pos, sys_path=sys_path)
if pxd is None: # XXX Keep this until Includes/Deprecated is removed if pxd is None: # XXX Keep this until Includes/Deprecated is removed
if (qualified_name.startswith('python') or if (qualified_name.startswith('python') or
qualified_name in ('stdlib', 'stdio', 'stl')): qualified_name in ('stdlib', 'stdio', 'stl')):
......
...@@ -123,12 +123,15 @@ Search paths for definition files ...@@ -123,12 +123,15 @@ Search paths for definition files
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
When you :keyword:`cimport` a module called ``modulename``, the Cython When you :keyword:`cimport` a module called ``modulename``, the Cython
compiler searches for a file called :file:`modulename.pxd` along the search compiler searches for a file called :file:`modulename.pxd`.
path for include files, as specified by ``-I`` command line options. It searches for this file along the path for include files
(as specified by ``-I`` command line options or the ``include_path``
option to ``cythonize()``), as well as ``sys.path``.
Also, whenever you compile a file :file:`modulename.pyx`, the corresponding Also, whenever you compile a file :file:`modulename.pyx`, the corresponding
definition file :file:`modulename.pxd` is first searched for along the same definition file :file:`modulename.pxd` is first searched for along the
path, and if found, it is processed before processing the ``.pyx`` file. include path (but not ``sys.path``), and if found, it is processed before
processing the ``.pyx`` file.
Using cimport to resolve naming conflicts Using cimport to resolve naming conflicts
......
PYTHON setup.py build_ext --inplace
######## setup.py ########
from Cython.Build import cythonize
from Cython.Distutils.extension import Extension
import sys
sys.path.append("path")
ext_modules = [
Extension("a", ["a.pyx"]),
Extension("b", ["b.pyx"]),
Extension("c", ["c.pyx"]),
]
ext_modules = cythonize(ext_modules, include_path=["include"])
######## a.pyx ########
# Implicit cimport looking in include_path
cdef my_type foo
######## include/a.pxd ########
ctypedef int my_type
######## b.pyx ########
# Explicit cimport looking in sys.path
from b cimport *
cdef my_type foo
######## path/b.pxd ########
ctypedef int my_type
######## c.pyx ########
# Implicit cimport NOT looking in sys.path
######## path/c.pxd ########
+++syntax error just to show that this file is not actually cimported+++
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment