~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_lazy_import.py

  • Committer: Martin
  • Date: 2010-09-05 17:15:46 UTC
  • mto: This revision was merged to the branch mainline in revision 5412.
  • Revision ID: gzlist@googlemail.com-20100905171546-3ggqhogy23fgoxc8
Add check in lazy import ScopeReplacer to ensure it's not trying to replace itself

Show diffs side-by-side

added added

removed removed

Lines of Context:
127
127
        else:
128
128
            self.fail('test_obj1 was not supposed to exist yet')
129
129
 
130
 
        orig_globals = set(globals().keys())
131
 
 
132
130
        InstrumentedReplacer(scope=globals(), name='test_obj1',
133
131
                             factory=factory)
134
132
 
135
 
        new_globals = set(globals().keys())
136
 
 
137
133
        # We can't use isinstance() because that uses test_obj1.__class__
138
134
        # and that goes through __getattribute__ which would activate
139
135
        # the replacement
168
164
        else:
169
165
            self.fail('test_obj6 was not supposed to exist yet')
170
166
 
171
 
        orig_globals = set(globals().keys())
172
 
 
173
167
        lazy_import.ScopeReplacer(scope=globals(), name='test_obj6',
174
168
                                  factory=factory)
175
169
 
176
 
        new_globals = set(globals().keys())
177
 
 
178
170
        # We can't use isinstance() because that uses test_obj6.__class__
179
171
        # and that goes through __getattribute__ which would activate
180
172
        # the replacement
440
432
                          ('foo', 4),
441
433
                         ], actions)
442
434
 
 
435
    def test_replacing_from_own_scope_fails(self):
 
436
        """If a ScopeReplacer tries to replace itself a nice error is given"""
 
437
        actions = []
 
438
        InstrumentedReplacer.use_actions(actions)
 
439
        TestClass.use_actions(actions)
 
440
 
 
441
        def factory(replacer, scope, name):
 
442
            actions.append('factory')
 
443
            # return the name in given scope, which is currently the replacer
 
444
            return scope[name]
 
445
 
 
446
        try:
 
447
            test_obj7
 
448
        except NameError:
 
449
            # test_obj7 shouldn't exist yet
 
450
            pass
 
451
        else:
 
452
            self.fail('test_obj7 was not supposed to exist yet')
 
453
 
 
454
        InstrumentedReplacer(scope=globals(), name='test_obj7',
 
455
                             factory=factory)
 
456
 
 
457
        self.assertEqual(InstrumentedReplacer,
 
458
                         object.__getattribute__(test_obj7, '__class__'))
 
459
        e = self.assertRaises(errors.IllegalUseOfScopeReplacer, test_obj7)
 
460
        self.assertIn("replace itself", e.msg)
 
461
        self.assertEqual([('__call__', (), {}),
 
462
                          '_replace',
 
463
                          'factory'], actions)
 
464
 
443
465
 
444
466
class ImportReplacerHelper(TestCaseInTempDir):
445
467
    """Test the ability to have a lazily imported module or object"""