Commit f3f7b612 authored by Stefan Behnel's avatar Stefan Behnel

Allow searching for include/import files without passing a source pos tuple...

Allow searching for include/import files without passing a source pos tuple and clean up the call chains a little.
parent f7991615
...@@ -209,7 +209,8 @@ class Context(object): ...@@ -209,7 +209,8 @@ class Context(object):
# Set pxd_file_loaded such that we don't need to # Set pxd_file_loaded such that we don't need to
# look for the non-existing pxd file next time. # look for the non-existing pxd file next time.
scope.pxd_file_loaded = True scope.pxd_file_loaded = True
package_pathname = self.search_include_directories(qualified_name, ".py", pos) package_pathname = self.search_include_directories(
qualified_name, suffix=".py", source_pos=pos)
if package_pathname and package_pathname.endswith(Utils.PACKAGE_FILES): if package_pathname and package_pathname.endswith(Utils.PACKAGE_FILES):
pass pass
else: else:
...@@ -232,7 +233,7 @@ class Context(object): ...@@ -232,7 +233,7 @@ class Context(object):
pass pass
return scope return scope
def find_pxd_file(self, qualified_name, pos, sys_path=True): def find_pxd_file(self, qualified_name, pos=None, sys_path=True, source_file_path=None):
# Search include path (and sys.path if sys_path is True) for # Search include path (and sys.path if sys_path is True) for
# the .pxd file corresponding to the given fully-qualified # the .pxd file corresponding to the given fully-qualified
# module name. # module name.
...@@ -241,34 +242,36 @@ class Context(object): ...@@ -241,34 +242,36 @@ class Context(object):
# 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=sys_path) pxd = self.search_include_directories(
qualified_name, suffix=".pxd", source_pos=pos, sys_path=sys_path, source_file_path=source_file_path)
if pxd is None and Options.cimport_from_pyx: if pxd is None and Options.cimport_from_pyx:
return self.find_pyx_file(qualified_name, pos) return self.find_pyx_file(qualified_name, pos)
return pxd return pxd
def find_pyx_file(self, qualified_name, pos): def find_pyx_file(self, qualified_name, pos=None, source_file_path=None):
# Search include path for the .pyx file corresponding to the # Search include path for the .pyx file corresponding to the
# given fully-qualified module name, as for find_pxd_file(). # given fully-qualified module name, as for find_pxd_file().
return self.search_include_directories(qualified_name, ".pyx", pos) return self.search_include_directories(
qualified_name, suffix=".pyx", source_pos=pos, source_file_path=source_file_path)
def find_include_file(self, filename, pos): def find_include_file(self, filename, pos=None, source_file_path=None):
# Search list of include directories for filename. # Search list of include directories for filename.
# Reports an error and returns None if not found. # Reports an error and returns None if not found.
path = self.search_include_directories(filename, "", pos, path = self.search_include_directories(
include=True) filename, source_pos=pos, include=True, source_file_path=source_file_path)
if not path: if not path:
error(pos, "'%s' not found" % filename) error(pos, "'%s' not found" % filename)
return path return path
def search_include_directories(self, qualified_name, suffix, pos, def search_include_directories(self, qualified_name,
include=False, sys_path=False): suffix=None, source_pos=None, include=False, sys_path=False, source_file_path=None):
include_dirs = self.include_directories include_dirs = self.include_directories
if sys_path: if sys_path:
include_dirs = include_dirs + sys.path include_dirs = include_dirs + sys.path
# include_dirs must be hashable for caching in @cached_function # include_dirs must be hashable for caching in @cached_function
include_dirs = tuple(include_dirs + [standard_include_path]) include_dirs = tuple(include_dirs + [standard_include_path])
return search_include_directories(include_dirs, qualified_name, return search_include_directories(
suffix, pos, include) include_dirs, qualified_name, suffix or "", source_pos, include, source_file_path)
def find_root_package_dir(self, file_path): def find_root_package_dir(self, file_path):
return Utils.find_root_package_dir(file_path) return Utils.find_root_package_dir(file_path)
...@@ -282,15 +285,14 @@ class Context(object): ...@@ -282,15 +285,14 @@ class Context(object):
c_time = Utils.modification_time(output_path) c_time = Utils.modification_time(output_path)
if Utils.file_newer_than(source_path, c_time): if Utils.file_newer_than(source_path, c_time):
return 1 return 1
pos = [source_path]
pxd_path = Utils.replace_suffix(source_path, ".pxd") pxd_path = Utils.replace_suffix(source_path, ".pxd")
if os.path.exists(pxd_path) and Utils.file_newer_than(pxd_path, c_time): if os.path.exists(pxd_path) and Utils.file_newer_than(pxd_path, c_time):
return 1 return 1
for kind, name in self.read_dependency_file(source_path): for kind, name in self.read_dependency_file(source_path):
if kind == "cimport": if kind == "cimport":
dep_path = self.find_pxd_file(name, pos) dep_path = self.find_pxd_file(name, source_file_path=source_path)
elif kind == "include": elif kind == "include":
dep_path = self.search_include_directories(name, "", pos) dep_path = self.search_include_directories(name, source_file_path=source_path)
else: else:
continue continue
if dep_path and Utils.file_newer_than(dep_path, c_time): if dep_path and Utils.file_newer_than(dep_path, c_time):
...@@ -629,24 +631,25 @@ def compile(source, options = None, full_module_name = None, **kwds): ...@@ -629,24 +631,25 @@ def compile(source, options = None, full_module_name = None, **kwds):
@Utils.cached_function @Utils.cached_function
def search_include_directories(dirs, qualified_name, suffix, pos, include=False): def search_include_directories(dirs, qualified_name, suffix="", pos=None, include=False, source_file_path=None):
""" """
Search the list of include directories for the given file name. Search the list of include directories for the given file name.
If a source file position is given, first searches the directory If a source file path or position is given, first searches the directory
containing that file. Returns None if not found, but does not containing that file. Returns None if not found, but does not report an error.
report an error.
The 'include' option will disable package dereferencing. The 'include' option will disable package dereferencing.
""" """
if pos: if pos and not source_file_path:
file_desc = pos[0] file_desc = pos[0]
if not isinstance(file_desc, FileSourceDescriptor): if not isinstance(file_desc, FileSourceDescriptor):
raise RuntimeError("Only file sources for code supported") raise RuntimeError("Only file sources for code supported")
source_file_path = file_desc.filename
if source_file_path:
if include: if include:
dirs = (os.path.dirname(file_desc.filename),) + dirs dirs = (os.path.dirname(source_file_path),) + dirs
else: else:
dirs = (Utils.find_root_package_dir(file_desc.filename),) + dirs dirs = (Utils.find_root_package_dir(source_file_path),) + dirs
# search for dotted filename e.g. <dir>/foo.bar.pxd # search for dotted filename e.g. <dir>/foo.bar.pxd
dotted_filename = qualified_name dotted_filename = qualified_name
......
...@@ -520,7 +520,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): ...@@ -520,7 +520,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
if not target_file_dir.startswith(target_dir): if not target_file_dir.startswith(target_dir):
# any other directories may not be writable => avoid trying # any other directories may not be writable => avoid trying
continue continue
source_file = search_include_file(included_file, "", self.pos, include=True) source_file = search_include_file(included_file, source_pos=self.pos, include=True)
if not source_file: if not source_file:
continue continue
if target_file_dir != target_dir and not os.path.exists(target_file_dir): if target_file_dir != target_dir and not os.path.exists(target_file_dir):
......
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