~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_lazy_import.py

  • Committer: Patch Queue Manager
  • Date: 2011-12-19 13:30:31 UTC
  • mfrom: (6355.2.5 absolute-lazy-import)
  • Revision ID: pqm@pqm.ubuntu.com-20111219133031-97tc08321g9zz6dd
(jelmer) Only allow absolute imports using lazy_import. (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
483
483
        self.addCleanup(sys.path.remove, base_path)
484
484
 
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)
489
489
        def cleanup():
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)
569
569
 
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)
573
573
 
574
 
        self.assertEqual([('import', sub_mod_path, []),
575
 
                          ('import', mod_path, []),
 
574
        self.assertEqual([('import', sub_mod_path, [], 0),
 
575
                          ('import', mod_path, [], 0),
576
576
                         ], self.actions)
577
577
 
578
578
 
601
601
        self.assertEqual([('__getattribute__', 'var1'),
602
602
                          '_replace',
603
603
                          ('_import', 'root1'),
604
 
                          ('import', self.root_name, []),
 
604
                          ('import', self.root_name, [], 0),
605
605
                         ], self.actions)
606
606
 
607
607
    def test_import_mod(self):
627
627
        self.assertEqual([('__getattribute__', 'var2'),
628
628
                          '_replace',
629
629
                          ('_import', 'mod1'),
630
 
                          ('import', mod_path, []),
 
630
                          ('import', mod_path, [], 0),
631
631
                         ], self.actions)
632
632
 
633
633
    def test_import_mod_from_root(self):
652
652
        self.assertEqual([('__getattribute__', 'var2'),
653
653
                          '_replace',
654
654
                          ('_import', 'mod2'),
655
 
                          ('import', self.root_name, [self.mod_name]),
 
655
                          ('import', self.root_name, [self.mod_name], 0),
656
656
                         ], self.actions)
657
657
 
658
658
    def test_import_root_and_mod(self):
685
685
        self.assertEqual([('__getattribute__', 'var1'),
686
686
                          '_replace',
687
687
                          ('_import', 'root3'),
688
 
                          ('import', self.root_name, []),
 
688
                          ('import', self.root_name, [], 0),
689
689
                          ('__getattribute__', 'var2'),
690
690
                          '_replace',
691
691
                          ('_import', 'mod3'),
692
 
                          ('import', mod_path, []),
 
692
                          ('import', mod_path, [], 0),
693
693
                         ], self.actions)
694
694
 
695
695
    def test_import_root_and_root_mod(self):
729
729
        self.assertEqual([('__getattribute__', 'mod4'),
730
730
                          '_replace',
731
731
                          ('_import', 'root4'),
732
 
                          ('import', self.root_name, []),
 
732
                          ('import', self.root_name, [], 0),
733
733
                          ('__getattribute__', 'var2'),
734
734
                          '_replace',
735
735
                          ('_import', 'mod4'),
736
 
                          ('import', mod_path, []),
 
736
                          ('import', mod_path, [], 0),
737
737
                         ], self.actions)
738
738
 
739
739
    def test_import_root_sub_submod(self):
794
794
        self.assertEqual([('__getattribute__', 'mod5'),
795
795
                          '_replace',
796
796
                          ('_import', 'root5'),
797
 
                          ('import', self.root_name, []),
 
797
                          ('import', self.root_name, [], 0),
798
798
                          ('__getattribute__', 'submoda5'),
799
799
                          '_replace',
800
800
                          ('_import', 'sub5'),
801
 
                          ('import', sub_path, []),
 
801
                          ('import', sub_path, [], 0),
802
802
                          ('__getattribute__', 'var2'),
803
803
                          '_replace',
804
804
                          ('_import', 'mod5'),
805
 
                          ('import', mod_path, []),
 
805
                          ('import', mod_path, [], 0),
806
806
                          ('__getattribute__', 'var4'),
807
807
                          '_replace',
808
808
                          ('_import', 'submoda5'),
809
 
                          ('import', submoda_path, []),
 
809
                          ('import', submoda_path, [], 0),
810
810
                          ('__getattribute__', 'var5'),
811
811
                          '_replace',
812
812
                          ('_import', 'submodb5'),
813
 
                          ('import', submodb_path, []),
 
813
                          ('import', submodb_path, [], 0),
814
814
                         ], self.actions)
815
815
 
816
816
 
1106
1106
        self.assertEqual([('__getattribute__', 'var1'),
1107
1107
                          '_replace',
1108
1108
                          ('_import', 'root6'),
1109
 
                          ('import', self.root_name, []),
 
1109
                          ('import', self.root_name, [], 0),
1110
1110
                         ], self.actions)
1111
1111
 
1112
1112
    def test_import_deep(self):
1142
1142
        self.assertEqual([('__getattribute__', 'var4'),
1143
1143
                          '_replace',
1144
1144
                          ('_import', 'submoda7'),
1145
 
                          ('import', submoda_path, []),
 
1145
                          ('import', submoda_path, [], 0),
1146
1146
                         ], self.actions)
1147
1147
 
1148
1148
    def test_lazy_import(self):
1167
1167
        self.assertEqual([('__getattribute__', 'var1'),
1168
1168
                          '_replace',
1169
1169
                          ('_import', 'root8'),
1170
 
                          ('import', self.root_name, []),
 
1170
                          ('import', self.root_name, [], 0),
1171
1171
                         ], self.actions)
1172
1172
 
1173
1173