326
335
self.assertRaises(errors.IllegalUseOfScopeReplacer,
327
336
getattr, test_obj3, 'foo')
330
self.assertEqual([('__getattribute__', 'foo'),
337
('__getattribute__', 'foo'),
338
self.assertEqual([('__getattribute__', 'foo'),
345
('__getattribute__', 'foo'),
349
def test_enable_proxying(self):
350
"""Test that we can allow ScopeReplacer to proxy."""
352
InstrumentedReplacer.use_actions(actions)
353
TestClass.use_actions(actions)
355
def factory(replacer, scope, name):
356
actions.append('factory')
362
# test_obj4 shouldn't exist yet
365
self.fail('test_obj4 was not supposed to exist yet')
367
lazy_import.ScopeReplacer._should_proxy = True
368
InstrumentedReplacer(scope=globals(), name='test_obj4',
371
self.assertEqual(InstrumentedReplacer,
372
object.__getattribute__(test_obj4, '__class__'))
373
test_obj5 = test_obj4
374
self.assertEqual(InstrumentedReplacer,
375
object.__getattribute__(test_obj4, '__class__'))
376
self.assertEqual(InstrumentedReplacer,
377
object.__getattribute__(test_obj5, '__class__'))
379
# The first use of the alternate variable causes test_obj2 to
381
self.assertEqual('foo', test_obj4.foo(1))
382
self.assertEqual(TestClass,
383
object.__getattribute__(test_obj4, '__class__'))
384
self.assertEqual(InstrumentedReplacer,
385
object.__getattribute__(test_obj5, '__class__'))
386
# We should be able to access test_obj4 attributes normally
387
self.assertEqual('foo', test_obj4.foo(2))
388
# because we enabled proxying, test_obj5 can access its members as well
389
self.assertEqual('foo', test_obj5.foo(3))
390
self.assertEqual('foo', test_obj5.foo(4))
392
# However, it cannot be replaced by the ScopeReplacer
393
self.assertEqual(InstrumentedReplacer,
394
object.__getattribute__(test_obj5, '__class__'))
396
self.assertEqual([('__getattribute__', 'foo'),
402
('__getattribute__', 'foo'),
404
('__getattribute__', 'foo'),