~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_lazy_import.py

  • Committer: John Arbash Meinel
  • Date: 2006-09-12 17:33:23 UTC
  • mto: This revision was merged to the branch mainline in revision 2004.
  • Revision ID: john@arbash-meinel.com-20060912173323-112d28652516fd94
Raise an exception when ScopeReplacer has been misused

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
import sys
21
21
 
22
22
from bzrlib import (
 
23
    errors,
23
24
    lazy_import,
24
25
    osutils,
25
26
    )
223
224
                          'func',
224
225
                         ], actions)
225
226
 
 
227
    def test_other_variable(self):
 
228
        """Test when a ScopeReplacer is assigned to another variable.
 
229
 
 
230
        This test could be updated if we find a way to trap '=' rather
 
231
        than just giving a belated exception.
 
232
        ScopeReplacer only knows about the variable it was created as,
 
233
        so until the object is replaced, it is illegal to pass it to
 
234
        another variable. (Though discovering this may take a while)
 
235
        """
 
236
        actions = []
 
237
        InstrumentedReplacer.use_actions(actions)
 
238
        TestClass.use_actions(actions)
 
239
 
 
240
        def factory(replacer, scope, name):
 
241
            actions.append('factory')
 
242
            return TestClass()
 
243
 
 
244
        try:
 
245
            test_obj2
 
246
        except NameError:
 
247
            # test_obj2 shouldn't exist yet
 
248
            pass
 
249
        else:
 
250
            self.fail('test_obj2 was not supposed to exist yet')
 
251
 
 
252
        InstrumentedReplacer(scope=globals(), name='test_obj2',
 
253
                             factory=factory)
 
254
 
 
255
        self.assertEqual(InstrumentedReplacer,
 
256
                         object.__getattribute__(test_obj2, '__class__'))
 
257
        # This is technically not allowed, but we don't have a way to
 
258
        # test it until later.
 
259
        test_obj3 = test_obj2
 
260
        self.assertEqual(InstrumentedReplacer,
 
261
                         object.__getattribute__(test_obj2, '__class__'))
 
262
        self.assertEqual(InstrumentedReplacer,
 
263
                         object.__getattribute__(test_obj3, '__class__'))
 
264
        
 
265
        # The first use of the alternate variable causes test_obj2 to
 
266
        # be replaced.
 
267
        self.assertEqual('foo', test_obj3.foo(1))
 
268
        # test_obj2 has been replaced, but the ScopeReplacer has no
 
269
        # idea of test_obj3
 
270
        self.assertEqual(TestClass,
 
271
                         object.__getattribute__(test_obj2, '__class__'))
 
272
        self.assertEqual(InstrumentedReplacer,
 
273
                         object.__getattribute__(test_obj3, '__class__'))
 
274
        # We should be able to access test_obj2 attributes normally
 
275
        self.assertEqual('foo', test_obj2.foo(2))
 
276
        self.assertEqual('foo', test_obj2.foo(3))
 
277
 
 
278
        # However, the next access on test_obj3 should raise an error
 
279
        # because only now are we able to detect the problem.
 
280
        self.assertRaises(errors.IllegalUseOfScopeReplacer,
 
281
                          getattr, test_obj3, 'foo')
 
282
        
 
283
        # However, the 
 
284
        self.assertEqual([('__getattribute__', 'foo'),
 
285
                          '_replace',
 
286
                          'factory',
 
287
                          'init',
 
288
                          ('foo', 1),
 
289
                          ('foo', 2),
 
290
                          ('foo', 3),
 
291
                          ('__getattribute__', 'foo'),
 
292
                          '_replace',
 
293
                         ], actions)
 
294
 
226
295
 
227
296
class ImportReplacerHelper(TestCaseInTempDir):
228
297
    """Test the ability to have a lazily imported module or object"""