def visit_ImportFrom(self, node): self._write_py_context(node.lineno) visitor = imputil.ImportVisitor(self.block.root.path) visitor.visit(node) for imp in visitor.imports: if imp.is_native: values = [b.value for b in imp.bindings] with self._import_native(imp.name, values) as mod: for binding in imp.bindings: # Strip the 'type_' prefix when populating the module. This means # that, e.g. 'from __go__.foo import type_Bar' will populate foo # with a member called Bar, not type_Bar (although the symbol in # the importing module will still be type_Bar unless aliased). This # bends the semantics of import but makes native module contents # more sensible. name = binding.value if name.startswith(_NATIVE_TYPE_PREFIX): name = name[len(_NATIVE_TYPE_PREFIX):] with self.block.alloc_temp() as member: self.writer.write_checked_call2( member, 'πg.GetAttr(πF, {}, {}, nil)', mod.expr, self.block.root.intern(name)) self.block.bind_var(self.writer, binding.alias, member.expr) elif node.module == '__future__': # At this stage all future imports are done in an initial pass (see # visit() above), so if they are encountered here after the last valid # __future__ then it's a syntax error. if node.lineno > self.future_features.future_lineno: raise util.ImportError(node, late_future) else: self._import_and_bind(imp)
def visit_ImportFrom(self, node): self._write_py_context(node.lineno) visitor = imputil.ImportVisitor(self.block.root.path, self.future_node) visitor.visit(node) for imp in visitor.imports: if imp.is_native: values = [b.value for b in imp.bindings] with self._import_native(imp.name, values) as mod: for binding in imp.bindings: # Strip the 'type_' prefix when populating the module. This means # that, e.g. 'from __go__.foo import type_Bar' will populate foo # with a member called Bar, not type_Bar (although the symbol in # the importing module will still be type_Bar unless aliased). This # bends the semantics of import but makes native module contents # more sensible. name = binding.value if name.startswith(_NATIVE_TYPE_PREFIX): name = name[len(_NATIVE_TYPE_PREFIX):] with self.block.alloc_temp() as member: self.writer.write_checked_call2( member, 'πg.GetAttr(πF, {}, {}, nil)', mod.expr, self.block.root.intern(name)) self.block.bind_var(self.writer, binding.alias, member.expr) elif node.module != '__future__': self._import_and_bind(imp)
def _visit_import(self, source, path=None): if not path: path = MockPath() visitor = imputil.ImportVisitor(path) visitor.visit(pythonparser.parse(source).body[0]) return visitor.imports
def testImportLateFuture(self): mod = pythonparser.parse( 'import os\nfrom __future__ import print_function') visitor = imputil.ImportVisitor(MockPath()) self.assertRaises(util.LateFutureError, visitor.visit, mod)
def testImportFromFuture(self): mod = pythonparser.parse('from __future__ import print_function') visitor = imputil.ImportVisitor(MockPath(), mod.body[0]) visitor.visit(mod) self.assertEqual([], visitor.imports)
def visit_Import(self, node): self._write_py_context(node.lineno) visitor = imputil.ImportVisitor(self.block.root.path) visitor.visit(node) for imp in visitor.imports: self._import_and_bind(imp)