def setUp(self): self.rootdir = tempfile.mkdtemp() self.pydir = os.path.join(self.rootdir, 'src', '__python__') self._materialize_tree( self.rootdir, {'src/': {'__python__/': self._PATH_SPEC}}) foo_script = os.path.join(self.rootdir, 'foo.py') self.importer = imputil.Importer(self.rootdir, 'foo', foo_script, False) bar_script = os.path.join(self.pydir, 'bar', '__init__.py') self.bar_importer = imputil.Importer( self.rootdir, 'bar', bar_script, False) fred_script = os.path.join(self.pydir, 'bar', 'fred', '__init__.py') self.fred_importer = imputil.Importer( self.rootdir, 'bar.fred', fred_script, False) self.foo_import = imputil.Import( 'foo', os.path.join(self.pydir, 'foo.py')) self.qux_import = imputil.Import( 'qux', os.path.join(self.pydir, 'qux.py')) self.bar_import = imputil.Import( 'bar', os.path.join(self.pydir, 'bar/__init__.py')) self.fred_import = imputil.Import( 'bar.fred', os.path.join(self.pydir, 'bar/fred/__init__.py')) self.quux_import = imputil.Import( 'bar.fred.quux', os.path.join(self.pydir, 'bar/fred/quux.py')) self.baz2_import = imputil.Import( 'bar.baz', os.path.join(self.pydir, 'bar/baz.py')) self.foo2_import = imputil.Import( 'bar.foo', os.path.join(self.pydir, 'bar/foo.py')) self.baz_import = imputil.Import( 'baz', os.path.join(self.pydir, 'baz.py'))
def testRelativeModuleFromSubModule(self): imp = copy.deepcopy(self.foo2_import) imp.add_binding(imputil.Import.MODULE, 'foo', 1) baz_script = os.path.join(self.pydir, 'bar', 'baz.py') importer = imputil.Importer(self.rootdir, 'bar.baz', baz_script, False) node = pythonparser.parse('from . import foo').body[0] self._assert_imports_equal([imp], importer.visit(node))
def testImportPackageModuleAbsoluteImport(self): imp = copy.deepcopy(self.baz_import) imp.add_binding(imputil.Import.MODULE, 'baz', 0) bar_script = os.path.join(self.pydir, 'bar', '__init__.py') importer = imputil.Importer(self.rootdir, 'bar', bar_script, True) got = importer.visit(pythonparser.parse('import baz').body[0]) self._assert_imports_equal([imp], got)
def testImportPackageModuleRelativeFromSubModule(self): imp = copy.deepcopy(self.baz2_import) imp.add_binding(imputil.Import.MODULE, 'baz', 1) foo_script = os.path.join(self.pydir, 'bar', 'foo.py') importer = imputil.Importer(self.rootdir, 'bar.foo', foo_script, False) got = importer.visit(pythonparser.parse('import baz').body[0]) self._assert_imports_equal([imp], got)
def _ParseAndVisit(source): mod = pythonparser.parse(source) _, future_features = imputil.parse_future_features(mod) importer = imputil.Importer(None, 'foo', 'foo.py', False) b = block.ModuleBlock(importer, '__main__', '<test>', source, future_features) visitor = stmt.StatementVisitor(b) visitor.visit(mod) return visitor
def _MakeModuleBlock(): importer = imputil.Importer(None, '__main__', '/tmp/foo.py', False) return block.ModuleBlock(importer, '__main__', '<test>', '', imputil.FutureFeatures())
def testImportEmptyPath(self): importer = imputil.Importer(None, 'foo', 'foo.py', False) self.assertRaises(util.ImportError, importer.visit, pythonparser.parse('import bar').body[0])