From b466f5a2359a37b0ee68752ba5d1fbded4d577aa Mon Sep 17 00:00:00 2001 From: tonghuaroot Date: Tue, 24 Nov 2026 12:08:42 +0800 Subject: [PATCH 1/2] gh-162531: clean up obsolete pyo references --- InternalDocs/interpreter.md | 7 +++--- Lib/zipimport.py | 22 +++++-------------- Makefile.pre.in | 4 ++-- ...-11-24-12-10-57.gh-issue-162531.Gw3bY8.rst | 2 ++ 4 files changed, 13 insertions(+), 22 deletions(-) create mode 100644 Misc/NEWS.d/next/Build/2026-11-24-12-10-57.gh-issue-162531.Gw3bY8.rst diff --git a/InternalDocs/interpreter.md b/InternalDocs/interpreter.md index fa89996aa654291..936a3848d68aa82 100644 --- a/InternalDocs/interpreter.md +++ b/InternalDocs/interpreter.md @@ -332,14 +332,13 @@ argument in a special way (like `FORMAT_VALUE` or `MAKE_FUNCTION`). If you make a change here that can affect the output of bytecode that is already in existence and you do not change the magic number, make -sure to delete your old .py(c|o) files! Even though you will end up changing +sure to delete your old .pyc files! Even though you will end up changing the magic number if you change the bytecode, while you are debugging your work you may be changing the bytecode output without constantly bumping up the magic number. This can leave you with stale .pyc files that will not be recreated. -Running `find . -name '*.py[co]' -exec rm -f '{}' +` should delete all .pyc -files you have, forcing new ones to be created and thus allow you test out your -new bytecode properly. Run `make regen-importlib` for updating the +Run `make pycremoval` to delete the .pyc files in your source tree before +testing your new bytecode. Run `make regen-importlib` for updating the bytecode of frozen importlib files. You have to run `make` again after this to recompile the generated C files. diff --git a/Lib/zipimport.py b/Lib/zipimport.py index 09a85f6d7f58e86..ba608e39a82e6cb 100644 --- a/Lib/zipimport.py +++ b/Lib/zipimport.py @@ -235,11 +235,7 @@ def __repr__(self): return f'' -# _zip_searchorder defines how we search for a module in the Zip -# archive: we first search for a package __init__, then for -# non-package .pyc, and .py entries. The .pyc entries -# are swapped by initzipimport() if we run in optimized mode. Also, -# '/' is replaced by path_sep there. +# Search for packages before modules, and for bytecode before source. _zip_searchorder = ( (path_sep + '__init__.pyc', True, True), (path_sep + '__init__.py', False, True), @@ -570,8 +566,7 @@ def _get_zlib_decompress_func(): global _importing_zlib if _importing_zlib: - # Someone has a zlib.py[co] in their Zip file - # let's avoid a stack overflow. + # Importing zlib from the archive would recurse. _bootstrap._verbose_message('zipimport: zlib UNAVAILABLE') raise ZipImportError("can't decompress data; zlib not available") @@ -601,8 +596,7 @@ def _get_zstd_decompressor_class(): global _importing_zstd if _importing_zstd: - # Someone has a _zstd.py[co] in their Zip file - # let's avoid a stack overflow. + # Importing _zstd from the archive would recurse. _bootstrap._verbose_message("zipimport: zstd UNAVAILABLE") raise ZipImportError("can't decompress data; zstd not available") @@ -705,9 +699,7 @@ def _eq_mtime(t1, t2): return abs(t1 - t2) <= 1 -# Given the contents of a .py[co] file, unmarshal the data -# and return the code object. Raises ImportError it the magic word doesn't -# match, or if the recorded .py[co] metadata does not match the source. +# Unmarshal the code object from a .pyc file. def _unmarshal_code(self, pathname, fullpath, fullname, data): exc_details = { 'name': fullname, @@ -781,8 +773,7 @@ def _parse_dostime(d, t): # or (0, 0) if no source is available. def _get_mtime_and_size_of_source(self, path): try: - # strip 'c' or 'o' from *.py[co] - assert path[-1:] in ('c', 'o') + assert path.endswith('.pyc') path = path[:-1] toc_entry = self._get_files()[path] # fetch the time stamp of the .py file for comparison @@ -799,8 +790,7 @@ def _get_mtime_and_size_of_source(self, path): # contents of the matching .py file, or None if no source # is available. def _get_pyc_source(self, path): - # strip 'c' or 'o' from *.py[co] - assert path[-1:] in ('c', 'o') + assert path.endswith('.pyc') path = path[:-1] try: diff --git a/Makefile.pre.in b/Makefile.pre.in index 650838d68286c73..a2ea11f926dd231 100644 --- a/Makefile.pre.in +++ b/Makefile.pre.in @@ -3334,7 +3334,7 @@ TAGS:: .PHONY: pycremoval pycremoval: -find $(srcdir) -depth -name '__pycache__' -exec rm -rf {} ';' - -find $(srcdir) -name '*.py[co]' -exec rm -f {} ';' + -find $(srcdir) -name '*.pyc' -exec rm -f {} ';' .PHONY: rmtestturds rmtestturds: @@ -3362,7 +3362,7 @@ clean-retain-profile: pycremoval find . -name '*.lst' -exec rm -f {} ';' find build -name 'fficonfig.h' -exec rm -f {} ';' || true find build -name '*.py' -exec rm -f {} ';' || true - find build -name '*.py[co]' -exec rm -f {} ';' || true + find build -name '*.pyc' -exec rm -f {} ';' || true -rm -f pybuilddir.txt -rm -f _bootstrap_python -rm -rf web_example web_embed_test python.mjs python.wasm python*.symbols python*.map diff --git a/Misc/NEWS.d/next/Build/2026-11-24-12-10-57.gh-issue-162531.Gw3bY8.rst b/Misc/NEWS.d/next/Build/2026-11-24-12-10-57.gh-issue-162531.Gw3bY8.rst new file mode 100644 index 000000000000000..f8c3c59a7a8f545 --- /dev/null +++ b/Misc/NEWS.d/next/Build/2026-11-24-12-10-57.gh-issue-162531.Gw3bY8.rst @@ -0,0 +1,2 @@ +``make pycremoval`` no longer removes legacy ``.pyo`` files outside +``__pycache__`` directories. From 5c6892b0d7a238716b4b57c44a651679b5ab28df Mon Sep 17 00:00:00 2001 From: tonghuaroot Date: Tue, 24 Nov 2026 22:59:39 +0800 Subject: [PATCH 2/2] gh-162531: clean up remaining build and installer pyo references --- .gitignore | 1 - Lib/zipimport.py | 16 ++++++++++++---- Mac/BuildScript/build-installer.py | 2 +- Mac/Extras.install.py | 1 - Mac/Makefile.in | 2 +- Makefile.pre.in | 2 +- PCbuild/rmpyc.py | 3 +-- Tools/buildbot/clean.bat | 4 ++-- 8 files changed, 18 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index 91659803f5faf2e..d68162ed78dde09 100644 --- a/.gitignore +++ b/.gitignore @@ -17,7 +17,6 @@ *.orig *.pyc *.pyd -*.pyo *.rej *.swp *.pickle diff --git a/Lib/zipimport.py b/Lib/zipimport.py index ba608e39a82e6cb..28df28433440747 100644 --- a/Lib/zipimport.py +++ b/Lib/zipimport.py @@ -235,7 +235,9 @@ def __repr__(self): return f'' -# Search for packages before modules, and for bytecode before source. +# _zip_searchorder defines how we search for a module in the Zip +# archive: we first search for a package __init__, then for +# non-package .pyc, and .py entries. _zip_searchorder = ( (path_sep + '__init__.pyc', True, True), (path_sep + '__init__.py', False, True), @@ -566,7 +568,8 @@ def _get_zlib_decompress_func(): global _importing_zlib if _importing_zlib: - # Importing zlib from the archive would recurse. + # Someone has a zlib.pyc in their Zip file + # let's avoid a stack overflow. _bootstrap._verbose_message('zipimport: zlib UNAVAILABLE') raise ZipImportError("can't decompress data; zlib not available") @@ -596,7 +599,8 @@ def _get_zstd_decompressor_class(): global _importing_zstd if _importing_zstd: - # Importing _zstd from the archive would recurse. + # Someone has a _zstd.pyc in their Zip file + # let's avoid a stack overflow. _bootstrap._verbose_message("zipimport: zstd UNAVAILABLE") raise ZipImportError("can't decompress data; zstd not available") @@ -699,7 +703,9 @@ def _eq_mtime(t1, t2): return abs(t1 - t2) <= 1 -# Unmarshal the code object from a .pyc file. +# Given the contents of a .pyc file, unmarshal the data +# and return the code object. Raises ImportError if the magic word doesn't +# match, or if the recorded .pyc hash fails validation. def _unmarshal_code(self, pathname, fullpath, fullname, data): exc_details = { 'name': fullname, @@ -773,6 +779,7 @@ def _parse_dostime(d, t): # or (0, 0) if no source is available. def _get_mtime_and_size_of_source(self, path): try: + # strip 'c' from *.pyc assert path.endswith('.pyc') path = path[:-1] toc_entry = self._get_files()[path] @@ -790,6 +797,7 @@ def _get_mtime_and_size_of_source(self, path): # contents of the matching .py file, or None if no source # is available. def _get_pyc_source(self, path): + # strip 'c' from *.pyc assert path.endswith('.pyc') path = path[:-1] diff --git a/Mac/BuildScript/build-installer.py b/Mac/BuildScript/build-installer.py index c45430243343cc7..1dfa5175cf09e41 100755 --- a/Mac/BuildScript/build-installer.py +++ b/Mac/BuildScript/build-installer.py @@ -1553,7 +1553,7 @@ def buildInstaller(): # Zap all compiled files for dirpath, _, filenames in os.walk(os.path.join(WORKDIR, '_root')): for fn in filenames: - if fn.endswith('.pyc') or fn.endswith('.pyo'): + if fn.endswith('.pyc'): os.unlink(os.path.join(dirpath, fn)) outdir = os.path.join(WORKDIR, 'installer') diff --git a/Mac/Extras.install.py b/Mac/Extras.install.py index 41bfa53d6163079..227aa11cd3dc414 100644 --- a/Mac/Extras.install.py +++ b/Mac/Extras.install.py @@ -15,7 +15,6 @@ def isclean(name): if name.endswith('~'): return 0 if name.endswith('.BAK'): return 0 if name.endswith('.pyc'): return 0 - if name.endswith('.pyo'): return 0 if name.endswith('.orig'): return 0 return 1 diff --git a/Mac/Makefile.in b/Mac/Makefile.in index 32e3a0f55c5d717..dfeafc04ff3bd06 100644 --- a/Mac/Makefile.in +++ b/Mac/Makefile.in @@ -204,7 +204,7 @@ install_Python: case $$i in \ *CVS) ;; \ *.svn) ;; \ - *.py[co]) ;; \ + *.pyc) ;; \ *.orig) ;; \ *~) ;; \ *idx) \ diff --git a/Makefile.pre.in b/Makefile.pre.in index a2ea11f926dd231..4d0b92e88690e88 100644 --- a/Makefile.pre.in +++ b/Makefile.pre.in @@ -2887,7 +2887,7 @@ libinstall: all $(srcdir)/Modules/xxmodule.c do \ case $$i in \ *CVS) ;; \ - *.py[co]) ;; \ + *.pyc) ;; \ *.orig) ;; \ *~) ;; \ *) \ diff --git a/PCbuild/rmpyc.py b/PCbuild/rmpyc.py index 0b58f687729f7e7..410b01220f6e0bd 100644 --- a/PCbuild/rmpyc.py +++ b/PCbuild/rmpyc.py @@ -8,8 +8,7 @@ def deltree(root): npyc = 0 for root, dirs, files in os.walk(root): for name in files: - # to be thorough - if name.endswith(('.pyc', '.pyo')): + if name.endswith('.pyc'): npyc += 1 os.remove(join(root, name)) diff --git a/Tools/buildbot/clean.bat b/Tools/buildbot/clean.bat index fe252a916b1c244..02368a779a2122d 100644 --- a/Tools/buildbot/clean.bat +++ b/Tools/buildbot/clean.bat @@ -9,8 +9,8 @@ echo Deleting build call "%pcbuild%\build.bat" -t Clean -k %* call "%pcbuild%\build.bat" -t Clean -k -d %* -echo Deleting .pyc/.pyo files ... -del /s "%root%\Lib\*.pyc" "%root%\Lib\*.pyo" +echo Deleting .pyc files ... +del /s "%root%\Lib\*.pyc" echo Deleting test leftovers ... rmdir /s /q "%root%\build"