예제 #1
0
    def testHashing(self):
        n1a = modulegraph.Node('n1')
        n1b = modulegraph.Node('n1')
        n2 = modulegraph.Node('n2')

        d = {}
        d[n1a] = 'n1'
        d[n2] = 'n2'
        self.assertEqual(d[n1b], 'n1')
        self.assertEqual(d[n2], 'n2')
예제 #2
0
    def test_submodules(self):
        n = modulegraph.Node("foobar.xyz")
        self.assertEqual(n._submodule_basename_to_node, {})

        sm = modulegraph.Node("bar.baz")
        self.assertFalse(n.is_submodule('bar'))
        n.add_submodule('bar', sm)
        self.assertTrue(n.is_submodule('bar'))
        self.assertIs(n.get_submodule('bar'), sm)
        self.assertRaises(KeyError, n.get_submodule, 'XXX')
        self.assertIs(n.get_submodule_or_none('XXX'), None)
예제 #3
0
    def testOrder(self):
        n1 = modulegraph.Node("n1")
        n2 = modulegraph.Node("n2")

        self.assertTrue(n1 < n2)
        self.assertFalse(n2 < n1)
        self.assertTrue(n1 <= n1)
        self.assertFalse(n1 == n2)
        self.assertTrue(n1 == n1)
        self.assertTrue(n1 != n2)
        self.assertFalse(n1 != n1)
        self.assertTrue(n2 > n1)
        self.assertFalse(n1 > n2)
        self.assertTrue(n1 >= n1)
        self.assertTrue(n2 >= n1)
예제 #4
0
 def _safe_import(name, mod, fromlist, level):
     if name in node_map:
         node = node_map[name]
     else:
         node = modulegraph.Node(name)
     node_map[name] = node
     return [node]
예제 #5
0
 def _safe_import_module(self, partname, fqname, parent):
     record.append((partname, fqname, parent))
     if partname == 'raises' or '.raises.' in fqname:
         # FIXME: original _load_tail returned a MissingModule if
         # _import_module did return None. PyInstaller changed this
         # in cae47e4f5b51a94ac3ceb5d093283ba0cc895589
         return self.createNode(modulegraph.MissingModule, fqname)
     return modulegraph.Node(fqname)
예제 #6
0
 def testBasicAttributes(self):
     n = modulegraph.Node("foobar.xyz")
     self.assertEqual(n.identifier, n.graphident)
     self.assertEqual(n.identifier, 'foobar.xyz')
     self.assertEqual(n.filename, None)
     self.assertEqual(n.packagepath, None)
     self.assertEqual(n.code, None)
     self.assertEqual(n._deferred_imports, None)
     self.assertEqual(n._starimported_ignored_module_names, set())
예제 #7
0
    def test_createReference(self):
        graph = modulegraph.ModuleGraph()
        n1 = modulegraph.Node('n1')
        n2 = modulegraph.Node('n2')
        graph.addNode(n1)
        graph.addNode(n2)

        graph.createReference(n1, n2)
        outs, ins = map(list, graph.get_edges(n1))
        self.assertEqual(outs, [n2])
        self.assertEqual(ins, [])
        outs, ins = map(list, graph.get_edges(n2))
        self.assertEqual(outs, [])
        self.assertEqual(ins, [n1])

        e = graph.graph.edge_by_node('n1', 'n2')
        self.assertIsInstance(e, int)
        self.assertEqual(graph.graph.edge_data(e), 'direct')
예제 #8
0
    def test_global_attrs(self):
        n = modulegraph.Node("foobar.xyz")
        self.assertEqual(n._global_attr_names, set())

        self.assertFalse(n.is_global_attr('foo'))
        n.add_global_attr('foo')
        self.assertTrue(n.is_global_attr('foo'))
        n.remove_global_attr_if_found('foo')
        self.assertFalse(n.is_global_attr('foo'))
        # removing then name again must not fail
        n.remove_global_attr_if_found('foo')
예제 #9
0
    def test_scan_code(self):
        mod = modulegraph.Node('root')

        graph = modulegraph.ModuleGraph()
        code = compile('', '<test>', 'exec', 0, False)
        graph.scan_code(code, mod)
        self.assertEqual(list(graph.nodes()), [])

        node_map = {}

        def _safe_import(name, mod, fromlist, level):
            if name in node_map:
                node = node_map[name]
            else:
                node = modulegraph.Node(name)
            node_map[name] = node
            return [node]

        graph = modulegraph.ModuleGraph()
        graph._safe_import_hook = _safe_import

        code = compile(
            textwrap.dedent('''\
            import sys
            import os.path

            def testfunc():
                import shutil
            '''), '<test>', 'exec', 0, False)
        graph.scan_code(code, mod)
        modules = [node.identifier for node in graph.nodes()]
        self.assertEqual(set(node_map), set(['sys', 'os.path', 'shutil']))

        # from module import a, b, c
        # from module import *
        #  both:
        #   -> with/without globals
        #   -> with/without modules in globals (e.g,
        #       from os import * adds dependency to os.path)
        # from .module import a
        # from ..module import a
        #   -> check levels
        # import name
        # import a.b
        #   -> should add dependency to a
        # try to build case where commented out
        # code would behave different than current code
        # (Carbon.SomeMod contains 'import Sibling' seems
        # to cause difference in real code)

        self.fail("actual test needed")
예제 #10
0
    def test_load_tail(self):
        # XXX: This test is dodgy!

        class MockedModuleGraph(modulegraph.ModuleGraph):
            def _safe_import_module(self, partname, fqname, parent):
                record.append((partname, fqname, parent))
                if partname == 'raises' or '.raises.' in fqname:
                    # FIXME: original _load_tail returned a MissingModule if
                    # _import_module did return None. PyInstaller changed this
                    # in cae47e4f5b51a94ac3ceb5d093283ba0cc895589
                    return self.createNode(modulegraph.MissingModule, fqname)
                return modulegraph.Node(fqname)

        graph = MockedModuleGraph()

        record = []
        root = modulegraph.Node('root')
        m = graph._load_tail(root, '')
        self.assertTrue(m is root)
        self.assertEqual(record, [])

        record = []
        root = modulegraph.Node('root')
        m = graph._load_tail(root, 'sub')
        self.assertFalse(m is root)
        self.assertEqual(record, [
            ('sub', 'root.sub', root),
        ])

        record = []
        root = modulegraph.Node('root')
        m = graph._load_tail(root, 'sub.sub1')
        self.assertFalse(m is root)
        node = modulegraph.Node('root.sub')
        self.assertEqual(record, [
            ('sub', 'root.sub', root),
            ('sub1', 'root.sub.sub1', node),
        ])

        record = []
        root = modulegraph.Node('root')
        m = graph._load_tail(root, 'sub.sub1.sub2')
        self.assertFalse(m is root)
        node = modulegraph.Node('root.sub')
        node2 = modulegraph.Node('root.sub.sub1')
        self.assertEqual(record, [
            ('sub', 'root.sub', root),
            ('sub1', 'root.sub.sub1', node),
            ('sub2', 'root.sub.sub1.sub2', node2),
        ])

        n = graph._load_tail(root, 'raises')
        self.assertIsInstance(n, modulegraph.MissingModule)
        self.assertEqual(n.identifier, 'root.raises')

        n = graph._load_tail(root, 'sub.raises')
        self.assertIsInstance(n, modulegraph.MissingModule)
        self.assertEqual(n.identifier, 'root.sub.raises')

        n = graph._load_tail(root, 'sub.raises.sub')
        self.assertIsInstance(n, modulegraph.MissingModule)
        self.assertEqual(n.identifier, 'root.sub.raises.sub')
예제 #11
0
    def test_subclasses(self):
        self.assertIsSubclass(modulegraph.AliasNode, modulegraph.Node)
        self.assertIsSubclass(modulegraph.Script, modulegraph.Node)
        self.assertIsSubclass(modulegraph.BadModule, modulegraph.Node)
        self.assertIsSubclass(modulegraph.ExcludedModule,
                              modulegraph.BadModule)
        self.assertIsSubclass(modulegraph.MissingModule, modulegraph.BadModule)
        self.assertIsSubclass(modulegraph.BaseModule, modulegraph.Node)
        self.assertIsSubclass(modulegraph.BuiltinModule,
                              modulegraph.BaseModule)
        self.assertIsSubclass(modulegraph.SourceModule, modulegraph.BaseModule)
        self.assertIsSubclass(modulegraph.CompiledModule,
                              modulegraph.BaseModule)
        self.assertIsSubclass(modulegraph.Package, modulegraph.BaseModule)
        self.assertIsSubclass(modulegraph.Extension, modulegraph.BaseModule)

        # These classes have no new functionality, check that no code
        # got added:
        self.assertNoMethods(modulegraph.BadModule)
        self.assertNoMethods(modulegraph.ExcludedModule)
        self.assertNoMethods(modulegraph.MissingModule)
        self.assertNoMethods(modulegraph.BuiltinModule)
        self.assertNoMethods(modulegraph.SourceModule)
        self.assertNoMethods(modulegraph.CompiledModule)
        self.assertNoMethods(modulegraph.Package)
        self.assertNoMethods(modulegraph.Extension)

        # AliasNode is basicly a clone of an existing node
        self.assertHasExactMethods(modulegraph.Script, '__init__', 'infoTuple')
        n1 = modulegraph.Node('n1')
        n1.packagepath = ['a', 'b']

        a1 = modulegraph.AliasNode('a1', n1)
        self.assertEqual(a1.graphident, 'a1')
        self.assertEqual(a1.identifier, 'n1')
        self.assertTrue(a1.packagepath is n1.packagepath)

        self.assertIs(a1._deferred_imports, None)
        self.assertIs(a1._global_attr_names, n1._global_attr_names)
        self.assertIs(a1._starimported_ignored_module_names,
                      n1._starimported_ignored_module_names)
        self.assertIs(a1._submodule_basename_to_node,
                      n1._submodule_basename_to_node)

        v = a1.infoTuple()
        self.assertEqual(v, ('a1', 'n1'))

        # Scripts have a filename
        self.assertHasExactMethods(modulegraph.Script, '__init__', 'infoTuple')
        s1 = modulegraph.Script('do_import')
        self.assertEqual(s1.graphident, 'do_import')
        self.assertEqual(s1.identifier, 'do_import')
        self.assertEqual(s1.filename, 'do_import')

        v = s1.infoTuple()
        self.assertEqual(v, ('do_import', ))

        # BaseModule adds some attributes and a custom infotuple
        self.assertHasExactMethods(modulegraph.BaseModule, '__init__',
                                   'infoTuple')
        m1 = modulegraph.BaseModule('foo')
        self.assertEqual(m1.graphident, 'foo')
        self.assertEqual(m1.identifier, 'foo')
        self.assertEqual(m1.filename, None)
        self.assertEqual(m1.packagepath, None)

        m1 = modulegraph.BaseModule('foo', 'bar', ['a'])
        self.assertEqual(m1.graphident, 'foo')
        self.assertEqual(m1.identifier, 'foo')
        self.assertEqual(m1.filename, 'bar')
        self.assertEqual(m1.packagepath, ['a'])
예제 #12
0
 def test_infoTuple(self):
     n = modulegraph.Node('n1')
     self.assertEqual(n.infoTuple(), ('n1', ))