Skip to content

Commit 3814e20

Browse files
bpo-45019: Clean up the frozen __hello__ module. (gh-28374)
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
1 parent d081eab commit 3814e20

File tree

12 files changed

+76
-50
lines changed

12 files changed

+76
-50
lines changed

Lib/__hello__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
initialized = True
2+
3+
def main():
4+
print("Hello world!")
5+
6+
if __name__ == '__main__':
7+
main()

Lib/ctypes/test/test_values.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import unittest
88
import sys
99
from ctypes import *
10-
from test.support import import_helper, captured_stdout
10+
from test.support import import_helper
1111

1212
import _ctypes_test
1313

Lib/test/test_frozen.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ def test_frozen(self):
2121
if name in sys.modules:
2222
del sys.modules[name]
2323
with import_helper.frozen_modules():
24-
with captured_stdout() as out:
25-
import __hello__
24+
import __hello__
25+
with captured_stdout() as out:
26+
__hello__.main()
2627
self.assertEqual(out.getvalue(), 'Hello world!\n')
2728

2829

Lib/test/test_importlib/frozen/test_loader.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@ def deprecated():
2121
def 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

3231
class 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):
119120
class 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!\nHello 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

Makefile.pre.in

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -783,8 +783,8 @@ Python/frozen_modules/posixpath.h: Programs/_freeze_module Lib/posixpath.py
783783
Python/frozen_modules/stat.h: Programs/_freeze_module Lib/stat.py
784784
$(srcdir)/Programs/_freeze_module stat $(srcdir)/Lib/stat.py $(srcdir)/Python/frozen_modules/stat.h
785785

786-
Python/frozen_modules/hello.h: Programs/_freeze_module Tools/freeze/flag.py
787-
$(srcdir)/Programs/_freeze_module hello $(srcdir)/Tools/freeze/flag.py $(srcdir)/Python/frozen_modules/hello.h
786+
Python/frozen_modules/__hello__.h: Programs/_freeze_module Lib/__hello__.py
787+
$(srcdir)/Programs/_freeze_module __hello__ $(srcdir)/Lib/__hello__.py $(srcdir)/Python/frozen_modules/__hello__.h
788788

789789
# END: freezing modules
790790

@@ -1030,7 +1030,7 @@ FROZEN_FILES = \
10301030
$(srcdir)/Python/frozen_modules/ntpath.h \
10311031
$(srcdir)/Python/frozen_modules/posixpath.h \
10321032
$(srcdir)/Python/frozen_modules/stat.h \
1033-
$(srcdir)/Python/frozen_modules/hello.h
1033+
$(srcdir)/Python/frozen_modules/__hello__.h
10341034
# End FROZEN_FILES
10351035

10361036
Python/frozen.o: $(FROZEN_FILES)

PCbuild/_freeze_module.vcxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,10 @@
245245
<IntFile>$(IntDir)zipimport.g.h</IntFile>
246246
<OutFile>$(PySourcePath)Python\frozen_modules\zipimport.h</OutFile>
247247
</None>
248-
<None Include="..\Tools\freeze\flag.py">
249-
<ModName>hello</ModName>
250-
<IntFile>$(IntDir)hello.g.h</IntFile>
251-
<OutFile>$(PySourcePath)Python\frozen_modules\hello.h</OutFile>
248+
<None Include="..\Lib\__hello__.py">
249+
<ModName>__hello__</ModName>
250+
<IntFile>$(IntDir)__hello__.g.h</IntFile>
251+
<OutFile>$(PySourcePath)Python\frozen_modules\__hello__.h</OutFile>
252252
</None>
253253
<!-- END frozen modules -->
254254
</ItemGroup>

PCbuild/_freeze_module.vcxproj.filters

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<None Include="..\Lib\zipimport.py">
2626
<Filter>Python Files</Filter>
2727
</None>
28-
<None Include="..\Tools\freeze\flag.py">
28+
<None Include="..\Lib\__hello__.py">
2929
<Filter>Python Files</Filter>
3030
</None>
3131
<!-- END frozen modules -->

Python/frozen.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
#include "frozen_modules/ntpath.h"
5050
#include "frozen_modules/posixpath.h"
5151
#include "frozen_modules/stat.h"
52-
#include "frozen_modules/hello.h"
52+
#include "frozen_modules/__hello__.h"
5353
/* End includes */
5454

5555
/* Note that a negative size indicates a package. */
@@ -74,9 +74,9 @@ static const struct _frozen _PyImport_FrozenModules[] = {
7474
{"stat", _Py_M__stat, (int)sizeof(_Py_M__stat)},
7575

7676
/* Test module */
77-
{"__hello__", _Py_M__hello, (int)sizeof(_Py_M__hello)},
78-
{"__phello__", _Py_M__hello, -(int)sizeof(_Py_M__hello)},
79-
{"__phello__.spam", _Py_M__hello, (int)sizeof(_Py_M__hello)},
77+
{"__hello__", _Py_M____hello__, (int)sizeof(_Py_M____hello__)},
78+
{"__phello__", _Py_M____hello__, -(int)sizeof(_Py_M____hello__)},
79+
{"__phello__.spam", _Py_M____hello__, (int)sizeof(_Py_M____hello__)},
8080
{0, 0, 0} /* sentinel */
8181
};
8282

Python/frozen_modules/MANIFEST

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/frozen_modules/__hello__.h

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/frozen_modules/hello.h

Lines changed: 0 additions & 14 deletions
This file was deleted.

Tools/scripts/freeze_modules.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ def find_tool():
8282
'stat',
8383
]),
8484
('Test module', [
85-
'hello : __hello__ = ' + os.path.join(TOOLS_DIR, 'freeze', 'flag.py'),
86-
'hello : <__phello__>',
87-
'hello : __phello__.spam',
85+
'__hello__',
86+
'__hello__ : <__phello__>',
87+
'__hello__ : __phello__.spam',
8888
]),
8989
]
9090
ESSENTIAL = {
@@ -578,7 +578,7 @@ def regen_pcbuild(modules):
578578
for src in _iter_sources(modules):
579579
# For now we only require the essential frozen modules on Windows.
580580
# See bpo-45186 and bpo-45188.
581-
if src.id not in ESSENTIAL and src.id != 'hello':
581+
if src.id not in ESSENTIAL and src.id != '__hello__':
582582
continue
583583
pyfile = relpath_for_windows_display(src.pyfile, ROOT_DIR)
584584
header = relpath_for_windows_display(src.frozenfile, ROOT_DIR)

0 commit comments

Comments
 (0)