From 12e1e0e36ce8f26aca90253ab0ef9252fa626681 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Wed, 15 Sep 2021 11:51:47 -0600 Subject: [PATCH 1/4] Use _imp to silence a deprecation warning. --- Lib/ctypes/test/test_values.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/ctypes/test/test_values.py b/Lib/ctypes/test/test_values.py index 03bc9bba317571..4f525cde69e863 100644 --- a/Lib/ctypes/test/test_values.py +++ b/Lib/ctypes/test/test_values.py @@ -2,7 +2,7 @@ A testcase which accesses *values* in a dll. """ -import imp +import _imp import importlib.util import unittest import sys @@ -81,7 +81,7 @@ class struct_frozen(Structure): self.assertIsNone(spec.submodule_search_locations) with import_helper.frozen_modules(): - expected = imp._frozen_module_names() + expected = _imp._frozen_module_names() self.maxDiff = None self.assertEqual(modules, expected, "PyImport_FrozenModules example " "in Doc/library/ctypes.rst may be out of date") From 549b655132b8f1d54366f46975b6d233d74116b5 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Wed, 15 Sep 2021 11:54:40 -0600 Subject: [PATCH 2/4] Hide text printed during test_frozentable. --- Lib/ctypes/test/test_values.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Lib/ctypes/test/test_values.py b/Lib/ctypes/test/test_values.py index 4f525cde69e863..f50e7cadf4dc65 100644 --- a/Lib/ctypes/test/test_values.py +++ b/Lib/ctypes/test/test_values.py @@ -73,7 +73,9 @@ class struct_frozen(Structure): self.assertTrue([entry.code[i] for i in range(abs(entry.size))]) # Check the module's package-ness. with import_helper.frozen_modules(): - spec = importlib.util.find_spec(modname) + # Hide the message written by the __hello__ module. + with captured_stdout(): + spec = importlib.util.find_spec(modname) if entry.size < 0: # It's a package. self.assertIsNotNone(spec.submodule_search_locations) From 6290438a6e2694128145099af8d38ed2b8b89b92 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Wed, 15 Sep 2021 12:46:13 -0600 Subject: [PATCH 3/4] Remove stdout side effects from importing __hello__. --- Lib/ctypes/test/test_values.py | 5 ++- Lib/test/test_frozen.py | 5 +-- Lib/test/test_importlib/frozen/test_loader.py | 35 +++++++++++-------- Python/frozen_modules/MANIFEST | 6 ++-- Python/frozen_modules/hello.h | 31 ++++++++++------ Tools/freeze/flag.py | 7 +++- Tools/freeze/hello.py | 6 +++- 7 files changed, 61 insertions(+), 34 deletions(-) diff --git a/Lib/ctypes/test/test_values.py b/Lib/ctypes/test/test_values.py index f50e7cadf4dc65..1e258f0e425bb2 100644 --- a/Lib/ctypes/test/test_values.py +++ b/Lib/ctypes/test/test_values.py @@ -7,7 +7,7 @@ import unittest import sys from ctypes import * -from test.support import import_helper, captured_stdout +from test.support import import_helper import _ctypes_test @@ -74,8 +74,7 @@ class struct_frozen(Structure): # Check the module's package-ness. with import_helper.frozen_modules(): # Hide the message written by the __hello__ module. - with captured_stdout(): - spec = importlib.util.find_spec(modname) + spec = importlib.util.find_spec(modname) if entry.size < 0: # It's a package. self.assertIsNotNone(spec.submodule_search_locations) diff --git a/Lib/test/test_frozen.py b/Lib/test/test_frozen.py index 52d8f7ced96803..3d212b9202f90a 100644 --- a/Lib/test/test_frozen.py +++ b/Lib/test/test_frozen.py @@ -21,8 +21,9 @@ def test_frozen(self): if name in sys.modules: del sys.modules[name] with import_helper.frozen_modules(): - with captured_stdout() as out: - import __hello__ + import __hello__ + with captured_stdout() as out: + __hello__.main() self.assertEqual(out.getvalue(), 'Hello world!\n') diff --git a/Lib/test/test_importlib/frozen/test_loader.py b/Lib/test/test_importlib/frozen/test_loader.py index 1b0a56f7e8afa9..cfa5e5bb31bea9 100644 --- a/Lib/test/test_importlib/frozen/test_loader.py +++ b/Lib/test/test_importlib/frozen/test_loader.py @@ -21,12 +21,11 @@ def deprecated(): def fresh(name, *, oldapi=False): with util.uncache(name): with import_helper.frozen_modules(): - with captured_stdout() as stdout: - if oldapi: - with deprecated(): - yield stdout - else: - yield stdout + if oldapi: + with deprecated(): + yield + else: + yield class ExecModuleTests(abc.LoaderTests): @@ -44,8 +43,10 @@ def exec_module(self, name): module.__spec__ = spec assert not hasattr(module, 'initialized') - with fresh(name) as stdout: + with fresh(name): self.machinery.FrozenImporter.exec_module(module) + with captured_stdout() as stdout: + module.main() self.assertTrue(module.initialized) self.assertTrue(hasattr(module, '__spec__')) @@ -119,8 +120,10 @@ def test_unloadable(self): class LoaderTests(abc.LoaderTests): def load_module(self, name): - with fresh(name, oldapi=True) as stdout: + with fresh(name, oldapi=True): module = self.machinery.FrozenImporter.load_module(name) + with captured_stdout() as stdout: + module.main() return module, stdout def test_module(self): @@ -165,15 +168,18 @@ def test_lacking_parent(self): self.assertFalse(hasattr(module, '__file__')) def test_module_reuse(self): - with fresh('__hello__', oldapi=True) as stdout: + with fresh('__hello__', oldapi=True): module1 = self.machinery.FrozenImporter.load_module('__hello__') module2 = self.machinery.FrozenImporter.load_module('__hello__') + with captured_stdout() as stdout: + module1.main() + module2.main() self.assertIs(module1, module2) self.assertEqual(stdout.getvalue(), 'Hello world!\nHello world!\n') def test_module_repr(self): - with fresh('__hello__', oldapi=True) as stdout: + with fresh('__hello__', oldapi=True): module = self.machinery.FrozenImporter.load_module('__hello__') repr_str = self.machinery.FrozenImporter.module_repr(module) self.assertEqual(repr_str, @@ -203,11 +209,12 @@ class InspectLoaderTests: def test_get_code(self): # Make sure that the code object is good. name = '__hello__' + with import_helper.frozen_modules(): + code = self.machinery.FrozenImporter.get_code(name) + mod = types.ModuleType(name) + exec(code, mod.__dict__) with captured_stdout() as stdout: - with import_helper.frozen_modules(): - code = self.machinery.FrozenImporter.get_code(name) - mod = types.ModuleType(name) - exec(code, mod.__dict__) + mod.main() self.assertTrue(hasattr(mod, 'initialized')) self.assertEqual(stdout.getvalue(), 'Hello world!\n') diff --git a/Python/frozen_modules/MANIFEST b/Python/frozen_modules/MANIFEST index 811dc5b48e4233..3557dcbba286e0 100644 --- a/Python/frozen_modules/MANIFEST +++ b/Python/frozen_modules/MANIFEST @@ -15,6 +15,6 @@ genericpath no genericpath.h ntpath no ntpath.h 15b9b80fa9c2 posixpath no posixpath.h 3fc077252afd stat no stat.h 27c32a0815c2 -__hello__ no Tools/freeze/flag.py hello.h af6fb665713f -__phello__ YES Tools/freeze/flag.py hello.h af6fb665713f -__phello__.spam no Tools/freeze/flag.py hello.h af6fb665713f +__hello__ no Tools/freeze/flag.py hello.h 57921e98bf5c +__phello__ YES Tools/freeze/flag.py hello.h 57921e98bf5c +__phello__.spam no Tools/freeze/flag.py hello.h 57921e98bf5c diff --git a/Python/frozen_modules/hello.h b/Python/frozen_modules/hello.h index 2658c05886a6db..239fba7b255fb0 100644 --- a/Python/frozen_modules/hello.h +++ b/Python/frozen_modules/hello.h @@ -1,14 +1,25 @@ /* Auto-generated by Programs/_freeze_module.c */ const unsigned char _Py_M__hello[] = { 99,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0, - 0,0,0,0,0,115,16,0,0,0,100,0,90,0,101,1, - 100,1,131,1,1,0,100,2,83,0,41,3,84,122,12,72, - 101,108,108,111,32,119,111,114,108,100,33,78,41,2,90,11, - 105,110,105,116,105,97,108,105,122,101,100,218,5,112,114,105, - 110,116,169,0,243,0,0,0,0,122,14,60,102,114,111,122, - 101,110,32,104,101,108,108,111,62,218,8,60,109,111,100,117, - 108,101,62,114,3,0,0,0,1,0,0,0,243,4,0,0, - 0,4,0,12,1,114,4,0,0,0,115,16,0,0,0,15, - 19,1,12,1,6,7,21,1,22,1,22,1,22,1,22,114, - 2,0,0,0, + 0,0,0,0,0,115,32,0,0,0,100,0,90,0,100,1, + 132,0,90,1,101,2,100,2,107,2,114,14,101,1,131,0, + 1,0,100,3,83,0,100,3,83,0,41,4,84,99,0,0, + 0,0,0,0,0,0,0,0,0,0,2,0,0,0,3,0, + 0,0,115,12,0,0,0,116,0,100,1,131,1,1,0,100, + 0,83,0,41,2,78,122,12,72,101,108,108,111,32,119,111, + 114,108,100,33,41,1,218,5,112,114,105,110,116,169,0,243, + 0,0,0,0,250,14,60,102,114,111,122,101,110,32,104,101, + 108,108,111,62,218,4,109,97,105,110,114,4,0,0,0,3, + 0,0,0,243,2,0,0,0,12,1,114,5,0,0,0,115, + 12,0,0,0,5,10,11,25,5,26,5,26,5,26,5,26, + 114,2,0,0,0,90,8,95,95,109,97,105,110,95,95,78, + 41,3,90,11,105,110,105,116,105,97,108,105,122,101,100,114, + 4,0,0,0,218,8,95,95,110,97,109,101,95,95,114,1, + 0,0,0,114,2,0,0,0,114,3,0,0,0,218,8,60, + 109,111,100,117,108,101,62,114,7,0,0,0,1,0,0,0, + 115,10,0,0,0,4,0,6,2,8,3,10,1,4,255,115, + 8,0,0,0,4,0,6,3,6,2,16,1,115,32,0,0, + 0,15,19,1,12,1,26,1,26,1,26,4,12,16,26,4, + 26,1,11,5,9,5,11,5,11,5,11,5,11,1,11,1, + 11,114,2,0,0,0, }; diff --git a/Tools/freeze/flag.py b/Tools/freeze/flag.py index 1cefa0a2fd87c0..d37bd2766ac1c6 100644 --- a/Tools/freeze/flag.py +++ b/Tools/freeze/flag.py @@ -1,2 +1,7 @@ initialized = True -print("Hello world!") + +def main(): + print("Hello world!") + +if __name__ == '__main__': + main() diff --git a/Tools/freeze/hello.py b/Tools/freeze/hello.py index d7a3729c9b42cc..a45932c498e5bc 100644 --- a/Tools/freeze/hello.py +++ b/Tools/freeze/hello.py @@ -1 +1,5 @@ -print('Hello world...') +def main(): + print("Hello world!") + +if __name__ == '__main__': + main() From b45e0d63fb4badf80138a18b46bf74080816c7b6 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Wed, 15 Sep 2021 13:05:08 -0600 Subject: [PATCH 4/4] Roll back the changes with the __hello__ module. --- Lib/ctypes/test/test_values.py | 3 +- Lib/test/test_frozen.py | 5 ++- Lib/test/test_importlib/frozen/test_loader.py | 35 ++++++++----------- Python/frozen_modules/MANIFEST | 6 ++-- Python/frozen_modules/hello.h | 31 ++++++---------- Tools/freeze/flag.py | 7 +--- Tools/freeze/hello.py | 6 +--- 7 files changed, 32 insertions(+), 61 deletions(-) diff --git a/Lib/ctypes/test/test_values.py b/Lib/ctypes/test/test_values.py index 1e258f0e425bb2..4f525cde69e863 100644 --- a/Lib/ctypes/test/test_values.py +++ b/Lib/ctypes/test/test_values.py @@ -7,7 +7,7 @@ import unittest import sys from ctypes import * -from test.support import import_helper +from test.support import import_helper, captured_stdout import _ctypes_test @@ -73,7 +73,6 @@ class struct_frozen(Structure): self.assertTrue([entry.code[i] for i in range(abs(entry.size))]) # Check the module's package-ness. with import_helper.frozen_modules(): - # Hide the message written by the __hello__ module. spec = importlib.util.find_spec(modname) if entry.size < 0: # It's a package. diff --git a/Lib/test/test_frozen.py b/Lib/test/test_frozen.py index 3d212b9202f90a..52d8f7ced96803 100644 --- a/Lib/test/test_frozen.py +++ b/Lib/test/test_frozen.py @@ -21,9 +21,8 @@ def test_frozen(self): if name in sys.modules: del sys.modules[name] with import_helper.frozen_modules(): - import __hello__ - with captured_stdout() as out: - __hello__.main() + with captured_stdout() as out: + import __hello__ self.assertEqual(out.getvalue(), 'Hello world!\n') diff --git a/Lib/test/test_importlib/frozen/test_loader.py b/Lib/test/test_importlib/frozen/test_loader.py index cfa5e5bb31bea9..1b0a56f7e8afa9 100644 --- a/Lib/test/test_importlib/frozen/test_loader.py +++ b/Lib/test/test_importlib/frozen/test_loader.py @@ -21,11 +21,12 @@ def deprecated(): def fresh(name, *, oldapi=False): with util.uncache(name): with import_helper.frozen_modules(): - if oldapi: - with deprecated(): - yield - else: - yield + with captured_stdout() as stdout: + if oldapi: + with deprecated(): + yield stdout + else: + yield stdout class ExecModuleTests(abc.LoaderTests): @@ -43,10 +44,8 @@ def exec_module(self, name): module.__spec__ = spec assert not hasattr(module, 'initialized') - with fresh(name): + with fresh(name) as stdout: self.machinery.FrozenImporter.exec_module(module) - with captured_stdout() as stdout: - module.main() self.assertTrue(module.initialized) self.assertTrue(hasattr(module, '__spec__')) @@ -120,10 +119,8 @@ def test_unloadable(self): class LoaderTests(abc.LoaderTests): def load_module(self, name): - with fresh(name, oldapi=True): + with fresh(name, oldapi=True) as stdout: module = self.machinery.FrozenImporter.load_module(name) - with captured_stdout() as stdout: - module.main() return module, stdout def test_module(self): @@ -168,18 +165,15 @@ def test_lacking_parent(self): self.assertFalse(hasattr(module, '__file__')) def test_module_reuse(self): - with fresh('__hello__', oldapi=True): + with fresh('__hello__', oldapi=True) as stdout: module1 = self.machinery.FrozenImporter.load_module('__hello__') module2 = self.machinery.FrozenImporter.load_module('__hello__') - with captured_stdout() as stdout: - module1.main() - module2.main() self.assertIs(module1, module2) self.assertEqual(stdout.getvalue(), 'Hello world!\nHello world!\n') def test_module_repr(self): - with fresh('__hello__', oldapi=True): + with fresh('__hello__', oldapi=True) as stdout: module = self.machinery.FrozenImporter.load_module('__hello__') repr_str = self.machinery.FrozenImporter.module_repr(module) self.assertEqual(repr_str, @@ -209,12 +203,11 @@ class InspectLoaderTests: def test_get_code(self): # Make sure that the code object is good. name = '__hello__' - with import_helper.frozen_modules(): - code = self.machinery.FrozenImporter.get_code(name) - mod = types.ModuleType(name) - exec(code, mod.__dict__) with captured_stdout() as stdout: - mod.main() + with import_helper.frozen_modules(): + code = self.machinery.FrozenImporter.get_code(name) + mod = types.ModuleType(name) + exec(code, mod.__dict__) self.assertTrue(hasattr(mod, 'initialized')) self.assertEqual(stdout.getvalue(), 'Hello world!\n') diff --git a/Python/frozen_modules/MANIFEST b/Python/frozen_modules/MANIFEST index 3557dcbba286e0..811dc5b48e4233 100644 --- a/Python/frozen_modules/MANIFEST +++ b/Python/frozen_modules/MANIFEST @@ -15,6 +15,6 @@ genericpath no genericpath.h ntpath no ntpath.h 15b9b80fa9c2 posixpath no posixpath.h 3fc077252afd stat no stat.h 27c32a0815c2 -__hello__ no Tools/freeze/flag.py hello.h 57921e98bf5c -__phello__ YES Tools/freeze/flag.py hello.h 57921e98bf5c -__phello__.spam no Tools/freeze/flag.py hello.h 57921e98bf5c +__hello__ no Tools/freeze/flag.py hello.h af6fb665713f +__phello__ YES Tools/freeze/flag.py hello.h af6fb665713f +__phello__.spam no Tools/freeze/flag.py hello.h af6fb665713f diff --git a/Python/frozen_modules/hello.h b/Python/frozen_modules/hello.h index 239fba7b255fb0..2658c05886a6db 100644 --- a/Python/frozen_modules/hello.h +++ b/Python/frozen_modules/hello.h @@ -1,25 +1,14 @@ /* Auto-generated by Programs/_freeze_module.c */ const unsigned char _Py_M__hello[] = { 99,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0, - 0,0,0,0,0,115,32,0,0,0,100,0,90,0,100,1, - 132,0,90,1,101,2,100,2,107,2,114,14,101,1,131,0, - 1,0,100,3,83,0,100,3,83,0,41,4,84,99,0,0, - 0,0,0,0,0,0,0,0,0,0,2,0,0,0,3,0, - 0,0,115,12,0,0,0,116,0,100,1,131,1,1,0,100, - 0,83,0,41,2,78,122,12,72,101,108,108,111,32,119,111, - 114,108,100,33,41,1,218,5,112,114,105,110,116,169,0,243, - 0,0,0,0,250,14,60,102,114,111,122,101,110,32,104,101, - 108,108,111,62,218,4,109,97,105,110,114,4,0,0,0,3, - 0,0,0,243,2,0,0,0,12,1,114,5,0,0,0,115, - 12,0,0,0,5,10,11,25,5,26,5,26,5,26,5,26, - 114,2,0,0,0,90,8,95,95,109,97,105,110,95,95,78, - 41,3,90,11,105,110,105,116,105,97,108,105,122,101,100,114, - 4,0,0,0,218,8,95,95,110,97,109,101,95,95,114,1, - 0,0,0,114,2,0,0,0,114,3,0,0,0,218,8,60, - 109,111,100,117,108,101,62,114,7,0,0,0,1,0,0,0, - 115,10,0,0,0,4,0,6,2,8,3,10,1,4,255,115, - 8,0,0,0,4,0,6,3,6,2,16,1,115,32,0,0, - 0,15,19,1,12,1,26,1,26,1,26,4,12,16,26,4, - 26,1,11,5,9,5,11,5,11,5,11,5,11,1,11,1, - 11,114,2,0,0,0, + 0,0,0,0,0,115,16,0,0,0,100,0,90,0,101,1, + 100,1,131,1,1,0,100,2,83,0,41,3,84,122,12,72, + 101,108,108,111,32,119,111,114,108,100,33,78,41,2,90,11, + 105,110,105,116,105,97,108,105,122,101,100,218,5,112,114,105, + 110,116,169,0,243,0,0,0,0,122,14,60,102,114,111,122, + 101,110,32,104,101,108,108,111,62,218,8,60,109,111,100,117, + 108,101,62,114,3,0,0,0,1,0,0,0,243,4,0,0, + 0,4,0,12,1,114,4,0,0,0,115,16,0,0,0,15, + 19,1,12,1,6,7,21,1,22,1,22,1,22,1,22,114, + 2,0,0,0, }; diff --git a/Tools/freeze/flag.py b/Tools/freeze/flag.py index d37bd2766ac1c6..1cefa0a2fd87c0 100644 --- a/Tools/freeze/flag.py +++ b/Tools/freeze/flag.py @@ -1,7 +1,2 @@ initialized = True - -def main(): - print("Hello world!") - -if __name__ == '__main__': - main() +print("Hello world!") diff --git a/Tools/freeze/hello.py b/Tools/freeze/hello.py index a45932c498e5bc..d7a3729c9b42cc 100644 --- a/Tools/freeze/hello.py +++ b/Tools/freeze/hello.py @@ -1,5 +1 @@ -def main(): - print("Hello world!") - -if __name__ == '__main__': - main() +print('Hello world...')