260
260
mod-XXX.py <= This will contain var2, func2
262
262
__init__.py <= Contains var3, func3
263
submod-XXX.py <= contains var4, func4
263
submoda-XXX.py <= contains var4, func4
264
submodb-XXX.py <= containse var5, func5
265
266
rand_suffix = osutils.rand_chars(4)
266
267
root_name = 'root_' + rand_suffix
267
268
mod_name = 'mod_' + rand_suffix
268
269
sub_name = 'sub_' + rand_suffix
269
submod_name = 'submod_' + rand_suffix
270
submoda_name = 'submoda_' + rand_suffix
271
submodb_name = 'submodb_' + rand_suffix
271
274
root_path = osutils.pathjoin('base', root_name)
272
275
os.mkdir(root_path)
290
293
f.write('var3 = 3\ndef func3(a):\n return a\n')
293
submod_path = osutils.pathjoin(sub_path, submod_name + '.py')
294
f = open(submod_path, 'wb')
296
submoda_path = osutils.pathjoin(sub_path, submoda_name + '.py')
297
f = open(submoda_path, 'wb')
296
299
f.write('var4 = 4\ndef func4(a):\n return a\n')
302
submodb_path = osutils.pathjoin(sub_path, submodb_name + '.py')
303
f = open(submodb_path, 'wb')
305
f.write('var5 = 5\ndef func5(a):\n return a\n')
299
308
self.root_name = root_name
300
309
self.mod_name = mod_name
301
310
self.sub_name = sub_name
302
self.submod_name = submod_name
311
self.submoda_name = submoda_name
312
self.submodb_name = submodb_name
304
314
def test_basic_import(self):
315
"""Test that a real import of these modules works"""
305
316
sub_mod_path = '.'.join([self.root_name, self.sub_name,
307
318
root = __import__(sub_mod_path, globals(), locals(), [])
308
319
self.assertEqual(1, root.var1)
309
320
self.assertEqual(3, getattr(root, self.sub_name).var3)
310
321
self.assertEqual(4, getattr(getattr(root, self.sub_name),
311
self.submod_name).var4)
322
self.submoda_name).var4)
313
324
mod_path = '.'.join([self.root_name, self.mod_name])
314
325
root = __import__(mod_path, globals(), locals(), [])
471
486
('_import', 'mod4'),
472
487
('import', mod_path, []),
490
def test_import_root_sub_submod(self):
491
"""Test import root.mod, root.sub.submoda, root.sub.submodb
492
root should be a lazy import, with multiple children, who also
493
have children to be imported.
494
And when root is imported, the children should be lazy, and
495
reuse the intermediate lazy object.
500
# root4 shouldn't exist yet
503
self.fail('root5 was not supposed to exist yet')
505
InstrumentedImportReplacer(scope=globals(),
506
name='root5', module_path=[self.root_name], member=None,
507
children=[('mod5', [self.root_name, self.mod_name], []),
508
('sub5', [self.root_name, self.sub_name],
509
[('submoda5', [self.root_name, self.sub_name,
510
self.submoda_name], []),
511
('submodb5', [self.root_name, self.sub_name,
512
self.submodb_name], [])
516
# So 'root5' should be a lazy import
517
self.assertEqual(InstrumentedImportReplacer,
518
object.__getattribute__(root5, '__class__'))
520
# Accessing root5.mod5 should import root, but mod should stay lazy
521
self.assertEqual(InstrumentedImportReplacer,
522
object.__getattribute__(root5.mod5, '__class__'))
523
# root5.sub5 should still be lazy, but not re-import root5
524
self.assertEqual(InstrumentedImportReplacer,
525
object.__getattribute__(root5.sub5, '__class__'))
526
# Accessing root5.sub5.submoda5 should import sub5, but not either
527
# of the sub objects (they should be available as lazy objects
528
self.assertEqual(InstrumentedImportReplacer,
529
object.__getattribute__(root5.sub5.submoda5, '__class__'))
530
self.assertEqual(InstrumentedImportReplacer,
531
object.__getattribute__(root5.sub5.submodb5, '__class__'))
533
# This should import mod5
534
self.assertEqual(2, root5.mod5.var2)
535
# These should import submoda5 and submodb5
536
self.assertEqual(4, root5.sub5.submoda5.var4)
537
self.assertEqual(5, root5.sub5.submodb5.var5)
539
mod_path = self.root_name + '.' + self.mod_name
540
sub_path = self.root_name + '.' + self.sub_name
541
submoda_path = sub_path + '.' + self.submoda_name
542
submodb_path = sub_path + '.' + self.submodb_name
544
self.assertEqual([('__getattribute__', 'mod5'),
546
('_import', 'root5'),
547
('import', self.root_name, []),
548
('__getattribute__', 'submoda5'),
551
('import', sub_path, []),
552
('__getattribute__', 'var2'),
555
('import', mod_path, []),
556
('__getattribute__', 'var4'),
558
('_import', 'submoda5'),
559
('import', submoda_path, []),
560
('__getattribute__', 'var5'),
562
('_import', 'submodb5'),
563
('import', submodb_path, []),