Skip to content

bpo-45019: Add a tool to generate list of modules to include for frozen modules #27980

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 30 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
a10471a
Clean up _freeze_importlib.c.
ericsnowcurrently Jun 10, 2021
6252a37
_freeze_importlib -> _freeze_module
ericsnowcurrently Aug 10, 2021
b230af8
Add tooling to freeze stdlib modules.
ericsnowcurrently Jun 8, 2021
e4ef8fc
Support freezing a package's submodules.
ericsnowcurrently Aug 10, 2021
6bcc8c9
Use freeze_modules.py for all frozen modules.
ericsnowcurrently Aug 11, 2021
1cae771
Regen frozen.c and Makefile.pre.in.
ericsnowcurrently Aug 11, 2021
ba9417c
Add the updating_file_with_tmpfile() "tool".
ericsnowcurrently Aug 11, 2021
6dcb926
Regen frozen.c in place.
ericsnowcurrently Aug 11, 2021
8a2b4b7
Clean up.
ericsnowcurrently Aug 11, 2021
8ad3c1d
Add --no-regen.
ericsnowcurrently Aug 11, 2021
1e88d34
Be consistent about the frozen module filename (frozen_*.h).
ericsnowcurrently Aug 12, 2021
c4874f2
Use consistently named make targets for regen-frozen-*.
ericsnowcurrently Aug 12, 2021
06af093
Drop the stdlib placeholders.
ericsnowcurrently Aug 12, 2021
3a036b7
Simplify FROZEN a little.
ericsnowcurrently Aug 12, 2021
6a27a1d
Combine FROZEN and MODULES.
ericsnowcurrently Aug 12, 2021
e3e07a9
Drop the frozen module groups.
ericsnowcurrently Aug 12, 2021
3d2ebdb
Fix a typo.
ericsnowcurrently Aug 12, 2021
9a5141f
Fix the Makefile.
ericsnowcurrently Aug 24, 2021
3687fda
Make freeze_modules.py less fragile.
gvanrossum Aug 24, 2021
1331a77
Simplify specs.
ericsnowcurrently Aug 25, 2021
30972b2
Explicitly freeze modules in Makefile.
ericsnowcurrently Aug 25, 2021
7a4a876
Fix a typo.
ericsnowcurrently Aug 25, 2021
1306a4e
Fix dependencies.
ericsnowcurrently Aug 25, 2021
f724deb
Drop some dead code.
ericsnowcurrently Aug 25, 2021
9a4d120
Fix another typo.
ericsnowcurrently Aug 25, 2021
54b0116
Generate the frozen file in-place rather than with a temp file.
ericsnowcurrently Aug 25, 2021
16adb8e
Update Windows build config.
ericsnowcurrently Aug 24, 2021
2d51cba
Move frozen modules into their own directory.
ericsnowcurrently Aug 26, 2021
5cfd853
Add a NEWS entry.
ericsnowcurrently Aug 26, 2021
0905b51
Fix a typo.
ericsnowcurrently Aug 26, 2021
55e1ffc
Fix outdated comments.
ericsnowcurrently Aug 30, 2021
b970fa3
Avoid buffer overflows.
ericsnowcurrently Aug 30, 2021
99daab4
Freeze the modules when running "make regen-frozen".
ericsnowcurrently Aug 30, 2021
10a14de
Be more verbose about what is running in freeze_modules.py.
ericsnowcurrently Aug 30, 2021
2651962
Update the explanation in frozen.c.
ericsnowcurrently Aug 30, 2021
7406c58
Mark all frozen modules as "generated".
ericsnowcurrently Aug 30, 2021
f4c136e
Drop a superfluous comment.
ericsnowcurrently Aug 30, 2021
2730169
Drop the CLI.
ericsnowcurrently Aug 30, 2021
d84fd85
Use update_file_with_tmpfile() in updating_file_with_tmpfile().
ericsnowcurrently Aug 30, 2021
cd8fe0a
Do not update the file if there was an error.
ericsnowcurrently Aug 30, 2021
9a97726
Fix the check_generated_files CI job.
ericsnowcurrently Aug 30, 2021
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Modules/Setup.config
Modules/Setup.local
Modules/config.c
Modules/ld_so_aix
Programs/_freeze_importlib
Programs/_freeze_module
Programs/_testembed
PC/python_nt*.h
PC/pythonnt_rc*.h
Expand Down
2 changes: 1 addition & 1 deletion Doc/c-api/init.rst
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ to 1 and ``-bb`` sets :c:data:`Py_BytesWarningFlag` to 2.
Suppress error messages when calculating the module search path in
:c:func:`Py_GetPath`.

Private flag used by ``_freeze_importlib`` and ``frozenmain`` programs.
Private flag used by ``_freeze_module`` and ``frozenmain`` programs.

.. c:var:: int Py_HashRandomizationFlag

Expand Down
92 changes: 52 additions & 40 deletions Makefile.pre.in
Original file line number Diff line number Diff line change
Expand Up @@ -573,8 +573,8 @@ coverage-lcov:
@echo "lcov report at $(COVERAGE_REPORT)/index.html"
@echo

# Force regeneration of parser and importlib
coverage-report: regen-token regen-importlib
# Force regeneration of parser and frozen modules
coverage-report: regen-token regen-frozen
@ # build with coverage info
$(MAKE) coverage
@ # run tests, ignore failures
Expand Down Expand Up @@ -733,45 +733,59 @@ Programs/_testembed: Programs/_testembed.o $(LIBRARY_DEPS)
$(LINKCC) $(PY_CORE_LDFLAGS) $(LINKFORSHARED) -o $@ Programs/_testembed.o $(BLDLIBRARY) $(LIBS) $(MODLIBS) $(SYSLIBS)

############################################################################
# Importlib
# frozen modules (including importlib)

Programs/_freeze_importlib.o: Programs/_freeze_importlib.c Makefile
Programs/_freeze_module.o: Programs/_freeze_module.c Makefile

Programs/_freeze_importlib: Programs/_freeze_importlib.o $(LIBRARY_OBJS_OMIT_FROZEN)
$(LINKCC) $(PY_CORE_LDFLAGS) -o $@ Programs/_freeze_importlib.o $(LIBRARY_OBJS_OMIT_FROZEN) $(LIBS) $(MODLIBS) $(SYSLIBS)
Programs/_freeze_module: Programs/_freeze_module.o $(LIBRARY_OBJS_OMIT_FROZEN)
$(LINKCC) $(PY_CORE_LDFLAGS) -o $@ Programs/_freeze_module.o $(LIBRARY_OBJS_OMIT_FROZEN) $(LIBS) $(MODLIBS) $(SYSLIBS)

Tools/scripts/freeze_modules.py: Programs/_freeze_module

.PHONY: regen-frozen
regen-frozen: Tools/scripts/freeze_modules.py $(FROZEN_FILES)
$(PYTHON_FOR_REGEN) $(srcdir)/Tools/scripts/freeze_modules.py --no-freeze

# BEGIN: freezing modules

Python/frozen_modules/importlib__bootstrap.h: $(srcdir)/Programs/_freeze_module $(srcdir)/Lib/importlib/_bootstrap.py
$(srcdir)/Programs/_freeze_module importlib._bootstrap \
$(srcdir)/Lib/importlib/_bootstrap.py \
$(srcdir)/Python/frozen_modules/importlib__bootstrap.h

Python/frozen_modules/importlib__bootstrap_external.h: $(srcdir)/Programs/_freeze_module $(srcdir)/Lib/importlib/_bootstrap_external.py
$(srcdir)/Programs/_freeze_module importlib._bootstrap_external \
$(srcdir)/Lib/importlib/_bootstrap_external.py \
$(srcdir)/Python/frozen_modules/importlib__bootstrap_external.h

Python/frozen_modules/zipimport.h: $(srcdir)/Programs/_freeze_module $(srcdir)/Lib/zipimport.py
$(srcdir)/Programs/_freeze_module zipimport \
$(srcdir)/Lib/zipimport.py \
$(srcdir)/Python/frozen_modules/zipimport.h

Python/frozen_modules/hello.h: $(srcdir)/Programs/_freeze_module $(srcdir)/Tools/freeze/flag.py
$(srcdir)/Programs/_freeze_module hello \
$(srcdir)/Tools/freeze/flag.py \
$(srcdir)/Python/frozen_modules/hello.h

# END: freezing modules

# We keep this renamed target around for folks with muscle memory.
.PHONY: regen-importlib
regen-importlib: Programs/_freeze_importlib
# Regenerate Python/importlib_external.h
# from Lib/importlib/_bootstrap_external.py using _freeze_importlib
./Programs/_freeze_importlib importlib._bootstrap_external \
$(srcdir)/Lib/importlib/_bootstrap_external.py \
$(srcdir)/Python/importlib_external.h.new
$(UPDATE_FILE) $(srcdir)/Python/importlib_external.h $(srcdir)/Python/importlib_external.h.new
# Regenerate Python/importlib.h from Lib/importlib/_bootstrap.py
# using _freeze_importlib
./Programs/_freeze_importlib importlib._bootstrap \
$(srcdir)/Lib/importlib/_bootstrap.py \
$(srcdir)/Python/importlib.h.new
$(UPDATE_FILE) $(srcdir)/Python/importlib.h $(srcdir)/Python/importlib.h.new
# Regenerate Python/importlib_zipimport.h from Lib/zipimport.py
# using _freeze_importlib
./Programs/_freeze_importlib zipimport \
$(srcdir)/Lib/zipimport.py \
$(srcdir)/Python/importlib_zipimport.h.new
$(UPDATE_FILE) $(srcdir)/Python/importlib_zipimport.h $(srcdir)/Python/importlib_zipimport.h.new
regen-importlib: regen-frozen

############################################################################
# ABI

regen-limited-abi: all
$(RUNSHARED) ./$(BUILDPYTHON) $(srcdir)/Tools/scripts/stable_abi.py --generate-all $(srcdir)/Misc/stable_abi.txt


############################################################################
# Regenerate all generated files

regen-all: regen-opcode regen-opcode-targets regen-typeslots \
regen-token regen-ast regen-keyword regen-importlib clinic \
regen-pegen-metaparser regen-pegen regen-frozen regen-test-frozenmain
regen-token regen-ast regen-keyword regen-frozen clinic \
regen-pegen-metaparser regen-pegen regen-test-frozenmain
@echo
@echo "Note: make regen-stdlib-module-names and autoconf should be run manually"

Expand Down Expand Up @@ -883,15 +897,6 @@ regen-opcode:
$(srcdir)/Include/opcode.h.new
$(UPDATE_FILE) $(srcdir)/Include/opcode.h $(srcdir)/Include/opcode.h.new

.PHONY: regen-frozen
regen-frozen: Programs/_freeze_importlib
# Regenerate code for frozen module "__hello__".
./Programs/_freeze_importlib hello \
$(srcdir)/Tools/freeze/flag.py \
$(srcdir)/Python/frozen_hello.h.new
$(UPDATE_FILE) $(srcdir)/Python/frozen_hello.h \
$(srcdir)/Python/frozen_hello.h.new

.PHONY: regen-token
regen-token:
# Regenerate Doc/library/token-list.inc from Grammar/Tokens
Expand Down Expand Up @@ -994,8 +999,15 @@ regen-opcode-targets:
Python/ceval.o: $(srcdir)/Python/opcode_targets.h $(srcdir)/Python/ceval_gil.h \
$(srcdir)/Python/condvar.h

Python/frozen.o: $(srcdir)/Python/importlib.h $(srcdir)/Python/importlib_external.h \
$(srcdir)/Python/importlib_zipimport.h $(srcdir)/Python/frozen_hello.h
# FROZEN_FILES is auto-generated by Tools/scripts/freeze_modules.py.
FROZEN_FILES = \
$(srcdir)/Python/frozen_modules/importlib__bootstrap.h \
$(srcdir)/Python/frozen_modules/importlib__bootstrap_external.h \
$(srcdir)/Python/frozen_modules/zipimport.h \
$(srcdir)/Python/frozen_modules/hello.h
# End FROZEN_FILES

Python/frozen.o: $(FROZEN_FILES)

# Generate DTrace probe macros, then rename them (PYTHON_ -> PyDTrace_) to
# follow our naming conventions. dtrace(1) uses the output filename to generate
Expand Down Expand Up @@ -1917,7 +1929,7 @@ clean-retain-profile: pycremoval
find build -name '*.py[co]' -exec rm -f {} ';' || true
-rm -f pybuilddir.txt
-rm -f Lib/lib2to3/*Grammar*.pickle
-rm -f Programs/_testembed Programs/_freeze_importlib
-rm -f Programs/_testembed Programs/_freeze_module
-find build -type f -a ! -name '*.gc??' -exec rm -f {} ';'
-rm -f Include/pydtrace_probes.h
-rm -f profile-gen-stamp
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Generate lines in relevant files for frozen modules. Up until now each of
the files had to be edited manually. This change makes it easier to add to
and modify the frozen modules.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
<PropertyGroup Label="Globals">
<ProjectGuid>{19C0C13F-47CA-4432-AFF3-799A296A4DDC}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>_freeze_importlib</RootNamespace>
<RootNamespace>_freeze_module</RootNamespace>
<SupportPGO>false</SupportPGO>
</PropertyGroup>
<Import Project="python.props" />
Expand All @@ -95,7 +95,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\Programs\_freeze_importlib.c" />
<ClCompile Include="..\Programs\_freeze_module.c" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="pythoncore.vcxproj">
Expand All @@ -108,31 +108,33 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<!-- BEGIN frozen modules -->
<None Include="..\Lib\importlib\_bootstrap.py">
<ModName>importlib._bootstrap</ModName>
<IntFile>$(IntDir)importlib.g.h</IntFile>
<OutFile>$(PySourcePath)Python\importlib.h</OutFile>
<IntFile>$(IntDir)importlib__bootstrap.g.h</IntFile>
<OutFile>$(PySourcePath)Python\frozen_modules\importlib__bootstrap.h</OutFile>
</None>
<None Include="..\Lib\importlib\_bootstrap_external.py">
<ModName>importlib._bootstrap_external</ModName>
<IntFile>$(IntDir)importlib_external.g.h</IntFile>
<OutFile>$(PySourcePath)Python\importlib_external.h</OutFile>
<IntFile>$(IntDir)importlib__bootstrap_external.g.h</IntFile>
<OutFile>$(PySourcePath)Python\frozen_modules\importlib__bootstrap_external.h</OutFile>
</None>
<None Include="..\Lib\zipimport.py">
<ModName>zipimport</ModName>
<IntFile>$(IntDir)importlib_zipimport.g.h</IntFile>
<OutFile>$(PySourcePath)Python\importlib_zipimport.h</OutFile>
<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)frozen_hello.g.h</IntFile>
<OutFile>$(PySourcePath)Python\frozen_hello.h</OutFile>
<IntFile>$(IntDir)ello.g.h</IntFile>
<OutFile>$(PySourcePath)Python\frozen_modules\hello.h</OutFile>
</None>
<!-- END frozen modules -->
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
<Target Name="_RebuildImportLib">
<Target Name="_RebuildFrozen">
<Exec Command='"$(TargetPath)" "%(None.ModName)" "%(None.FullPath)" "%(None.IntFile)"' />

<Copy SourceFiles="%(None.IntFile)"
Expand All @@ -143,15 +145,18 @@

<Message Text="Updated files: @(_Updated->'%(Filename)%(Extension)',', ')"
Condition="'@(_Updated)' != ''" Importance="high" />
<Warning Text="Frozen importlib files were updated. Please rebuild to pick up the changes.%0D%0A%0D%0AIf you are not developing on Windows but you see this error on a continuous integration build, please run 'make regen-all' and commit anything that changes."
<Warning Text="Frozen modules (e.g. importlib) were updated. Please rebuild to pick up the changes.%0D%0A%0D%0AIf you are not developing on Windows but you see this error on a continuous integration build, please run 'make regen-all' and commit anything that changes."
Condition="'@(_Updated)' != '' and $(Configuration) == 'Debug'" />
<Error Text="Frozen importlib files were updated. Please rebuild to pick up the changes.%0D%0A%0D%0AIf you are not developing on Windows but you see this error on a continuous integration build, please run 'make regen-all' and commit anything that changes."
<Error Text="Frozen (e.g. importlib) files were updated. Please rebuild to pick up the changes.%0D%0A%0D%0AIf you are not developing on Windows but you see this error on a continuous integration build, please run 'make regen-all' and commit anything that changes."
Condition="'@(_Updated)' != '' and $(Configuration) == 'Release'" />
</Target>
<Target Name="RebuildFrozen" AfterTargets="AfterBuild" Condition="$(Configuration) == 'Debug' or $(Configuration) == 'Release'"
DependsOnTargets="_RebuildFrozen">
</Target>
<Target Name="RebuildImportLib" AfterTargets="AfterBuild" Condition="$(Configuration) == 'Debug' or $(Configuration) == 'Release'"
DependsOnTargets="_RebuildImportLib">
DependsOnTargets="_RebuildFrozen">
</Target>
<Target Name="_CleanImportLib" BeforeTargets="CoreClean">
<Target Name="_CleanFrozen" BeforeTargets="CoreClean">
<ItemGroup>
<Clean Include="%(None.IntFile)" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,24 @@
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\Programs\_freeze_importlib.c">
<ClCompile Include="..\Programs\_freeze_module.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<!-- BEGIN frozen modules -->
<None Include="..\Lib\importlib\_bootstrap.py">
<Filter>Source Files</Filter>
<Filter>Python Files</Filter>
</None>
<None Include="..\Lib\importlib\_bootstrap_external.py">
<Filter>Python Files</Filter>
</None>
<None Include="..\Lib\zipimport.py">
<Filter>Python Files</Filter>
</None>
<None Include="..\Lib\importlib\_bootstrap_external.py">
<None Include="..\Tools\freeze\flag.py">
<Filter>Python Files</Filter>
</None>
<!-- END frozen modules -->
</ItemGroup>
</Project>
</Project>
4 changes: 2 additions & 2 deletions PCbuild/pcbuild.proj
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@
<BuildInParallel>false</BuildInParallel>
</Projects>

<!-- _freeze_importlib -->
<Projects2 Condition="$(Platform) != 'ARM' and $(Platform) != 'ARM64'" Include="_freeze_importlib.vcxproj" />
<!-- _freeze_module -->
<Projects2 Condition="$(Platform) != 'ARM' and $(Platform) != 'ARM64'" Include="_freeze_module.vcxproj" />
<!-- python[w].exe -->
<Projects2 Include="python.vcxproj;pythonw.vcxproj" />
<Projects2 Include="python_uwp.vcxproj;pythonw_uwp.vcxproj" Condition="$(IncludeUwp)" />
Expand Down
2 changes: 1 addition & 1 deletion PCbuild/pcbuild.sln
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "pywlauncher", "pywlauncher.
{7B2727B5-5A3F-40EE-A866-43A13CD31446} = {7B2727B5-5A3F-40EE-A866-43A13CD31446}
EndProjectSection
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "_freeze_importlib", "_freeze_importlib.vcxproj", "{19C0C13F-47CA-4432-AFF3-799A296A4DDC}"
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "_freeze_module", "_freeze_module.vcxproj", "{19C0C13F-47CA-4432-AFF3-799A296A4DDC}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "_overlapped", "_overlapped.vcxproj", "{EB6E69DD-04BF-4543-9B92-49FAABCEAC2E}"
EndProject
Expand Down
7 changes: 4 additions & 3 deletions PCbuild/readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,10 @@ _testembed

These are miscellaneous sub-projects that don't really fit the other
categories:
_freeze_importlib
_freeze_importlib.exe, used to regenerate Python\importlib.h after
changes have been made to Lib\importlib\_bootstrap.py
_freeze_module
_freeze_module.exe, used to regenerate frozen modules in Python\
after changes have been made to the corresponding source files
(e.g. Lib\importlib\_bootstrap.py).
pyshellext
pyshellext.dll, the shell extension deployed with the launcher
python3dll
Expand Down
Loading