~bzr-pqm/bzr/bzr.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
# Copyright (C) 2006-2011 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

"""Test the lazy_import functionality."""

import linecache
import os
import re
import sys

from bzrlib import (
    errors,
    lazy_import,
    osutils,
    )
from bzrlib.tests import (
    TestCase,
    TestCaseInTempDir,
    )


class InstrumentedReplacer(lazy_import.ScopeReplacer):
    """Track what actions are done"""

    @staticmethod
    def use_actions(actions):
        InstrumentedReplacer.actions = actions

    def __getattribute__(self, attr):
        InstrumentedReplacer.actions.append(('__getattribute__', attr))
        return lazy_import.ScopeReplacer.__getattribute__(self, attr)

    def __call__(self, *args, **kwargs):
        InstrumentedReplacer.actions.append(('__call__', args, kwargs))
        return lazy_import.ScopeReplacer.__call__(self, *args, **kwargs)


class InstrumentedImportReplacer(lazy_import.ImportReplacer):

    @staticmethod
    def use_actions(actions):
        InstrumentedImportReplacer.actions = actions

    def _import(self, scope, name):
        InstrumentedImportReplacer.actions.append(('_import', name))
        return lazy_import.ImportReplacer._import(self, scope, name)

    def __getattribute__(self, attr):
        InstrumentedImportReplacer.actions.append(('__getattribute__', attr))
        return lazy_import.ScopeReplacer.__getattribute__(self, attr)

    def __call__(self, *args, **kwargs):
        InstrumentedImportReplacer.actions.append(('__call__', args, kwargs))
        return lazy_import.ScopeReplacer.__call__(self, *args, **kwargs)


class TestClass(object):
    """Just a simple test class instrumented for the test cases"""

    class_member = 'class_member'

    @staticmethod
    def use_actions(actions):
        TestClass.actions = actions

    def __init__(self):
        TestClass.actions.append('init')

    def foo(self, x):
        TestClass.actions.append(('foo', x))
        return 'foo'


class TestScopeReplacer(TestCase):
    """Test the ability of the replacer to put itself into the correct scope.

    In these tests we use the global scope, because we cannot replace
    variables in the local scope. This means that we need to be careful
    and not have the replacing objects use the same name, or we would
    get collisions.
    """

    def setUp(self):
        TestCase.setUp(self)
        # These tests assume we will not be proxying, so make sure proxying is
        # disabled.
        orig_proxy = lazy_import.ScopeReplacer._should_proxy
        def restore():
            lazy_import.ScopeReplacer._should_proxy = orig_proxy
        lazy_import.ScopeReplacer._should_proxy = False

    def test_object(self):
        """ScopeReplacer can create an instance in local scope.

        An object should appear in globals() by constructing a ScopeReplacer,
        and it will be replaced with the real object upon the first request.
        """
        actions = []
        InstrumentedReplacer.use_actions(actions)
        TestClass.use_actions(actions)

        def factory(replacer, scope, name):
            actions.append('factory')
            return TestClass()

        try:
            test_obj1
        except NameError:
            # test_obj1 shouldn't exist yet
            pass
        else:
            self.fail('test_obj1 was not supposed to exist yet')

        InstrumentedReplacer(scope=globals(), name='test_obj1',
                             factory=factory)

        # We can't use isinstance() because that uses test_obj1.__class__
        # and that goes through __getattribute__ which would activate
        # the replacement
        self.assertEqual(InstrumentedReplacer,
                         object.__getattribute__(test_obj1, '__class__'))
        self.assertEqual('foo', test_obj1.foo(1))
        self.assertIsInstance(test_obj1, TestClass)
        self.assertEqual('foo', test_obj1.foo(2))
        self.assertEqual([('__getattribute__', 'foo'),
                          'factory',
                          'init',
                          ('foo', 1),
                          ('foo', 2),
                         ], actions)

    def test_setattr_replaces(self):
        """ScopeReplacer can create an instance in local scope.

        An object should appear in globals() by constructing a ScopeReplacer,
        and it will be replaced with the real object upon the first request.
        """
        actions = []
        TestClass.use_actions(actions)
        def factory(replacer, scope, name):
            return TestClass()
        try:
            test_obj6
        except NameError:
            # test_obj6 shouldn't exist yet
            pass
        else:
            self.fail('test_obj6 was not supposed to exist yet')

        lazy_import.ScopeReplacer(scope=globals(), name='test_obj6',
                                  factory=factory)

        # We can't use isinstance() because that uses test_obj6.__class__
        # and that goes through __getattribute__ which would activate
        # the replacement
        self.assertEqual(lazy_import.ScopeReplacer,
                         object.__getattribute__(test_obj6, '__class__'))
        test_obj6.bar = 'test'
        self.assertNotEqual(lazy_import.ScopeReplacer,
                            object.__getattribute__(test_obj6, '__class__'))
        self.assertEqual('test', test_obj6.bar)

    def test_replace_side_effects(self):
        """Creating a new object should only create one entry in globals.

        And only that entry even after replacement.
        """
        try:
            test_scope1
        except NameError:
            # test_scope1 shouldn't exist yet
            pass
        else:
            self.fail('test_scope1 was not supposed to exist yet')

        # ignore the logged actions
        TestClass.use_actions([])

        def factory(replacer, scope, name):
            return TestClass()

        orig_globals = set(globals().keys())

        lazy_import.ScopeReplacer(scope=globals(), name='test_scope1',
                                  factory=factory)

        new_globals = set(globals().keys())

        self.assertEqual(lazy_import.ScopeReplacer,
                         object.__getattribute__(test_scope1, '__class__'))
        self.assertEqual('foo', test_scope1.foo(1))
        self.assertIsInstance(test_scope1, TestClass)

        final_globals = set(globals().keys())

        self.assertEqual(set(['test_scope1']), new_globals - orig_globals)
        self.assertEqual(set(), orig_globals - new_globals)
        self.assertEqual(set(), final_globals - new_globals)
        self.assertEqual(set(), new_globals - final_globals)

    def test_class(self):
        actions = []
        InstrumentedReplacer.use_actions(actions)
        TestClass.use_actions(actions)

        def factory(replacer, scope, name):
            actions.append('factory')
            return TestClass

        try:
            test_class1
        except NameError:
            # test_class2 shouldn't exist yet
            pass
        else:
            self.fail('test_class1 was not supposed to exist yet')

        InstrumentedReplacer(scope=globals(), name='test_class1',
                             factory=factory)

        self.assertEqual('class_member', test_class1.class_member)
        self.assertEqual(test_class1, TestClass)
        self.assertEqual([('__getattribute__', 'class_member'),
                          'factory',
                         ], actions)

    def test_call_class(self):
        actions = []
        InstrumentedReplacer.use_actions(actions)
        TestClass.use_actions(actions)

        def factory(replacer, scope, name):
            actions.append('factory')
            return TestClass

        try:
            test_class2
        except NameError:
            # test_class2 shouldn't exist yet
            pass
        else:
            self.fail('test_class2 was not supposed to exist yet')

        InstrumentedReplacer(scope=globals(), name='test_class2',
                             factory=factory)

        self.assertFalse(test_class2 is TestClass)
        obj = test_class2()
        self.assertIs(test_class2, TestClass)
        self.assertIsInstance(obj, TestClass)
        self.assertEqual('class_member', obj.class_member)
        self.assertEqual([('__call__', (), {}),
                          'factory',
                          'init',
                         ], actions)

    def test_call_func(self):
        actions = []
        InstrumentedReplacer.use_actions(actions)

        def func(a, b, c=None):
            actions.append('func')
            return (a, b, c)

        def factory(replacer, scope, name):
            actions.append('factory')
            return func

        try:
            test_func1
        except NameError:
            # test_func1 shouldn't exist yet
            pass
        else:
            self.fail('test_func1 was not supposed to exist yet')
        InstrumentedReplacer(scope=globals(), name='test_func1',
                             factory=factory)

        self.assertFalse(test_func1 is func)
        val = test_func1(1, 2, c='3')
        self.assertIs(test_func1, func)

        self.assertEqual((1,2,'3'), val)
        self.assertEqual([('__call__', (1,2), {'c':'3'}),
                          'factory',
                          'func',
                         ], actions)

    def test_other_variable(self):
        """Test when a ScopeReplacer is assigned to another variable.

        This test could be updated if we find a way to trap '=' rather
        than just giving a belated exception.
        ScopeReplacer only knows about the variable it was created as,
        so until the object is replaced, it is illegal to pass it to
        another variable. (Though discovering this may take a while)
        """
        actions = []
        InstrumentedReplacer.use_actions(actions)
        TestClass.use_actions(actions)

        def factory(replacer, scope, name):
            actions.append('factory')
            return TestClass()

        try:
            test_obj2
        except NameError:
            # test_obj2 shouldn't exist yet
            pass
        else:
            self.fail('test_obj2 was not supposed to exist yet')

        InstrumentedReplacer(scope=globals(), name='test_obj2',
                             factory=factory)

        self.assertEqual(InstrumentedReplacer,
                         object.__getattribute__(test_obj2, '__class__'))
        # This is technically not allowed, but we don't have a way to
        # test it until later.
        test_obj3 = test_obj2
        self.assertEqual(InstrumentedReplacer,
                         object.__getattribute__(test_obj2, '__class__'))
        self.assertEqual(InstrumentedReplacer,
                         object.__getattribute__(test_obj3, '__class__'))

        # The first use of the alternate variable causes test_obj2 to
        # be replaced.
        self.assertEqual('foo', test_obj3.foo(1))
        # test_obj2 has been replaced, but the ScopeReplacer has no
        # idea of test_obj3
        self.assertEqual(TestClass,
                         object.__getattribute__(test_obj2, '__class__'))
        self.assertEqual(InstrumentedReplacer,
                         object.__getattribute__(test_obj3, '__class__'))
        # We should be able to access test_obj2 attributes normally
        self.assertEqual('foo', test_obj2.foo(2))
        self.assertEqual('foo', test_obj2.foo(3))

        # However, the next access on test_obj3 should raise an error
        # because only now are we able to detect the problem.
        self.assertRaises(errors.IllegalUseOfScopeReplacer,
                          getattr, test_obj3, 'foo')

        self.assertEqual([('__getattribute__', 'foo'),
                          'factory',
                          'init',
                          ('foo', 1),
                          ('foo', 2),
                          ('foo', 3),
                          ('__getattribute__', 'foo'),
                         ], actions)

    def test_enable_proxying(self):
        """Test that we can allow ScopeReplacer to proxy."""
        actions = []
        InstrumentedReplacer.use_actions(actions)
        TestClass.use_actions(actions)

        def factory(replacer, scope, name):
            actions.append('factory')
            return TestClass()

        try:
            test_obj4
        except NameError:
            # test_obj4 shouldn't exist yet
            pass
        else:
            self.fail('test_obj4 was not supposed to exist yet')

        lazy_import.ScopeReplacer._should_proxy = True
        InstrumentedReplacer(scope=globals(), name='test_obj4',
                             factory=factory)

        self.assertEqual(InstrumentedReplacer,
                         object.__getattribute__(test_obj4, '__class__'))
        test_obj5 = test_obj4
        self.assertEqual(InstrumentedReplacer,
                         object.__getattribute__(test_obj4, '__class__'))
        self.assertEqual(InstrumentedReplacer,
                         object.__getattribute__(test_obj5, '__class__'))

        # The first use of the alternate variable causes test_obj2 to
        # be replaced.
        self.assertEqual('foo', test_obj4.foo(1))
        self.assertEqual(TestClass,
                         object.__getattribute__(test_obj4, '__class__'))
        self.assertEqual(InstrumentedReplacer,
                         object.__getattribute__(test_obj5, '__class__'))
        # We should be able to access test_obj4 attributes normally
        self.assertEqual('foo', test_obj4.foo(2))
        # because we enabled proxying, test_obj5 can access its members as well
        self.assertEqual('foo', test_obj5.foo(3))
        self.assertEqual('foo', test_obj5.foo(4))

        # However, it cannot be replaced by the ScopeReplacer
        self.assertEqual(InstrumentedReplacer,
                         object.__getattribute__(test_obj5, '__class__'))

        self.assertEqual([('__getattribute__', 'foo'),
                          'factory',
                          'init',
                          ('foo', 1),
                          ('foo', 2),
                          ('__getattribute__', 'foo'),
                          ('foo', 3),
                          ('__getattribute__', 'foo'),
                          ('foo', 4),
                         ], actions)

    def test_replacing_from_own_scope_fails(self):
        """If a ScopeReplacer tries to replace itself a nice error is given"""
        actions = []
        InstrumentedReplacer.use_actions(actions)
        TestClass.use_actions(actions)

        def factory(replacer, scope, name):
            actions.append('factory')
            # return the name in given scope, which is currently the replacer
            return scope[name]

        try:
            test_obj7
        except NameError:
            # test_obj7 shouldn't exist yet
            pass
        else:
            self.fail('test_obj7 was not supposed to exist yet')

        InstrumentedReplacer(scope=globals(), name='test_obj7',
                             factory=factory)

        self.assertEqual(InstrumentedReplacer,
                         object.__getattribute__(test_obj7, '__class__'))
        e = self.assertRaises(errors.IllegalUseOfScopeReplacer, test_obj7)
        self.assertIn("replace itself", e.msg)
        self.assertEqual([('__call__', (), {}),
                          'factory'], actions)


class ImportReplacerHelper(TestCaseInTempDir):
    """Test the ability to have a lazily imported module or object"""

    def setUp(self):
        TestCaseInTempDir.setUp(self)
        self.create_modules()
        base_path = self.test_dir + '/base'

        self.actions = []
        InstrumentedImportReplacer.use_actions(self.actions)

        sys.path.append(base_path)
        self.addCleanup(sys.path.remove, base_path)

        original_import = __import__
        def instrumented_import(mod, scope1, scope2, fromlist, level):
            self.actions.append(('import', mod, fromlist, level))
            return original_import(mod, scope1, scope2, fromlist, level)
        def cleanup():
            __builtins__['__import__'] = original_import
        self.addCleanup(cleanup)
        __builtins__['__import__'] = instrumented_import

    def create_modules(self):
        """Create some random modules to be imported.

        Each entry has a random suffix, and the full names are saved

        These are setup as follows:
         base/ <= used to ensure not in default search path
            root-XXX/
                __init__.py <= This will contain var1, func1
                mod-XXX.py <= This will contain var2, func2
                sub-XXX/
                    __init__.py <= Contains var3, func3
                    submoda-XXX.py <= contains var4, func4
                    submodb-XXX.py <= containse var5, func5
        """
        rand_suffix = osutils.rand_chars(4)
        root_name = 'root_' + rand_suffix
        mod_name = 'mod_' + rand_suffix
        sub_name = 'sub_' + rand_suffix
        submoda_name = 'submoda_' + rand_suffix
        submodb_name = 'submodb_' + rand_suffix

        os.mkdir('base')
        root_path = osutils.pathjoin('base', root_name)
        os.mkdir(root_path)
        root_init = osutils.pathjoin(root_path, '__init__.py')
        f = open(osutils.pathjoin(root_path, '__init__.py'), 'wb')
        try:
            f.write('var1 = 1\ndef func1(a):\n  return a\n')
        finally:
            f.close()
        mod_path = osutils.pathjoin(root_path, mod_name + '.py')
        f = open(mod_path, 'wb')
        try:
            f.write('var2 = 2\ndef func2(a):\n  return a\n')
        finally:
            f.close()

        sub_path = osutils.pathjoin(root_path, sub_name)
        os.mkdir(sub_path)
        f = open(osutils.pathjoin(sub_path, '__init__.py'), 'wb')
        try:
            f.write('var3 = 3\ndef func3(a):\n  return a\n')
        finally:
            f.close()
        submoda_path = osutils.pathjoin(sub_path, submoda_name + '.py')
        f = open(submoda_path, 'wb')
        try:
            f.write('var4 = 4\ndef func4(a):\n  return a\n')
        finally:
            f.close()
        submodb_path = osutils.pathjoin(sub_path, submodb_name + '.py')
        f = open(submodb_path, 'wb')
        try:
            f.write('var5 = 5\ndef func5(a):\n  return a\n')
        finally:
            f.close()
        self.root_name = root_name
        self.mod_name = mod_name
        self.sub_name = sub_name
        self.submoda_name = submoda_name
        self.submodb_name = submodb_name


class TestImportReplacerHelper(ImportReplacerHelper):

    def test_basic_import(self):
        """Test that a real import of these modules works"""
        sub_mod_path = '.'.join([self.root_name, self.sub_name,
                                  self.submoda_name])
        root = __import__(sub_mod_path, globals(), locals(), [], 0)
        self.assertEqual(1, root.var1)
        self.assertEqual(3, getattr(root, self.sub_name).var3)
        self.assertEqual(4, getattr(getattr(root, self.sub_name),
                                    self.submoda_name).var4)

        mod_path = '.'.join([self.root_name, self.mod_name])
        root = __import__(mod_path, globals(), locals(), [], 0)
        self.assertEqual(2, getattr(root, self.mod_name).var2)

        self.assertEqual([('import', sub_mod_path, [], 0),
                          ('import', mod_path, [], 0),
                         ], self.actions)


class TestImportReplacer(ImportReplacerHelper):

    def test_import_root(self):
        """Test 'import root-XXX as root1'"""
        try:
            root1
        except NameError:
            # root1 shouldn't exist yet
            pass
        else:
            self.fail('root1 was not supposed to exist yet')

        # This should replicate 'import root-xxyyzz as root1'
        InstrumentedImportReplacer(scope=globals(), name='root1',
                                   module_path=[self.root_name],
                                   member=None, children={})

        self.assertEqual(InstrumentedImportReplacer,
                         object.__getattribute__(root1, '__class__'))
        self.assertEqual(1, root1.var1)
        self.assertEqual('x', root1.func1('x'))

        self.assertEqual([('__getattribute__', 'var1'),
                          ('_import', 'root1'),
                          ('import', self.root_name, [], 0),
                         ], self.actions)

    def test_import_mod(self):
        """Test 'import root-XXX.mod-XXX as mod2'"""
        try:
            mod1
        except NameError:
            # mod1 shouldn't exist yet
            pass
        else:
            self.fail('mod1 was not supposed to exist yet')

        mod_path = self.root_name + '.' + self.mod_name
        InstrumentedImportReplacer(scope=globals(), name='mod1',
                                   module_path=[self.root_name, self.mod_name],
                                   member=None, children={})

        self.assertEqual(InstrumentedImportReplacer,
                         object.__getattribute__(mod1, '__class__'))
        self.assertEqual(2, mod1.var2)
        self.assertEqual('y', mod1.func2('y'))

        self.assertEqual([('__getattribute__', 'var2'),
                          ('_import', 'mod1'),
                          ('import', mod_path, [], 0),
                         ], self.actions)

    def test_import_mod_from_root(self):
        """Test 'from root-XXX import mod-XXX as mod2'"""
        try:
            mod2
        except NameError:
            # mod2 shouldn't exist yet
            pass
        else:
            self.fail('mod2 was not supposed to exist yet')

        InstrumentedImportReplacer(scope=globals(), name='mod2',
                                   module_path=[self.root_name],
                                   member=self.mod_name, children={})

        self.assertEqual(InstrumentedImportReplacer,
                         object.__getattribute__(mod2, '__class__'))
        self.assertEqual(2, mod2.var2)
        self.assertEqual('y', mod2.func2('y'))

        self.assertEqual([('__getattribute__', 'var2'),
                          ('_import', 'mod2'),
                          ('import', self.root_name, [self.mod_name], 0),
                         ], self.actions)

    def test_import_root_and_mod(self):
        """Test 'import root-XXX.mod-XXX' remapping both to root3.mod3"""
        try:
            root3
        except NameError:
            # root3 shouldn't exist yet
            pass
        else:
            self.fail('root3 was not supposed to exist yet')

        InstrumentedImportReplacer(scope=globals(),
            name='root3', module_path=[self.root_name], member=None,
            children={'mod3':([self.root_name, self.mod_name], None, {})})

        # So 'root3' should be a lazy import
        # and once it is imported, mod3 should also be lazy until
        # actually accessed.
        self.assertEqual(InstrumentedImportReplacer,
                         object.__getattribute__(root3, '__class__'))
        self.assertEqual(1, root3.var1)

        # There is a mod3 member, but it is also lazy
        self.assertEqual(InstrumentedImportReplacer,
                         object.__getattribute__(root3.mod3, '__class__'))
        self.assertEqual(2, root3.mod3.var2)

        mod_path = self.root_name + '.' + self.mod_name
        self.assertEqual([('__getattribute__', 'var1'),
                          ('_import', 'root3'),
                          ('import', self.root_name, [], 0),
                          ('__getattribute__', 'var2'),
                          ('_import', 'mod3'),
                          ('import', mod_path, [], 0),
                         ], self.actions)

    def test_import_root_and_root_mod(self):
        """Test that 'import root, root.mod' can be done.

        The second import should re-use the first one, and just add
        children to be imported.
        """
        try:
            root4
        except NameError:
            # root4 shouldn't exist yet
            pass
        else:
            self.fail('root4 was not supposed to exist yet')

        InstrumentedImportReplacer(scope=globals(),
            name='root4', module_path=[self.root_name], member=None,
            children={})

        # So 'root4' should be a lazy import
        self.assertEqual(InstrumentedImportReplacer,
                         object.__getattribute__(root4, '__class__'))

        # Lets add a new child to be imported on demand
        # This syntax of using object.__getattribute__ is the correct method
        # for accessing the _import_replacer_children member
        children = object.__getattribute__(root4, '_import_replacer_children')
        children['mod4'] = ([self.root_name, self.mod_name], None, {})

        # Accessing root4.mod4 should import root, but mod should stay lazy
        self.assertEqual(InstrumentedImportReplacer,
                         object.__getattribute__(root4.mod4, '__class__'))
        self.assertEqual(2, root4.mod4.var2)

        mod_path = self.root_name + '.' + self.mod_name
        self.assertEqual([('__getattribute__', 'mod4'),
                          ('_import', 'root4'),
                          ('import', self.root_name, [], 0),
                          ('__getattribute__', 'var2'),
                          ('_import', 'mod4'),
                          ('import', mod_path, [], 0),
                         ], self.actions)

    def test_import_root_sub_submod(self):
        """Test import root.mod, root.sub.submoda, root.sub.submodb
        root should be a lazy import, with multiple children, who also
        have children to be imported.
        And when root is imported, the children should be lazy, and
        reuse the intermediate lazy object.
        """
        try:
            root5
        except NameError:
            # root5 shouldn't exist yet
            pass
        else:
            self.fail('root5 was not supposed to exist yet')

        InstrumentedImportReplacer(scope=globals(),
            name='root5', module_path=[self.root_name], member=None,
            children={'mod5':([self.root_name, self.mod_name], None, {}),
                      'sub5':([self.root_name, self.sub_name], None,
                            {'submoda5':([self.root_name, self.sub_name,
                                         self.submoda_name], None, {}),
                             'submodb5':([self.root_name, self.sub_name,
                                          self.submodb_name], None, {})
                            }),
                     })

        # So 'root5' should be a lazy import
        self.assertEqual(InstrumentedImportReplacer,
                         object.__getattribute__(root5, '__class__'))

        # Accessing root5.mod5 should import root, but mod should stay lazy
        self.assertEqual(InstrumentedImportReplacer,
                         object.__getattribute__(root5.mod5, '__class__'))
        # root5.sub5 should still be lazy, but not re-import root5
        self.assertEqual(InstrumentedImportReplacer,
                         object.__getattribute__(root5.sub5, '__class__'))

        # Accessing root5.sub5.submoda5 should import sub5, but not either
        # of the sub objects (they should be available as lazy objects
        self.assertEqual(InstrumentedImportReplacer,
                     object.__getattribute__(root5.sub5.submoda5, '__class__'))
        self.assertEqual(InstrumentedImportReplacer,
                     object.__getattribute__(root5.sub5.submodb5, '__class__'))

        # This should import mod5
        self.assertEqual(2, root5.mod5.var2)
        # These should import submoda5 and submodb5
        self.assertEqual(4, root5.sub5.submoda5.var4)
        self.assertEqual(5, root5.sub5.submodb5.var5)

        mod_path = self.root_name + '.' + self.mod_name
        sub_path = self.root_name + '.' + self.sub_name
        submoda_path = sub_path + '.' + self.submoda_name
        submodb_path = sub_path + '.' + self.submodb_name

        self.assertEqual([('__getattribute__', 'mod5'),
                          ('_import', 'root5'),
                          ('import', self.root_name, [], 0),
                          ('__getattribute__', 'submoda5'),
                          ('_import', 'sub5'),
                          ('import', sub_path, [], 0),
                          ('__getattribute__', 'var2'),
                          ('_import', 'mod5'),
                          ('import', mod_path, [], 0),
                          ('__getattribute__', 'var4'),
                          ('_import', 'submoda5'),
                          ('import', submoda_path, [], 0),
                          ('__getattribute__', 'var5'),
                          ('_import', 'submodb5'),
                          ('import', submodb_path, [], 0),
                         ], self.actions)


class TestConvertImportToMap(TestCase):
    """Directly test the conversion from import strings to maps"""

    def check(self, expected, import_strings):
        proc = lazy_import.ImportProcessor()
        for import_str in import_strings:
            proc._convert_import_str(import_str)
        self.assertEqual(expected, proc.imports,
                         'Import of %r was not converted correctly'
                         ' %s != %s' % (import_strings, expected,
                                        proc.imports))

    def test_import_one(self):
        self.check({'one':(['one'], None, {}),
                   }, ['import one'])

    def test_import_one_two(self):
        one_two_map = {'one':(['one'], None,
                              {'two':(['one', 'two'], None, {}),
                              }),
                      }
        self.check(one_two_map, ['import one.two'])
        self.check(one_two_map, ['import one, one.two'])
        self.check(one_two_map, ['import one', 'import one.two'])
        self.check(one_two_map, ['import one.two', 'import one'])

    def test_import_one_two_three(self):
        one_two_three_map = {
            'one':(['one'], None,
                   {'two':(['one', 'two'], None,
                           {'three':(['one', 'two', 'three'], None, {}),
                           }),
                   }),
        }
        self.check(one_two_three_map, ['import one.two.three'])
        self.check(one_two_three_map, ['import one, one.two.three'])
        self.check(one_two_three_map, ['import one',
                                              'import one.two.three'])
        self.check(one_two_three_map, ['import one.two.three',
                                              'import one'])

    def test_import_one_as_x(self):
        self.check({'x':(['one'], None, {}),
                          }, ['import one as x'])

    def test_import_one_two_as_x(self):
        self.check({'x':(['one', 'two'], None, {}),
                   }, ['import one.two as x'])

    def test_import_mixed(self):
        mixed = {'x':(['one', 'two'], None, {}),
                 'one':(['one'], None,
                       {'two':(['one', 'two'], None, {}),
                       }),
                }
        self.check(mixed, ['import one.two as x, one.two'])
        self.check(mixed, ['import one.two as x', 'import one.two'])
        self.check(mixed, ['import one.two', 'import one.two as x'])

    def test_import_with_as(self):
        self.check({'fast':(['fast'], None, {})}, ['import fast'])


class TestFromToMap(TestCase):
    """Directly test the conversion of 'from foo import bar' syntax"""

    def check_result(self, expected, from_strings):
        proc = lazy_import.ImportProcessor()
        for from_str in from_strings:
            proc._convert_from_str(from_str)
        self.assertEqual(expected, proc.imports,
                         'Import of %r was not converted correctly'
                         ' %s != %s' % (from_strings, expected, proc.imports))

    def test_from_one_import_two(self):
        self.check_result({'two':(['one'], 'two', {})},
                          ['from one import two'])

    def test_from_one_import_two_as_three(self):
        self.check_result({'three':(['one'], 'two', {})},
                          ['from one import two as three'])

    def test_from_one_import_two_three(self):
        two_three_map = {'two':(['one'], 'two', {}),
                         'three':(['one'], 'three', {}),
                        }
        self.check_result(two_three_map,
                          ['from one import two, three'])
        self.check_result(two_three_map,
                          ['from one import two',
                           'from one import three'])

    def test_from_one_two_import_three(self):
        self.check_result({'three':(['one', 'two'], 'three', {})},
                          ['from one.two import three'])


class TestCanonicalize(TestCase):
    """Test that we can canonicalize import texts"""

    def check(self, expected, text):
        proc = lazy_import.ImportProcessor()
        parsed = proc._canonicalize_import_text(text)
        self.assertEqual(expected, parsed,
                         'Incorrect parsing of text:\n%s\n%s\n!=\n%s'
                         % (text, expected, parsed))

    def test_import_one(self):
        self.check(['import one'], 'import one')
        self.check(['import one'], '\nimport one\n\n')

    def test_import_one_two(self):
        self.check(['import one, two'], 'import one, two')
        self.check(['import one, two'], '\nimport one, two\n\n')

    def test_import_one_as_two_as(self):
        self.check(['import one as x, two as y'], 'import one as x, two as y')
        self.check(['import one as x, two as y'],
                   '\nimport one as x, two as y\n')

    def test_from_one_import_two(self):
        self.check(['from one import two'], 'from one import two')
        self.check(['from one import two'], '\nfrom one import two\n\n')
        self.check(['from one import two'], '\nfrom one import (two)\n')
        self.check(['from one import  two '], '\nfrom one import (\n\ttwo\n)\n')

    def test_multiple(self):
        self.check(['import one', 'import two, three', 'from one import four'],
                   'import one\nimport two, three\nfrom one import four')
        self.check(['import one', 'import two, three', 'from one import four'],
                   'import one\nimport (two, three)\nfrom one import four')
        self.check(['import one', 'import two, three', 'from one import four'],
                   'import one\n'
                   'import two, three\n'
                   'from one import four')
        self.check(['import one',
                    'import two, three', 'from one import  four, '],
                   'import one\n'
                   'import two, three\n'
                   'from one import (\n'
                   '    four,\n'
                   '    )\n'
                   )

    def test_missing_trailing(self):
        proc = lazy_import.ImportProcessor()
        self.assertRaises(errors.InvalidImportLine,
                          proc._canonicalize_import_text,
                          "from foo import (\n  bar\n")


class TestImportProcessor(TestCase):
    """Test that ImportProcessor can turn import texts into lazy imports"""

    def check(self, expected, text):
        proc = lazy_import.ImportProcessor()
        proc._build_map(text)
        self.assertEqual(expected, proc.imports,
                         'Incorrect processing of:\n%s\n%s\n!=\n%s'
                         % (text, expected, proc.imports))

    def test_import_one(self):
        exp = {'one':(['one'], None, {})}
        self.check(exp, 'import one')
        self.check(exp, '\nimport one\n')

    def test_import_one_two(self):
        exp = {'one':(['one'], None,
                      {'two':(['one', 'two'], None, {}),
                      }),
              }
        self.check(exp, 'import one.two')
        self.check(exp, 'import one, one.two')
        self.check(exp, 'import one\nimport one.two')

    def test_import_as(self):
        exp = {'two':(['one'], None, {})}
        self.check(exp, 'import one as two')

    def test_import_many(self):
        exp = {'one':(['one'], None,
                      {'two':(['one', 'two'], None,
                              {'three':(['one', 'two', 'three'], None, {}),
                              }),
                       'four':(['one', 'four'], None, {}),
                      }),
               'five':(['one', 'five'], None, {}),
              }
        self.check(exp, 'import one.two.three, one.four, one.five as five')
        self.check(exp, 'import one.five as five\n'
                        'import one\n'
                        'import one.two.three\n'
                        'import one.four\n')

    def test_from_one_import_two(self):
        exp = {'two':(['one'], 'two', {})}
        self.check(exp, 'from one import two\n')
        self.check(exp, 'from one import (\n'
                        '    two,\n'
                        '    )\n')

    def test_from_one_import_two(self):
        exp = {'two':(['one'], 'two', {})}
        self.check(exp, 'from one import two\n')
        self.check(exp, 'from one import (two)\n')
        self.check(exp, 'from one import (two,)\n')
        self.check(exp, 'from one import two as two\n')
        self.check(exp, 'from one import (\n'
                        '    two,\n'
                        '    )\n')

    def test_from_many(self):
        exp = {'two':(['one'], 'two', {}),
               'three':(['one', 'two'], 'three', {}),
               'five':(['one', 'two'], 'four', {}),
              }
        self.check(exp, 'from one import two\n'
                        'from one.two import three, four as five\n')
        self.check(exp, 'from one import two\n'
                        'from one.two import (\n'
                        '    three,\n'
                        '    four as five,\n'
                        '    )\n')

    def test_mixed(self):
        exp = {'two':(['one'], 'two', {}),
               'three':(['one', 'two'], 'three', {}),
               'five':(['one', 'two'], 'four', {}),
               'one':(['one'], None,
                      {'two':(['one', 'two'], None, {}),
                      }),
              }
        self.check(exp, 'from one import two\n'
                        'from one.two import three, four as five\n'
                        'import one.two')
        self.check(exp, 'from one import two\n'
                        'from one.two import (\n'
                        '    three,\n'
                        '    four as five,\n'
                        '    )\n'
                        'import one\n'
                        'import one.two\n')

    def test_incorrect_line(self):
        proc = lazy_import.ImportProcessor()
        self.assertRaises(errors.InvalidImportLine,
                          proc._build_map, 'foo bar baz')
        self.assertRaises(errors.InvalidImportLine,
                          proc._build_map, 'improt foo')
        self.assertRaises(errors.InvalidImportLine,
                          proc._build_map, 'importfoo')
        self.assertRaises(errors.InvalidImportLine,
                          proc._build_map, 'fromimport')

    def test_name_collision(self):
        proc = lazy_import.ImportProcessor()
        proc._build_map('import foo')

        # All of these would try to create an object with the
        # same name as an existing object.
        self.assertRaises(errors.ImportNameCollision,
                          proc._build_map, 'import bar as foo')
        self.assertRaises(errors.ImportNameCollision,
                          proc._build_map, 'from foo import bar as foo')
        self.assertRaises(errors.ImportNameCollision,
                          proc._build_map, 'from bar import foo')


class TestLazyImportProcessor(ImportReplacerHelper):

    def test_root(self):
        try:
            root6
        except NameError:
            pass # root6 should not be defined yet
        else:
            self.fail('root6 was not supposed to exist yet')

        text = 'import %s as root6' % (self.root_name,)
        proc = lazy_import.ImportProcessor(InstrumentedImportReplacer)
        proc.lazy_import(scope=globals(), text=text)

        # So 'root6' should be a lazy import
        self.assertEqual(InstrumentedImportReplacer,
                         object.__getattribute__(root6, '__class__'))

        self.assertEqual(1, root6.var1)
        self.assertEqual('x', root6.func1('x'))

        self.assertEqual([('__getattribute__', 'var1'),
                          ('_import', 'root6'),
                          ('import', self.root_name, [], 0),
                         ], self.actions)

    def test_import_deep(self):
        """Test import root.mod, root.sub.submoda, root.sub.submodb
        root should be a lazy import, with multiple children, who also
        have children to be imported.
        And when root is imported, the children should be lazy, and
        reuse the intermediate lazy object.
        """
        try:
            submoda7
        except NameError:
            pass # submoda7 should not be defined yet
        else:
            self.fail('submoda7 was not supposed to exist yet')

        text = """\
import %(root_name)s.%(sub_name)s.%(submoda_name)s as submoda7
""" % self.__dict__
        proc = lazy_import.ImportProcessor(InstrumentedImportReplacer)
        proc.lazy_import(scope=globals(), text=text)

        # So 'submoda7' should be a lazy import
        self.assertEqual(InstrumentedImportReplacer,
                         object.__getattribute__(submoda7, '__class__'))

        # This should import submoda7
        self.assertEqual(4, submoda7.var4)

        sub_path = self.root_name + '.' + self.sub_name
        submoda_path = sub_path + '.' + self.submoda_name

        self.assertEqual([('__getattribute__', 'var4'),
                          ('_import', 'submoda7'),
                          ('import', submoda_path, [], 0),
                         ], self.actions)

    def test_lazy_import(self):
        """Smoke test that lazy_import() does the right thing"""
        try:
            root8
        except NameError:
            pass # root8 should not be defined yet
        else:
            self.fail('root8 was not supposed to exist yet')
        lazy_import.lazy_import(globals(),
                                'import %s as root8' % (self.root_name,),
                                lazy_import_class=InstrumentedImportReplacer)

        self.assertEqual(InstrumentedImportReplacer,
                         object.__getattribute__(root8, '__class__'))

        self.assertEqual(1, root8.var1)
        self.assertEqual(1, root8.var1)
        self.assertEqual(1, root8.func1(1))

        self.assertEqual([('__getattribute__', 'var1'),
                          ('_import', 'root8'),
                          ('import', self.root_name, [], 0),
                         ], self.actions)


class TestScopeReplacerReentrance(TestCase):
    """The ScopeReplacer should be reentrant.

    Invoking a replacer while an invocation was already on-going leads to a
    race to see which invocation will be the first to call _replace.
    The losing caller used to see an exception (bugs 396819 and 702914).

    These tests set up a tracer that stops at a suitable moment (upon
    entry of a specified method) and starts another call to the
    functionality in question (__call__, __getattribute__, __setattr_)
    in order to win the race, setting up the original caller to lose.
    """

    def tracer(self, frame, event, arg):
        if event != 'call':
            return self.tracer
        # Grab the name of the file that contains the code being executed.
        code = frame.f_code
        filename = code.co_filename
        # Convert ".pyc" and ".pyo" file names to their ".py" equivalent.
        filename = re.sub(r'\.py[co]$', '.py', filename)
        function_name = code.co_name
        # If we're executing a line of code from the right module...
        if (filename.endswith('lazy_import.py') and
            function_name == self.method_to_trace):
            # We don't need to trace any more.
            sys.settrace(None)
            # Run another racer.  This one will "win" the race.
            self.racer()
        return self.tracer

    def run_race(self, racer, method_to_trace='_resolve'):
        self.overrideAttr(lazy_import.ScopeReplacer, '_should_proxy', True)
        self.racer = racer
        self.method_to_trace = method_to_trace
        sys.settrace(self.tracer)
        self.racer() # Should not raise any exception
        # Make sure the tracer actually found the code it was
        # looking for.  If not, maybe the code was refactored in
        # such a way that these tests aren't needed any more.
        self.assertEqual(None, sys.gettrace())

    def test_call(self):
        def factory(*args):
            return factory
        replacer = lazy_import.ScopeReplacer({}, factory, 'name')
        self.run_race(replacer)

    def test_setattr(self):
        class Replaced:
            pass

        def factory(*args):
            return Replaced()

        replacer = lazy_import.ScopeReplacer({}, factory, 'name')

        def racer():
            replacer.foo = 42

        self.run_race(racer)

    def test_getattribute(self):
        class Replaced:
            foo = 'bar'

        def factory(*args):
            return Replaced()

        replacer = lazy_import.ScopeReplacer({}, factory, 'name')

        def racer():
            replacer.foo

        self.run_race(racer)