/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test_lazy_import.py

  • Committer: Jelmer Vernooij
  • Date: 2019-03-04 00:16:27 UTC
  • mfrom: (7293 work)
  • mto: This revision was merged to the branch mainline in revision 7318.
  • Revision ID: jelmer@jelmer.uk-20190304001627-v6u7o6pf97tukhek
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
98
98
        # These tests assume we will not be proxying, so make sure proxying is
99
99
        # disabled.
100
100
        orig_proxy = lazy_import.ScopeReplacer._should_proxy
 
101
 
101
102
        def restore():
102
103
            lazy_import.ScopeReplacer._should_proxy = orig_proxy
103
104
        lazy_import.ScopeReplacer._should_proxy = False
140
141
                          'init',
141
142
                          ('foo', 1),
142
143
                          ('foo', 2),
143
 
                         ], actions)
 
144
                          ], actions)
144
145
 
145
146
    def test_setattr_replaces(self):
146
147
        """ScopeReplacer can create an instance in local scope.
150
151
        """
151
152
        actions = []
152
153
        TestClass.use_actions(actions)
 
154
 
153
155
        def factory(replacer, scope, name):
154
156
            return TestClass()
155
157
        try:
235
237
        self.assertEqual(test_class1, TestClass)
236
238
        self.assertEqual([('__getattribute__', 'class_member'),
237
239
                          'factory',
238
 
                         ], actions)
 
240
                          ], actions)
239
241
 
240
242
    def test_call_class(self):
241
243
        actions = []
265
267
        self.assertEqual([('__call__', (), {}),
266
268
                          'factory',
267
269
                          'init',
268
 
                         ], actions)
 
270
                          ], actions)
269
271
 
270
272
    def test_call_func(self):
271
273
        actions = []
294
296
        self.assertIs(test_func1, func)
295
297
 
296
298
        self.assertEqual((1, 2, '3'), val)
297
 
        self.assertEqual([('__call__', (1, 2), {'c':'3'}),
 
299
        self.assertEqual([('__call__', (1, 2), {'c': '3'}),
298
300
                          'factory',
299
301
                          'func',
300
 
                         ], actions)
 
302
                          ], actions)
301
303
 
302
304
    def test_other_variable(self):
303
305
        """Test when a ScopeReplacer is assigned to another variable.
362
364
                          ('foo', 2),
363
365
                          ('foo', 3),
364
366
                          ('__getattribute__', 'foo'),
365
 
                         ], actions)
 
367
                          ], actions)
366
368
 
367
369
    def test_enable_proxying(self):
368
370
        """Test that we can allow ScopeReplacer to proxy."""
420
422
                          ('foo', 3),
421
423
                          ('__getattribute__', 'foo'),
422
424
                          ('foo', 4),
423
 
                         ], actions)
 
425
                          ], actions)
424
426
 
425
427
    def test_replacing_from_own_scope_fails(self):
426
428
        """If a ScopeReplacer tries to replace itself a nice error is given"""
526
528
    def test_basic_import(self):
527
529
        """Test that a real import of these modules works"""
528
530
        sub_mod_path = '.'.join([self.root_name, self.sub_name,
529
 
                                  self.submoda_name])
 
531
                                 self.submoda_name])
530
532
        root = lazy_import._builtin_import(sub_mod_path, {}, {}, [], 0)
531
533
        self.assertEqual(1, root.var1)
532
534
        self.assertEqual(3, getattr(root, self.sub_name).var3)
539
541
 
540
542
        self.assertEqual([('import', sub_mod_path, [], 0),
541
543
                          ('import', mod_path, [], 0),
542
 
                         ], self.actions)
 
544
                          ], self.actions)
543
545
 
544
546
 
545
547
class TestImportReplacer(ImportReplacerHelper):
567
569
        self.assertEqual([('__getattribute__', 'var1'),
568
570
                          ('_import', 'root1'),
569
571
                          ('import', self.root_name, [], 0),
570
 
                         ], self.actions)
 
572
                          ], self.actions)
571
573
 
572
574
    def test_import_mod(self):
573
575
        """Test 'import root-XXX.mod-XXX as mod2'"""
592
594
        self.assertEqual([('__getattribute__', 'var2'),
593
595
                          ('_import', 'mod1'),
594
596
                          ('import', mod_path, [], 0),
595
 
                         ], self.actions)
 
597
                          ], self.actions)
596
598
 
597
599
    def test_import_mod_from_root(self):
598
600
        """Test 'from root-XXX import mod-XXX as mod2'"""
616
618
        self.assertEqual([('__getattribute__', 'var2'),
617
619
                          ('_import', 'mod2'),
618
620
                          ('import', self.root_name, [self.mod_name], 0),
619
 
                         ], self.actions)
 
621
                          ], self.actions)
620
622
 
621
623
    def test_import_root_and_mod(self):
622
624
        """Test 'import root-XXX.mod-XXX' remapping both to root3.mod3"""
629
631
            self.fail('root3 was not supposed to exist yet')
630
632
 
631
633
        InstrumentedImportReplacer(scope=globals(),
632
 
            name='root3', module_path=[self.root_name], member=None,
633
 
            children={'mod3':([self.root_name, self.mod_name], None, {})})
 
634
                                   name='root3', module_path=[self.root_name], member=None,
 
635
                                   children={'mod3': ([self.root_name, self.mod_name], None, {})})
634
636
 
635
637
        # So 'root3' should be a lazy import
636
638
        # and once it is imported, mod3 should also be lazy until
651
653
                          ('__getattribute__', 'var2'),
652
654
                          ('_import', 'mod3'),
653
655
                          ('import', mod_path, [], 0),
654
 
                         ], self.actions)
 
656
                          ], self.actions)
655
657
 
656
658
    def test_import_root_and_root_mod(self):
657
659
        """Test that 'import root, root.mod' can be done.
668
670
            self.fail('root4 was not supposed to exist yet')
669
671
 
670
672
        InstrumentedImportReplacer(scope=globals(),
671
 
            name='root4', module_path=[self.root_name], member=None,
672
 
            children={})
 
673
                                   name='root4', module_path=[self.root_name], member=None,
 
674
                                   children={})
673
675
 
674
676
        # So 'root4' should be a lazy import
675
677
        self.assertEqual(InstrumentedImportReplacer,
693
695
                          ('__getattribute__', 'var2'),
694
696
                          ('_import', 'mod4'),
695
697
                          ('import', mod_path, [], 0),
696
 
                         ], self.actions)
 
698
                          ], self.actions)
697
699
 
698
700
    def test_import_root_sub_submod(self):
699
701
        """Test import root.mod, root.sub.submoda, root.sub.submodb
711
713
            self.fail('root5 was not supposed to exist yet')
712
714
 
713
715
        InstrumentedImportReplacer(scope=globals(),
714
 
            name='root5', module_path=[self.root_name], member=None,
715
 
            children={'mod5': ([self.root_name, self.mod_name], None, {}),
716
 
                      'sub5': ([self.root_name, self.sub_name], None,
717
 
                            {'submoda5':([self.root_name, self.sub_name,
718
 
                                         self.submoda_name], None, {}),
719
 
                             'submodb5':([self.root_name, self.sub_name,
720
 
                                          self.submodb_name], None, {})
721
 
                            }),
722
 
                     })
 
716
                                   name='root5', module_path=[self.root_name], member=None,
 
717
                                   children={'mod5': ([self.root_name, self.mod_name], None, {}),
 
718
                                             'sub5': ([self.root_name, self.sub_name], None,
 
719
                                                      {'submoda5': ([self.root_name, self.sub_name,
 
720
                                                                     self.submoda_name], None, {}),
 
721
                                                       'submodb5': ([self.root_name, self.sub_name,
 
722
                                                                     self.submodb_name], None, {})
 
723
                                                       }),
 
724
                                             })
723
725
 
724
726
        # So 'root5' should be a lazy import
725
727
        self.assertEqual(InstrumentedImportReplacer,
735
737
        # Accessing root5.sub5.submoda5 should import sub5, but not either
736
738
        # of the sub objects (they should be available as lazy objects
737
739
        self.assertEqual(InstrumentedImportReplacer,
738
 
                     object.__getattribute__(root5.sub5.submoda5, '__class__'))
 
740
                         object.__getattribute__(root5.sub5.submoda5, '__class__'))
739
741
        self.assertEqual(InstrumentedImportReplacer,
740
 
                     object.__getattribute__(root5.sub5.submodb5, '__class__'))
 
742
                         object.__getattribute__(root5.sub5.submodb5, '__class__'))
741
743
 
742
744
        # This should import mod5
743
745
        self.assertEqual(2, root5.mod5.var2)
765
767
                          ('__getattribute__', 'var5'),
766
768
                          ('_import', 'submodb5'),
767
769
                          ('import', submodb_path, [], 0),
768
 
                         ], self.actions)
 
770
                          ], self.actions)
769
771
 
770
772
 
771
773
class TestConvertImportToMap(TestCase):
782
784
 
783
785
    def test_import_one(self):
784
786
        self.check({'one': (['one'], None, {}),
785
 
                   }, ['import one'])
 
787
                    }, ['import one'])
786
788
 
787
789
    def test_import_one_two(self):
788
790
        one_two_map = {'one': (['one'], None,
789
 
                              {'two': (['one', 'two'], None, {}),
790
 
                              }),
791
 
                      }
 
791
                               {'two': (['one', 'two'], None, {}),
 
792
                                }),
 
793
                       }
792
794
        self.check(one_two_map, ['import one.two'])
793
795
        self.check(one_two_map, ['import one, one.two'])
794
796
        self.check(one_two_map, ['import one', 'import one.two'])
797
799
    def test_import_one_two_three(self):
798
800
        one_two_three_map = {
799
801
            'one': (['one'], None,
800
 
                   {'two': (['one', 'two'], None,
801
 
                           {'three': (['one', 'two', 'three'], None, {}),
802
 
                           }),
803
 
                   }),
 
802
                    {'two': (['one', 'two'], None,
 
803
                             {'three': (['one', 'two', 'three'], None, {}),
 
804
                              }),
 
805
                     }),
804
806
        }
805
807
        self.check(one_two_three_map, ['import one.two.three'])
806
808
        self.check(one_two_three_map, ['import one, one.two.three'])
807
809
        self.check(one_two_three_map, ['import one',
808
 
                                              'import one.two.three'])
 
810
                                       'import one.two.three'])
809
811
        self.check(one_two_three_map, ['import one.two.three',
810
 
                                              'import one'])
 
812
                                       'import one'])
811
813
 
812
814
    def test_import_one_as_x(self):
813
815
        self.check({'x': (['one'], None, {}),
814
 
                          }, ['import one as x'])
 
816
                    }, ['import one as x'])
815
817
 
816
818
    def test_import_one_two_as_x(self):
817
819
        self.check({'x': (['one', 'two'], None, {}),
818
 
                   }, ['import one.two as x'])
 
820
                    }, ['import one.two as x'])
819
821
 
820
822
    def test_import_mixed(self):
821
823
        mixed = {'x': (['one', 'two'], None, {}),
822
824
                 'one': (['one'], None,
823
 
                       {'two': (['one', 'two'], None, {}),
824
 
                       }),
825
 
                }
 
825
                         {'two': (['one', 'two'], None, {}),
 
826
                          }),
 
827
                 }
826
828
        self.check(mixed, ['import one.two as x, one.two'])
827
829
        self.check(mixed, ['import one.two as x', 'import one.two'])
828
830
        self.check(mixed, ['import one.two', 'import one.two as x'])
829
831
 
830
832
    def test_import_with_as(self):
831
 
        self.check({'fast':(['fast'], None, {})}, ['import fast'])
 
833
        self.check({'fast': (['fast'], None, {})}, ['import fast'])
832
834
 
833
835
 
834
836
class TestFromToMap(TestCase):
843
845
                         ' %s != %s' % (from_strings, expected, proc.imports))
844
846
 
845
847
    def test_from_one_import_two(self):
846
 
        self.check_result({'two':(['one'], 'two', {})},
 
848
        self.check_result({'two': (['one'], 'two', {})},
847
849
                          ['from one import two'])
848
850
 
849
851
    def test_from_one_import_two_as_three(self):
850
 
        self.check_result({'three':(['one'], 'two', {})},
 
852
        self.check_result({'three': (['one'], 'two', {})},
851
853
                          ['from one import two as three'])
852
854
 
853
855
    def test_from_one_import_two_three(self):
854
856
        two_three_map = {'two': (['one'], 'two', {}),
855
857
                         'three': (['one'], 'three', {}),
856
 
                        }
 
858
                         }
857
859
        self.check_result(two_three_map,
858
860
                          ['from one import two, three'])
859
861
        self.check_result(two_three_map,
861
863
                           'from one import three'])
862
864
 
863
865
    def test_from_one_two_import_three(self):
864
 
        self.check_result({'three':(['one', 'two'], 'three', {})},
 
866
        self.check_result({'three': (['one', 'two'], 'three', {})},
865
867
                          ['from one.two import three'])
866
868
 
867
869
 
892
894
        self.check(['from one import two'], 'from one import two')
893
895
        self.check(['from one import two'], '\nfrom one import two\n\n')
894
896
        self.check(['from one import two'], '\nfrom one import (two)\n')
895
 
        self.check(['from one import  two '], '\nfrom one import (\n\ttwo\n)\n')
 
897
        self.check(['from one import  two '],
 
898
                   '\nfrom one import (\n\ttwo\n)\n')
896
899
 
897
900
    def test_multiple(self):
898
901
        self.check(['import one', 'import two, three', 'from one import four'],
930
933
                         % (text, expected, proc.imports))
931
934
 
932
935
    def test_import_one(self):
933
 
        exp = {'one':(['one'], None, {})}
 
936
        exp = {'one': (['one'], None, {})}
934
937
        self.check(exp, 'import one')
935
938
        self.check(exp, '\nimport one\n')
936
939
 
937
940
    def test_import_one_two(self):
938
941
        exp = {'one': (['one'], None,
939
 
                      {'two': (['one', 'two'], None, {}),
940
 
                      }),
941
 
              }
 
942
                       {'two': (['one', 'two'], None, {}),
 
943
                        }),
 
944
               }
942
945
        self.check(exp, 'import one.two')
943
946
        self.check(exp, 'import one, one.two')
944
947
        self.check(exp, 'import one\nimport one.two')
945
948
 
946
949
    def test_import_as(self):
947
 
        exp = {'two':(['one'], None, {})}
 
950
        exp = {'two': (['one'], None, {})}
948
951
        self.check(exp, 'import one as two')
949
952
 
950
953
    def test_import_many(self):
951
954
        exp = {'one': (['one'], None,
952
 
                      {'two': (['one', 'two'], None,
953
 
                              {'three': (['one', 'two', 'three'], None, {}),
954
 
                              }),
955
 
                       'four': (['one', 'four'], None, {}),
956
 
                      }),
 
955
                       {'two': (['one', 'two'], None,
 
956
                                {'three': (['one', 'two', 'three'], None, {}),
 
957
                                 }),
 
958
                        'four': (['one', 'four'], None, {}),
 
959
                        }),
957
960
               'five': (['one', 'five'], None, {}),
958
 
              }
 
961
               }
959
962
        self.check(exp, 'import one.two.three, one.four, one.five as five')
960
963
        self.check(exp, 'import one.five as five\n'
961
964
                        'import one\n'
963
966
                        'import one.four\n')
964
967
 
965
968
    def test_from_one_import_two(self):
966
 
        exp = {'two':(['one'], 'two', {})}
 
969
        exp = {'two': (['one'], 'two', {})}
967
970
        self.check(exp, 'from one import two\n')
968
971
        self.check(exp, 'from one import (\n'
969
972
                        '    two,\n'
970
973
                        '    )\n')
971
974
 
972
975
    def test_from_one_import_two(self):
973
 
        exp = {'two':(['one'], 'two', {})}
 
976
        exp = {'two': (['one'], 'two', {})}
974
977
        self.check(exp, 'from one import two\n')
975
978
        self.check(exp, 'from one import (two)\n')
976
979
        self.check(exp, 'from one import (two,)\n')
983
986
        exp = {'two': (['one'], 'two', {}),
984
987
               'three': (['one', 'two'], 'three', {}),
985
988
               'five': (['one', 'two'], 'four', {}),
986
 
              }
 
989
               }
987
990
        self.check(exp, 'from one import two\n'
988
991
                        'from one.two import three, four as five\n')
989
992
        self.check(exp, 'from one import two\n'
997
1000
               'three': (['one', 'two'], 'three', {}),
998
1001
               'five': (['one', 'two'], 'four', {}),
999
1002
               'one': (['one'], None,
1000
 
                      {'two': (['one', 'two'], None, {}),
1001
 
                      }),
1002
 
              }
 
1003
                       {'two': (['one', 'two'], None, {}),
 
1004
                        }),
 
1005
               }
1003
1006
        self.check(exp, 'from one import two\n'
1004
1007
                        'from one.two import three, four as five\n'
1005
1008
                        'import one.two')
1051
1054
        try:
1052
1055
            root6
1053
1056
        except NameError:
1054
 
            pass # root6 should not be defined yet
 
1057
            pass  # root6 should not be defined yet
1055
1058
        else:
1056
1059
            self.fail('root6 was not supposed to exist yet')
1057
1060
 
1069
1072
        self.assertEqual([('__getattribute__', 'var1'),
1070
1073
                          ('_import', 'root6'),
1071
1074
                          ('import', self.root_name, [], 0),
1072
 
                         ], self.actions)
 
1075
                          ], self.actions)
1073
1076
 
1074
1077
    def test_import_deep(self):
1075
1078
        """Test import root.mod, root.sub.submoda, root.sub.submodb
1081
1084
        try:
1082
1085
            submoda7
1083
1086
        except NameError:
1084
 
            pass # submoda7 should not be defined yet
 
1087
            pass  # submoda7 should not be defined yet
1085
1088
        else:
1086
1089
            self.fail('submoda7 was not supposed to exist yet')
1087
1090
 
1104
1107
        self.assertEqual([('__getattribute__', 'var4'),
1105
1108
                          ('_import', 'submoda7'),
1106
1109
                          ('import', submoda_path, [], 0),
1107
 
                         ], self.actions)
 
1110
                          ], self.actions)
1108
1111
 
1109
1112
    def test_lazy_import(self):
1110
1113
        """Smoke test that lazy_import() does the right thing"""
1111
1114
        try:
1112
1115
            root8
1113
1116
        except NameError:
1114
 
            pass # root8 should not be defined yet
 
1117
            pass  # root8 should not be defined yet
1115
1118
        else:
1116
1119
            self.fail('root8 was not supposed to exist yet')
1117
1120
        lazy_import.lazy_import(globals(),
1128
1131
        self.assertEqual([('__getattribute__', 'var1'),
1129
1132
                          ('_import', 'root8'),
1130
1133
                          ('import', self.root_name, [], 0),
1131
 
                         ], self.actions)
 
1134
                          ], self.actions)
1132
1135
 
1133
1136
 
1134
1137
class TestScopeReplacerReentrance(TestCase):
1154
1157
        filename = re.sub(r'\.py[co]$', '.py', filename)
1155
1158
        function_name = code.co_name
1156
1159
        # If we're executing a line of code from the right module...
1157
 
        if (filename.endswith('lazy_import.py') and
1158
 
            function_name == self.method_to_trace):
 
1160
        if (filename.endswith('lazy_import.py')
 
1161
                and function_name == self.method_to_trace):
1159
1162
            # We don't need to trace any more.
1160
1163
            sys.settrace(None)
1161
1164
            # Run another racer.  This one will "win" the race.
1167
1170
        self.racer = racer
1168
1171
        self.method_to_trace = method_to_trace
1169
1172
        sys.settrace(self.tracer)
1170
 
        self.racer() # Should not raise any exception
 
1173
        self.racer()  # Should not raise any exception
1171
1174
        # Make sure the tracer actually found the code it was
1172
1175
        # looking for.  If not, maybe the code was refactored in
1173
1176
        # such a way that these tests aren't needed any more.