gh-140824: Build the math module as an extension package#152352
Open
serhiy-storchaka wants to merge 1 commit into
Open
gh-140824: Build the math module as an extension package#152352serhiy-storchaka wants to merge 1 commit into
serhiy-storchaka wants to merge 1 commit into
Conversation
Build math as an extension package (math/__init__ and math/integer) so that math.integer is a genuine extension submodule instead of the _math_integer extension module patched at runtime to look like one. This drops the sys.modules['_math_integer'] leak and gives math.integer's functions a correct __module__. Modules/makesetup now accepts dotted module names, mapping each to its registration name, on-disk path and init symbol; a package initializer is written with an explicit __init__ leaf (math.__init__). Makefile.pre.in keeps sub-directories when staging and installing shared modules, and check_extension_modules.py and generate_stdlib_module_names.py handle package and dotted submodule names. The package works both for shared builds and for builds where math is a builtin (Windows, WebAssembly): math's exec slot sets __path__ when the import machinery does not, and the static inittab uses a symbol derived from the full dotted name (PyInit_math_integer) to stay unique, selected with Py_BUILD_CORE_BUILTIN. ExtensionFileLoader.is_package() now mirrors FileLoader by returning False for the "__init__" name itself, so that modulefinder (used by Tools/freeze) does not recurse on an extension package. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
00db9e8 to
9dc3148
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Build
mathas a real extension package (math/__init__andmath/integer), so thatmath.integeris a genuine extension submodule instead of the_math_integerextension module patched at runtime to look like a submodule.This removes the leftover
sys.modules['_math_integer'], givesmath.integer's functions a correct__module__, and drops the__name__/__module__and_PyImport_SetModuleStringworkarounds.How
Modules/makesetupnow accepts dotted module names and decouples three attributes that were previously tied to a single Setup token: the registration name (math.integer), the on-disk path (math/integer), and the C init symbol. A package initializer is written with an explicit__init__leaf (math.__init__).Makefile.pre.inpreserves sub-directories when staging into the build-tree dynload dir and when installing intolib-dynload.Tools/build/check_extension_modules.pyandTools/build/generate_stdlib_module_names.pyunderstand package and dotted submodule names (the submodule is excluded fromsys.stdlib_module_names).configure.ac/Setup.stdlib.inbuild the package through the normal module-detection machinery (MODULE_MATH_INTEGER_*).Shared vs. builtin
Works both for the usual shared build and for builds where
mathis compiled into the interpreter as a builtin (Windows, WebAssembly):__path__from the import machinery, somath's exec slot sets an empty__path__to mark it as a package and let the builtinmath.integerbe found.PyInit_<last component>(PyInit_integer) for the dynamic loader, but in an all-static build everyPyInit_*shares one symbol namespace where a bare last-component name would clash across packages. So the static inittab uses a symbol derived from the full dotted name (PyInit_math_integer), selected withPy_BUILD_CORE_BUILTIN; only one of the two symbols is compiled per build mode.PC/config.cregisters the Windows builtin accordingly.Testing
make,checksharedmods, andtest_math/test_cmath/test_importlib/test_sysall pass.math) build: verified thatimport math.integerworks and that only the uniquePyInit_math_integersymbol is present in the binary.PC/config.cmirrors the verified builtin path.🤖 Generated with Claude Code
https://claude.ai/code/session_01R4YFmYjuT6XeiobFzeQjDp