Skip to content

bpo-45019: Clean up the frozen __hello__ module. #28374

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Lib/__hello__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
initialized = True

def main():
print("Hello world!")

if __name__ == '__main__':
main()
2 changes: 1 addition & 1 deletion Lib/ctypes/test/test_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions Lib/test/test_frozen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')


Expand Down
35 changes: 21 additions & 14 deletions Lib/test/test_importlib/frozen/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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__'))
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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')

Expand Down
6 changes: 3 additions & 3 deletions Makefile.pre.in
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions PCbuild/_freeze_module.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,10 @@
<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>
Expand Down
2 changes: 1 addition & 1 deletion PCbuild/_freeze_module.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -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 -->
Expand Down
8 changes: 4 additions & 4 deletions Python/frozen.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand All @@ -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 */
};

Expand Down
6 changes: 3 additions & 3 deletions Python/frozen_modules/MANIFEST

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions Python/frozen_modules/__hello__.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 0 additions & 14 deletions Python/frozen_modules/hello.h

This file was deleted.

8 changes: 4 additions & 4 deletions Tools/scripts/freeze_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ def find_tool():
'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 = {
Expand Down Expand Up @@ -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)
Expand Down