Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def testResolveRelativeInNonPackage(self):
r = self.make_resolver("a.py", "a")
imp = parsepy.ImportStatement(".b", is_from=True)
with self.assertRaises(resolve.ImportException):
r.resolve_import(imp)
files = [(name, filename)]
if short_name:
short_filename = os.path.dirname(filename)
files.append((short_name, short_filename))
for module_name, path in files:
for fs in self.fs_path:
f = self._find_file(fs, path)
if not f or f == self.current_module.path:
# We cannot import a file from itself.
continue
if item.is_relative():
package_name = self.current_module.package_name
if package_name is None:
# Relative import in non-package
raise ImportException(name)
module_name = get_absolute_name(package_name, module_name)
if isinstance(self.current_module, System):
return System(f, module_name)
return Local(f, module_name, fs)
# If the module isn't found in the explicit pythonpath, see if python
# itself resolved it.
if item.source:
prefix, ext = os.path.splitext(item.source)
mod_name = name
# We need to check for importing a symbol here too.
if short_name:
mod = prefix.replace(os.path.sep, '.')
mod = utils.strip_suffix(mod, '.__init__')
if not mod.endswith(name) and mod.endswith(short_name):
mod_name = short_name
mod = prefix.replace(os.path.sep, '.')
mod = utils.strip_suffix(mod, '.__init__')
if not mod.endswith(name) and mod.endswith(short_name):
mod_name = short_name
if ext == '.pyc':
pyfile = prefix + '.py'
if os.path.exists(pyfile):
return System(pyfile, mod_name)
elif not ext:
pyfile = os.path.join(prefix, "__init__.py")
if os.path.exists(pyfile):
return System(pyfile, mod_name)
return System(item.source, mod_name)
raise ImportException(name)
def get_file_deps(self, filename):
r = resolve.Resolver(self.path, filename)
resolved = []
unresolved = []
provenance = {}
for imp in parsepy.get_imports(filename, self.env.python_version):
try:
f = r.resolve_import(imp)
if not f.is_extension():
full_path = os.path.abspath(f.path)
resolved.append(full_path)
provenance[full_path] = f
except resolve.ImportException:
unresolved.append(imp)
return (resolved, unresolved, provenance)
def __init__(self, module_name):
super(ImportException, self).__init__(module_name)
self.module_name = module_name
def get_file_deps(self, filename):
resolved = []
unresolved = []
parent = self.provenance[filename]
r = resolve.Resolver(self.path, parent)
for imp in parsepy.get_imports(filename, self.env.python_version):
try:
f = r.resolve_import(imp)
if f.is_extension():
continue
full_path = os.path.abspath(f.path)
resolved.append(full_path)
self.provenance[full_path] = f
except resolve.ImportException:
unresolved.append(imp)
return (resolved, unresolved)