294
296
self.assertIs(test_func1, func)
296
298
self.assertEqual((1, 2, '3'), val)
297
self.assertEqual([('__call__', (1, 2), {'c':'3'}),
299
self.assertEqual([('__call__', (1, 2), {'c': '3'}),
302
304
def test_other_variable(self):
303
305
"""Test when a ScopeReplacer is assigned to another variable.
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,
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)
629
631
self.fail('root3 was not supposed to exist yet')
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, {})})
635
637
# So 'root3' should be a lazy import
636
638
# and once it is imported, mod3 should also be lazy until
668
670
self.fail('root4 was not supposed to exist yet')
670
672
InstrumentedImportReplacer(scope=globals(),
671
name='root4', module_path=[self.root_name], member=None,
673
name='root4', module_path=[self.root_name], member=None,
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),
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')
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, {})
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, {})
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__'))
742
744
# This should import mod5
743
745
self.assertEqual(2, root5.mod5.var2)
783
785
def test_import_one(self):
784
786
self.check({'one': (['one'], None, {}),
787
789
def test_import_one_two(self):
788
790
one_two_map = {'one': (['one'], None,
789
{'two': (['one', 'two'], None, {}),
791
{'two': (['one', 'two'], None, {}),
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
{'two': (['one', 'two'], None,
803
{'three': (['one', 'two', 'three'], None, {}),
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',
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'])
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'])
820
822
def test_import_mixed(self):
821
823
mixed = {'x': (['one', 'two'], None, {}),
822
824
'one': (['one'], None,
823
{'two': (['one', 'two'], None, {}),
825
{'two': (['one', 'two'], None, {}),
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'])
830
832
def test_import_with_as(self):
831
self.check({'fast':(['fast'], None, {})}, ['import fast'])
833
self.check({'fast': (['fast'], None, {})}, ['import fast'])
834
836
class TestFromToMap(TestCase):
843
845
' %s != %s' % (from_strings, expected, proc.imports))
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'])
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'])
853
855
def test_from_one_import_two_three(self):
854
856
two_three_map = {'two': (['one'], 'two', {}),
855
857
'three': (['one'], 'three', {}),
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'])
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'])
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')
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))
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')
937
940
def test_import_one_two(self):
938
941
exp = {'one': (['one'], None,
939
{'two': (['one', 'two'], None, {}),
942
{'two': (['one', 'two'], None, {}),
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')
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')
950
953
def test_import_many(self):
951
954
exp = {'one': (['one'], None,
952
{'two': (['one', 'two'], None,
953
{'three': (['one', 'two', 'three'], None, {}),
955
'four': (['one', 'four'], None, {}),
955
{'two': (['one', 'two'], None,
956
{'three': (['one', 'two', 'three'], None, {}),
958
'four': (['one', 'four'], None, {}),
957
960
'five': (['one', 'five'], None, {}),
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'
963
966
'import one.four\n')
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'
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', {}),
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, {}),
1003
{'two': (['one', 'two'], None, {}),
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')
1069
1072
self.assertEqual([('__getattribute__', 'var1'),
1070
1073
('_import', 'root6'),
1071
1074
('import', self.root_name, [], 0),
1074
1077
def test_import_deep(self):
1075
1078
"""Test import root.mod, root.sub.submoda, root.sub.submodb
1104
1107
self.assertEqual([('__getattribute__', 'var4'),
1105
1108
('_import', 'submoda7'),
1106
1109
('import', submoda_path, [], 0),
1109
1112
def test_lazy_import(self):
1110
1113
"""Smoke test that lazy_import() does the right thing"""
1113
1116
except NameError:
1114
pass # root8 should not be defined yet
1117
pass # root8 should not be defined yet
1116
1119
self.fail('root8 was not supposed to exist yet')
1117
1120
lazy_import.lazy_import(globals(),
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.