483
483
self.addCleanup(sys.path.remove, base_path)
485
485
original_import = __import__
486
def instrumented_import(mod, scope1, scope2, fromlist):
487
self.actions.append(('import', mod, fromlist))
488
return original_import(mod, scope1, scope2, fromlist)
486
def instrumented_import(mod, scope1, scope2, fromlist, level):
487
self.actions.append(('import', mod, fromlist, level))
488
return original_import(mod, scope1, scope2, fromlist, level)
490
490
__builtins__['__import__'] = original_import
491
491
self.addCleanup(cleanup)
561
561
"""Test that a real import of these modules works"""
562
562
sub_mod_path = '.'.join([self.root_name, self.sub_name,
563
563
self.submoda_name])
564
root = __import__(sub_mod_path, globals(), locals(), [])
564
root = __import__(sub_mod_path, globals(), locals(), [], 0)
565
565
self.assertEqual(1, root.var1)
566
566
self.assertEqual(3, getattr(root, self.sub_name).var3)
567
567
self.assertEqual(4, getattr(getattr(root, self.sub_name),
568
568
self.submoda_name).var4)
570
570
mod_path = '.'.join([self.root_name, self.mod_name])
571
root = __import__(mod_path, globals(), locals(), [])
571
root = __import__(mod_path, globals(), locals(), [], 0)
572
572
self.assertEqual(2, getattr(root, self.mod_name).var2)
574
self.assertEqual([('import', sub_mod_path, []),
575
('import', mod_path, []),
574
self.assertEqual([('import', sub_mod_path, [], 0),
575
('import', mod_path, [], 0),
601
601
self.assertEqual([('__getattribute__', 'var1'),
603
603
('_import', 'root1'),
604
('import', self.root_name, []),
604
('import', self.root_name, [], 0),
607
607
def test_import_mod(self):
627
627
self.assertEqual([('__getattribute__', 'var2'),
629
629
('_import', 'mod1'),
630
('import', mod_path, []),
630
('import', mod_path, [], 0),
633
633
def test_import_mod_from_root(self):
652
652
self.assertEqual([('__getattribute__', 'var2'),
654
654
('_import', 'mod2'),
655
('import', self.root_name, [self.mod_name]),
655
('import', self.root_name, [self.mod_name], 0),
658
658
def test_import_root_and_mod(self):
685
685
self.assertEqual([('__getattribute__', 'var1'),
687
687
('_import', 'root3'),
688
('import', self.root_name, []),
688
('import', self.root_name, [], 0),
689
689
('__getattribute__', 'var2'),
691
691
('_import', 'mod3'),
692
('import', mod_path, []),
692
('import', mod_path, [], 0),
695
695
def test_import_root_and_root_mod(self):
729
729
self.assertEqual([('__getattribute__', 'mod4'),
731
731
('_import', 'root4'),
732
('import', self.root_name, []),
732
('import', self.root_name, [], 0),
733
733
('__getattribute__', 'var2'),
735
735
('_import', 'mod4'),
736
('import', mod_path, []),
736
('import', mod_path, [], 0),
739
739
def test_import_root_sub_submod(self):
794
794
self.assertEqual([('__getattribute__', 'mod5'),
796
796
('_import', 'root5'),
797
('import', self.root_name, []),
797
('import', self.root_name, [], 0),
798
798
('__getattribute__', 'submoda5'),
800
800
('_import', 'sub5'),
801
('import', sub_path, []),
801
('import', sub_path, [], 0),
802
802
('__getattribute__', 'var2'),
804
804
('_import', 'mod5'),
805
('import', mod_path, []),
805
('import', mod_path, [], 0),
806
806
('__getattribute__', 'var4'),
808
808
('_import', 'submoda5'),
809
('import', submoda_path, []),
809
('import', submoda_path, [], 0),
810
810
('__getattribute__', 'var5'),
812
812
('_import', 'submodb5'),
813
('import', submodb_path, []),
813
('import', submodb_path, [], 0),
1106
1106
self.assertEqual([('__getattribute__', 'var1'),
1108
1108
('_import', 'root6'),
1109
('import', self.root_name, []),
1109
('import', self.root_name, [], 0),
1110
1110
], self.actions)
1112
1112
def test_import_deep(self):
1142
1142
self.assertEqual([('__getattribute__', 'var4'),
1144
1144
('_import', 'submoda7'),
1145
('import', submoda_path, []),
1145
('import', submoda_path, [], 0),
1146
1146
], self.actions)
1148
1148
def test_lazy_import(self):
1167
1167
self.assertEqual([('__getattribute__', 'var1'),
1169
1169
('_import', 'root8'),
1170
('import', self.root_name, []),
1170
('import', self.root_name, [], 0),
1171
1171
], self.actions)