cpython: 9e1d7cd15d2e (original) (raw)
--- a/Lib/importlib/test/frozen/test_loader.py +++ b/Lib/importlib/test/frozen/test_loader.py @@ -3,20 +3,21 @@ import imp import unittest from .. import abc from .. import util - +from test.support import captured_stdout class LoaderTests(abc.LoaderTests): def test_module(self):
with util.uncache('__hello__'):[](#l1.13)
with util.uncache('__hello__'), captured_stdout() as stdout:[](#l1.14) module = machinery.FrozenImporter.load_module('__hello__')[](#l1.15) check = {'__name__': '__hello__', '__file__': '<frozen>',[](#l1.16) '__package__': '', '__loader__': machinery.FrozenImporter}[](#l1.17) for attr, value in check.items():[](#l1.18) self.assertEqual(getattr(module, attr), value)[](#l1.19)
self.assertEqual(stdout.getvalue(), 'Hello world!\n')[](#l1.20)
with util.uncache('__phello__'):[](#l1.23)
with util.uncache('__phello__'), captured_stdout() as stdout:[](#l1.24) module = machinery.FrozenImporter.load_module('__phello__')[](#l1.25) check = {'__name__': '__phello__', '__file__': '<frozen>',[](#l1.26) '__package__': '__phello__', '__path__': ['__phello__'],[](#l1.27)
@@ -26,9 +27,11 @@ class LoaderTests(abc.LoaderTests): self.assertEqual(attr_value, value, "for phello.%s, %r != %r" % (attr, attr_value, value))
self.assertEqual(stdout.getvalue(), 'Hello world!\n')[](#l1.32)
def test_lacking_parent(self):
with util.uncache('__phello__', '__phello__.spam'):[](#l1.35)
with util.uncache('__phello__', '__phello__.spam'), \[](#l1.36)
captured_stdout() as stdout:[](#l1.37) module = machinery.FrozenImporter.load_module('__phello__.spam')[](#l1.38) check = {'__name__': '__phello__.spam', '__file__': '<frozen>',[](#l1.39) '__package__': '__phello__',[](#l1.40)
@@ -38,12 +41,15 @@ class LoaderTests(abc.LoaderTests): self.assertEqual(attr_value, value, "for phello.spam.%s, %r != %r" % (attr, attr_value, value))
self.assertEqual(stdout.getvalue(), 'Hello world!\n')[](#l1.45)
with util.uncache('__hello__'):[](#l1.48)
with util.uncache('__hello__'), captured_stdout() as stdout:[](#l1.49) module1 = machinery.FrozenImporter.load_module('__hello__')[](#l1.50) module2 = machinery.FrozenImporter.load_module('__hello__')[](#l1.51) self.assertTrue(module1 is module2)[](#l1.52)
self.assertEqual(stdout.getvalue(),[](#l1.53)
'Hello world!\nHello world!\n')[](#l1.54)
def test_state_after_failure(self): # No way to trigger an error in a frozen module. @@ -62,10 +68,12 @@ class InspectLoaderTests(unittest.TestCa def test_get_code(self): # Make sure that the code object is good. name = 'hello'
code = machinery.FrozenImporter.get_code(name)[](#l1.62)
mod = imp.new_module(name)[](#l1.63)
exec(code, mod.__dict__)[](#l1.64)
self.assertTrue(hasattr(mod, 'initialized'))[](#l1.65)
with captured_stdout() as stdout:[](#l1.66)
code = machinery.FrozenImporter.get_code(name)[](#l1.67)
mod = imp.new_module(name)[](#l1.68)
exec(code, mod.__dict__)[](#l1.69)
self.assertTrue(hasattr(mod, 'initialized'))[](#l1.70)
self.assertEqual(stdout.getvalue(), 'Hello world!\n')[](#l1.71)