Skip to content

Commit 42c2e06

Browse files
committed
pythongh-140824: _math extension
1 parent 88953d5 commit 42c2e06

8 files changed

Lines changed: 61 additions & 46 deletions

File tree

Lib/math/__init__.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
math module -- Mathematical functions
3+
"""
4+
5+
from _math import *
6+
7+
# gh-140824: Fix module name for pickle
8+
def patch_module(objs, module):
9+
for obj in objs:
10+
if not hasattr(obj, "__module__"):
11+
continue
12+
obj.__module__ = module
13+
patch_module([obj for name, obj in globals().items()
14+
if not name.startswith('_')], 'math')
15+
16+
from _math_integer import comb, factorial, gcd, isqrt, lcm, perm
17+
patch_module([comb, factorial, gcd, isqrt, lcm, perm], 'math.integer')
18+
19+
del patch_module

Lib/math/integer.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""
2+
math.integer module -- integer-specific mathematics functions
3+
"""
4+
5+
from _math_integer import *

Lib/test/test_math.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2413,6 +2413,24 @@ def assertEqualSign(self, x, y):
24132413
self.assertEqual(x, y)
24142414
self.assertEqual(math.copysign(1.0, x), math.copysign(1.0, y))
24152415

2416+
def test_module(self):
2417+
# gh-140824: _math and _math_integer extensions are exported as math
2418+
# and math.integer names.
2419+
math_integer_names = set('comb factorial gcd isqrt lcm perm'.split())
2420+
for name in dir(math):
2421+
if name.startswith('_'):
2422+
continue
2423+
obj = getattr(math, name)
2424+
if not hasattr(obj, '__module__'):
2425+
continue
2426+
2427+
if name in math_integer_names:
2428+
module = 'math.integer'
2429+
else:
2430+
module = 'math'
2431+
with self.subTest(name=name):
2432+
self.assertEqual(obj.__module__, module)
2433+
24162434

24172435
class IsCloseTests(unittest.TestCase):
24182436
isclose = math.isclose # subclasses should override this

Modules/Setup

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ PYTHONPATH=$(COREPYTHONPATH)
155155
#array arraymodule.c
156156
#binascii binascii.c
157157
#cmath cmathmodule.c
158-
#math mathmodule.c
158+
#_math mathmodule.c
159159
#_math_integer mathintegermodule.c
160160
#mmap mmapmodule.c
161161
#select selectmodule.c

Modules/Setup.stdlib.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
@MODULE__ZONEINFO_TRUE@_zoneinfo _zoneinfo.c
5353

5454
# needs libm
55-
@MODULE_MATH_TRUE@math mathmodule.c
55+
@MODULE__MATH_TRUE@_math mathmodule.c
5656
@MODULE_CMATH_TRUE@cmath cmathmodule.c
5757
@MODULE__STATISTICS_TRUE@_statistics _statisticsmodule.c
5858

Modules/mathmodule.c

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2976,7 +2976,6 @@ math_ulp_impl(PyObject *module, double x)
29762976
static int
29772977
math_exec(PyObject *module)
29782978
{
2979-
29802979
if (PyModule_Add(module, "pi", PyFloat_FromDouble(Py_MATH_PI)) < 0) {
29812980
return -1;
29822981
}
@@ -2993,32 +2992,6 @@ math_exec(PyObject *module)
29932992
if (PyModule_Add(module, "nan", PyFloat_FromDouble(fabs(Py_NAN))) < 0) {
29942993
return -1;
29952994
}
2996-
2997-
PyObject *intmath = PyImport_ImportModule("_math_integer");
2998-
if (!intmath) {
2999-
return -1;
3000-
}
3001-
#define IMPORT_FROM_INTMATH(NAME) do { \
3002-
if (PyModule_Add(module, #NAME, \
3003-
PyObject_GetAttrString(intmath, #NAME)) < 0) { \
3004-
Py_DECREF(intmath); \
3005-
return -1; \
3006-
} \
3007-
} while(0)
3008-
3009-
IMPORT_FROM_INTMATH(comb);
3010-
IMPORT_FROM_INTMATH(factorial);
3011-
IMPORT_FROM_INTMATH(gcd);
3012-
IMPORT_FROM_INTMATH(isqrt);
3013-
IMPORT_FROM_INTMATH(lcm);
3014-
IMPORT_FROM_INTMATH(perm);
3015-
if (_PyImport_SetModuleString("math.integer", intmath) < 0) {
3016-
Py_DECREF(intmath);
3017-
return -1;
3018-
}
3019-
if (PyModule_Add(module, "integer", intmath) < 0) {
3020-
return -1;
3021-
}
30222995
return 0;
30232996
}
30242997

@@ -3095,15 +3068,15 @@ PyDoc_STRVAR(module_doc,
30953068

30963069
static struct PyModuleDef mathmodule = {
30973070
PyModuleDef_HEAD_INIT,
3098-
.m_name = "math",
3071+
.m_name = "_math",
30993072
.m_doc = module_doc,
31003073
.m_size = 0,
31013074
.m_methods = math_methods,
31023075
.m_slots = math_slots,
31033076
};
31043077

31053078
PyMODINIT_FUNC
3106-
PyInit_math(void)
3079+
PyInit__math(void)
31073080
{
31083081
return PyModuleDef_Init(&mathmodule);
31093082
}

configure

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

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7913,7 +7913,7 @@ PY_STDLIB_MOD([_posixshmem],
79137913
dnl needs libm
79147914
PY_STDLIB_MOD_SIMPLE([_statistics], [], [$LIBM])
79157915
PY_STDLIB_MOD_SIMPLE([cmath], [], [$LIBM])
7916-
PY_STDLIB_MOD_SIMPLE([math], [], [$LIBM])
7916+
PY_STDLIB_MOD_SIMPLE([_math], [], [$LIBM])
79177917

79187918
dnl needs libm and on some platforms librt
79197919
PY_STDLIB_MOD_SIMPLE([_datetime], [], [$TIMEMODULE_LIB $LIBM])

0 commit comments

Comments
 (0)