@@ -21,12 +21,11 @@ def deprecated():
2121def fresh (name , * , oldapi = False ):
2222 with util .uncache (name ):
2323 with import_helper .frozen_modules ():
24- with captured_stdout () as stdout :
25- if oldapi :
26- with deprecated ():
27- yield stdout
28- else :
29- yield stdout
24+ if oldapi :
25+ with deprecated ():
26+ yield
27+ else :
28+ yield
3029
3130
3231class ExecModuleTests (abc .LoaderTests ):
@@ -44,8 +43,10 @@ def exec_module(self, name):
4443 module .__spec__ = spec
4544 assert not hasattr (module , 'initialized' )
4645
47- with fresh (name ) as stdout :
46+ with fresh (name ):
4847 self .machinery .FrozenImporter .exec_module (module )
48+ with captured_stdout () as stdout :
49+ module .main ()
4950
5051 self .assertTrue (module .initialized )
5152 self .assertTrue (hasattr (module , '__spec__' ))
@@ -119,8 +120,10 @@ def test_unloadable(self):
119120class LoaderTests (abc .LoaderTests ):
120121
121122 def load_module (self , name ):
122- with fresh (name , oldapi = True ) as stdout :
123+ with fresh (name , oldapi = True ):
123124 module = self .machinery .FrozenImporter .load_module (name )
125+ with captured_stdout () as stdout :
126+ module .main ()
124127 return module , stdout
125128
126129 def test_module (self ):
@@ -165,15 +168,18 @@ def test_lacking_parent(self):
165168 self .assertFalse (hasattr (module , '__file__' ))
166169
167170 def test_module_reuse (self ):
168- with fresh ('__hello__' , oldapi = True ) as stdout :
171+ with fresh ('__hello__' , oldapi = True ):
169172 module1 = self .machinery .FrozenImporter .load_module ('__hello__' )
170173 module2 = self .machinery .FrozenImporter .load_module ('__hello__' )
174+ with captured_stdout () as stdout :
175+ module1 .main ()
176+ module2 .main ()
171177 self .assertIs (module1 , module2 )
172178 self .assertEqual (stdout .getvalue (),
173179 'Hello world!\n Hello world!\n ' )
174180
175181 def test_module_repr (self ):
176- with fresh ('__hello__' , oldapi = True ) as stdout :
182+ with fresh ('__hello__' , oldapi = True ):
177183 module = self .machinery .FrozenImporter .load_module ('__hello__' )
178184 repr_str = self .machinery .FrozenImporter .module_repr (module )
179185 self .assertEqual (repr_str ,
@@ -203,11 +209,12 @@ class InspectLoaderTests:
203209 def test_get_code (self ):
204210 # Make sure that the code object is good.
205211 name = '__hello__'
212+ with import_helper .frozen_modules ():
213+ code = self .machinery .FrozenImporter .get_code (name )
214+ mod = types .ModuleType (name )
215+ exec (code , mod .__dict__ )
206216 with captured_stdout () as stdout :
207- with import_helper .frozen_modules ():
208- code = self .machinery .FrozenImporter .get_code (name )
209- mod = types .ModuleType (name )
210- exec (code , mod .__dict__ )
217+ mod .main ()
211218 self .assertTrue (hasattr (mod , 'initialized' ))
212219 self .assertEqual (stdout .getvalue (), 'Hello world!\n ' )
213220
0 commit comments