Пример #1
0
 def testRelativeModuleMemberMixed(self):
     imp1 = copy.deepcopy(self.fred_import)
     imp1.add_binding(imputil.Import.MEMBER, 'qux', 'qux')
     imp2 = copy.deepcopy(self.quux_import)
     imp2.add_binding(imputil.Import.MODULE, 'quux', 2)
     node = pythonparser.parse('from .fred import qux, quux').body[0]
     self._assert_imports_equal([imp1, imp2], self.bar_importer.visit(node))
Пример #2
0
 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)
Пример #3
0
    def testFutureFeatures(self):
        testcases = [
            ('from __future__ import print_function',
             imputil.FutureFeatures(print_function=True)),
            ("""\
        "module docstring"

        from __future__ import print_function
        """, imputil.FutureFeatures(print_function=True)),
            ("""\
        "module docstring"

        from __future__ import print_function, with_statement
        from __future__ import nested_scopes
        """, imputil.FutureFeatures(print_function=True)),
            ('from __future__ import absolute_import',
             imputil.FutureFeatures(absolute_import=True)),
            ('from __future__ import absolute_import, print_function',
             imputil.FutureFeatures(absolute_import=True,
                                    print_function=True)),
            ('foo = 123\nfrom __future__ import print_function',
             imputil.FutureFeatures()),
            ('import os\nfrom __future__ import print_function',
             imputil.FutureFeatures()),
        ]

        for tc in testcases:
            source, want = tc
            mod = pythonparser.parse(textwrap.dedent(source))
            _, got = imputil.parse_future_features(mod)
            self.assertEqual(want.__dict__, got.__dict__)
Пример #4
0
 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)
Пример #5
0
 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))
Пример #6
0
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
Пример #7
0
def collect_imports(modname, script, gopath, package_dir=''):
    with open(script) as py_file:
        py_contents = py_file.read()
    mod = pythonparser.parse(py_contents)
    future_node, future_features = parse_future_features(mod)
    importer = Importer(gopath,
                        modname,
                        script,
                        future_features.absolute_import,
                        package_dir=package_dir)
    collector = _ImportCollector(importer, future_node)
    collector.visit(mod)
    return collector.imports
Пример #8
0
    def testImportFromFuture(self):
        testcases = [
            ('from __future__ import print_function',
             imputil.FutureFeatures(print_function=True)),
            ('from __future__ import generators', imputil.FutureFeatures()),
            ('from __future__ import generators, print_function',
             imputil.FutureFeatures(print_function=True)),
        ]

        for tc in testcases:
            source, want = tc
            mod = pythonparser.parse(textwrap.dedent(source))
            node = mod.body[0]
            got = imputil._make_future_features(node)  # pylint: disable=protected-access
            self.assertEqual(want.__dict__, got.__dict__)
Пример #9
0
    def testImportFromFutureParseError(self):
        testcases = [
            # NOTE: move this group to testImportFromFuture as they are implemented
            # by grumpy
            ('from __future__ import division',
             r'future feature \w+ not yet implemented'),
            ('from __future__ import braces', 'not a chance'),
            ('from __future__ import nonexistant_feature',
             r'future feature \w+ is not defined'),
        ]

        for tc in testcases:
            source, want_regexp = tc
            mod = pythonparser.parse(source)
            node = mod.body[0]
            self.assertRaisesRegexp(util.ParseError, want_regexp,
                                    imputil._make_future_features, node)  # pylint: disable=protected-access
Пример #10
0
 def testRelativeUpLevelMember(self):
     imp = copy.deepcopy(self.foo2_import)
     imp.add_binding(imputil.Import.MEMBER, 'qux', 'qux')
     node = pythonparser.parse('from ..foo import qux').body[0]
     self._assert_imports_equal([imp], self.fred_importer.visit(node))
Пример #11
0
def _ParseStmt(stmt_str):
  return pythonparser.parse(stmt_str).body[0]
Пример #12
0
 def testGlobalUsedPriorToDeclaration(self):
   node = pythonparser.parse('foo = 42\nglobal foo')
   visitor = block.BlockVisitor()
   self.assertRaisesRegexp(util.ParseError, 'used prior to global declaration',
                           visitor.generic_visit, node)
Пример #13
0
 def testImportEmptyPath(self):
     importer = imputil.Importer(None, 'foo', 'foo.py', False)
     self.assertRaises(util.ImportError, importer.visit,
                       pythonparser.parse('import bar').body[0])
Пример #14
0
 def testUndefinedFutureRaises(self):
     mod = pythonparser.parse('from __future__ import foo')
     self.assertRaisesRegexp(util.ParseError,
                             'future feature foo is not defined',
                             imputil.parse_future_features, mod)
Пример #15
0
 def testUnimplementedFutureRaises(self):
     mod = pythonparser.parse('from __future__ import division')
     msg = 'future feature division not yet implemented by grumpy'
     self.assertRaisesRegexp(util.ParseError, msg,
                             imputil.parse_future_features, mod)
Пример #16
0
 def testImportFromWildcardRaises(self):
     self.assertRaises(util.ImportError, self.importer.visit,
                       pythonparser.parse('from foo import *').body[0])
Пример #17
0
 def testRelativeImportNonPackage(self):
     self.assertRaises(util.ImportError, self.importer.visit,
                       pythonparser.parse('from . import bar').body[0])
Пример #18
0
def _ParseExpr(expr):
    return pythonparser.parse(expr).body[0].value
Пример #19
0
 def testRelativeUpLevel(self):
     imp = copy.deepcopy(self.foo2_import)
     imp.add_binding(imputil.Import.MODULE, 'foo', 1)
     node = pythonparser.parse('from .. import foo').body[0]
     self._assert_imports_equal([imp], self.fred_importer.visit(node))
Пример #20
0
 def testRelativeImportBeyondTopLevel(self):
     self.assertRaises(util.ImportError, self.bar_importer.visit,
                       pythonparser.parse('from .. import qux').body[0])
Пример #21
0
 def testRelativeModuleNoExist(self):
     imp = copy.deepcopy(self.bar_import)
     imp.add_binding(imputil.Import.MEMBER, 'qux', 'qux')
     node = pythonparser.parse('from . import qux').body[0]
     self._assert_imports_equal([imp], self.bar_importer.visit(node))
Пример #22
0
 def _check_imports(self, stmt, want):
     got = self.importer.visit(pythonparser.parse(stmt).body[0])
     self._assert_imports_equal(want, got)
Пример #23
0
 def testImportPackageModuleRelative(self):
     imp = copy.deepcopy(self.baz2_import)
     imp.add_binding(imputil.Import.MODULE, 'baz', 1)
     got = self.bar_importer.visit(pythonparser.parse('import baz').body[0])
     self._assert_imports_equal([imp], got)