]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-45019: Clean up the frozen __hello__ module. (gh-28374)
authorEric Snow <ericsnowcurrently@gmail.com>
Wed, 15 Sep 2021 20:15:32 +0000 (14:15 -0600)
committerGitHub <noreply@github.com>
Wed, 15 Sep 2021 20:15:32 +0000 (14:15 -0600)
Here's one more small cleanup that should have been in PR gh-28319. We eliminate stdout side-effects from importing the frozen __hello__ module, and update tests accordingly. We also move the module's source file into Lib/ from Toos/freeze/flag.py.

https://bugs.python.org/issue45019

12 files changed:
Lib/__hello__.py [new file with mode: 0644]
Lib/ctypes/test/test_values.py
Lib/test/test_frozen.py
Lib/test/test_importlib/frozen/test_loader.py
Makefile.pre.in
PCbuild/_freeze_module.vcxproj
PCbuild/_freeze_module.vcxproj.filters
Python/frozen.c
Python/frozen_modules/MANIFEST
Python/frozen_modules/__hello__.h [new file with mode: 0644]
Python/frozen_modules/hello.h [deleted file]
Tools/scripts/freeze_modules.py

diff --git a/Lib/__hello__.py b/Lib/__hello__.py
new file mode 100644 (file)
index 0000000..d37bd27
--- /dev/null
@@ -0,0 +1,7 @@
+initialized = True
+
+def main():
+    print("Hello world!")
+
+if __name__ == '__main__':
+    main()
index 4f525cde69e863c353946a2fcac9d3af7ce1271c..96521fd3abce9c61e77286c257ecbbcbe4862c40 100644 (file)
@@ -7,7 +7,7 @@ import importlib.util
 import unittest
 import sys
 from ctypes import *
-from test.support import import_helper, captured_stdout
+from test.support import import_helper
 
 import _ctypes_test
 
index 52d8f7ced96803375fc87f3884e9cfa99aa6f632..3d212b9202f90a2d3ef858819d50f044c013a93d 100644 (file)
@@ -21,8 +21,9 @@ class TestFrozen(unittest.TestCase):
         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')
 
 
index 1b0a56f7e8afa980753c1a58c2e1465d7ec88e1e..cfa5e5bb31bea9a8edf0628f279ae55ac927a06f 100644 (file)
@@ -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 @@ class ExecModuleTests(abc.LoaderTests):
         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 @@ class ExecModuleTests(abc.LoaderTests):
 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 @@ class LoaderTests(abc.LoaderTests):
         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')
 
index b2a8f0ecdddfd6dde57013b05c13bb088151cbf5..3e562d72cfe192dc2fc8106592a6c225d269e2d6 100644 (file)
@@ -783,8 +783,8 @@ Python/frozen_modules/posixpath.h: Programs/_freeze_module Lib/posixpath.py
 Python/frozen_modules/stat.h: Programs/_freeze_module Lib/stat.py
        $(srcdir)/Programs/_freeze_module stat $(srcdir)/Lib/stat.py $(srcdir)/Python/frozen_modules/stat.h
 
-Python/frozen_modules/hello.h: Programs/_freeze_module Tools/freeze/flag.py
-       $(srcdir)/Programs/_freeze_module hello $(srcdir)/Tools/freeze/flag.py $(srcdir)/Python/frozen_modules/hello.h
+Python/frozen_modules/__hello__.h: Programs/_freeze_module Lib/__hello__.py
+       $(srcdir)/Programs/_freeze_module __hello__ $(srcdir)/Lib/__hello__.py $(srcdir)/Python/frozen_modules/__hello__.h
 
 # END: freezing modules
 
@@ -1030,7 +1030,7 @@ FROZEN_FILES = \
                $(srcdir)/Python/frozen_modules/ntpath.h \
                $(srcdir)/Python/frozen_modules/posixpath.h \
                $(srcdir)/Python/frozen_modules/stat.h \
-               $(srcdir)/Python/frozen_modules/hello.h
+               $(srcdir)/Python/frozen_modules/__hello__.h
 # End FROZEN_FILES
 
 Python/frozen.o: $(FROZEN_FILES)
index a87dca75c34fa3095ed8e318455e0cc3a11d2944..e306884029fb06cc0e3a28dea40633814cd76be9 100644 (file)
       <IntFile>$(IntDir)zipimport.g.h</IntFile>
       <OutFile>$(PySourcePath)Python\frozen_modules\zipimport.h</OutFile>
     </None>
-    <None Include="..\Tools\freeze\flag.py">
-      <ModName>hello</ModName>
-      <IntFile>$(IntDir)hello.g.h</IntFile>
-      <OutFile>$(PySourcePath)Python\frozen_modules\hello.h</OutFile>
+    <None Include="..\Lib\__hello__.py">
+      <ModName>__hello__</ModName>
+      <IntFile>$(IntDir)__hello__.g.h</IntFile>
+      <OutFile>$(PySourcePath)Python\frozen_modules\__hello__.h</OutFile>
     </None>
     <!-- END frozen modules -->
   </ItemGroup>
index bed7920fdba638f3579ea72e61e3c28a633b7ebf..6e7c1bba263336d70bf9acae392a0c7d247df370 100644 (file)
@@ -25,7 +25,7 @@
     <None Include="..\Lib\zipimport.py">
       <Filter>Python Files</Filter>
     </None>
-    <None Include="..\Tools\freeze\flag.py">
+    <None Include="..\Lib\__hello__.py">
       <Filter>Python Files</Filter>
     </None>
     <!-- END frozen modules -->
index eef0f453f62607533a0628ba653452a413271cda..cf384d31768eb5b7ec8fb6a47dd55ece43e37a6e 100644 (file)
@@ -49,7 +49,7 @@
 #include "frozen_modules/ntpath.h"
 #include "frozen_modules/posixpath.h"
 #include "frozen_modules/stat.h"
-#include "frozen_modules/hello.h"
+#include "frozen_modules/__hello__.h"
 /* End includes */
 
 /* Note that a negative size indicates a package. */
@@ -74,9 +74,9 @@ static const struct _frozen _PyImport_FrozenModules[] = {
     {"stat", _Py_M__stat, (int)sizeof(_Py_M__stat)},
 
     /* Test module */
-    {"__hello__", _Py_M__hello, (int)sizeof(_Py_M__hello)},
-    {"__phello__", _Py_M__hello, -(int)sizeof(_Py_M__hello)},
-    {"__phello__.spam", _Py_M__hello, (int)sizeof(_Py_M__hello)},
+    {"__hello__", _Py_M____hello__, (int)sizeof(_Py_M____hello__)},
+    {"__phello__", _Py_M____hello__, -(int)sizeof(_Py_M____hello__)},
+    {"__phello__.spam", _Py_M____hello__, (int)sizeof(_Py_M____hello__)},
     {0, 0, 0} /* sentinel */
 };
 
index 811dc5b48e423319092151d25bf7d666acbaa0a4..b723684dc7201c36b02d3256d12b9675d541f345 100644 (file)
@@ -15,6 +15,6 @@ genericpath                  no  <genericpath>                   genericpath.h
 ntpath                       no  <ntpath>                        ntpath.h                        15b9b80fa9c2
 posixpath                    no  <posixpath>                     posixpath.h                     3fc077252afd
 stat                         no  <stat>                          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  <__hello__>                     __hello__.h                     81ba13956479
+__phello__                  YES  <__hello__>                     __hello__.h                     81ba13956479
+__phello__.spam              no  <__hello__>                     __hello__.h                     81ba13956479
diff --git a/Python/frozen_modules/__hello__.h b/Python/frozen_modules/__hello__.h
new file mode 100644 (file)
index 0000000..eea1029
--- /dev/null
@@ -0,0 +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,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,18,60,102,114,111,122,101,110,32,95,95,
+    104,101,108,108,111,95,95,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/Python/frozen_modules/hello.h b/Python/frozen_modules/hello.h
deleted file mode 100644 (file)
index 2658c05..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-/* 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,
-};
index 044ea095fdcd46b8f461f33d5d927efef7644165..89b66169d832becbd575688c2b6f9e773b1081c7 100644 (file)
@@ -82,9 +82,9 @@ FROZEN = [
         'stat',
         ]),
     ('Test module', [
-        'hello : __hello__ = ' + os.path.join(TOOLS_DIR, 'freeze', 'flag.py'),
-        'hello : <__phello__>',
-        'hello : __phello__.spam',
+        '__hello__',
+        '__hello__ : <__phello__>',
+        '__hello__ : __phello__.spam',
         ]),
 ]
 ESSENTIAL = {
@@ -578,7 +578,7 @@ def regen_pcbuild(modules):
     for src in _iter_sources(modules):
         # For now we only require the essential frozen modules on Windows.
         # See bpo-45186 and bpo-45188.
-        if src.id not in ESSENTIAL and src.id != 'hello':
+        if src.id not in ESSENTIAL and src.id != '__hello__':
             continue
         pyfile = relpath_for_windows_display(src.pyfile, ROOT_DIR)
         header = relpath_for_windows_display(src.frozenfile, ROOT_DIR)