Skip to content

Commit a5bac9c

Browse files
Fix the module name.
1 parent 3b7bd99 commit a5bac9c

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

Lib/test/test_math_integer.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,5 +388,16 @@ class MathTests(IntMathTests):
388388
import math as module
389389

390390

391+
class MiscTests(unittest.TestCase):
392+
393+
def test_module_name(self):
394+
import math.integer
395+
self.assertEqual(math.integer.__name__, 'math.integer')
396+
for name in dir(math.integer):
397+
if not name.startswith('_'):
398+
obj = getattr(math.integer, name)
399+
self.assertEqual(obj.__module__, 'math.integer')
400+
401+
391402
if __name__ == '__main__':
392403
unittest.main()

Modules/mathintegermodule.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,7 +1236,39 @@ static PyMethodDef math_integer_methods[] = {
12361236
{NULL, NULL} /* sentinel */
12371237
};
12381238

1239+
static int
1240+
math_integer_exec(PyObject *module)
1241+
{
1242+
/* Fix the __name__ attribute of the module and the __module__ attribute
1243+
* of its functions.
1244+
*/
1245+
PyObject *name = PyUnicode_FromString("math.integer");
1246+
if (name == NULL) {
1247+
return -1;
1248+
}
1249+
if (PyObject_SetAttrString(module, "__name__", name) < 0) {
1250+
Py_DECREF(name);
1251+
return -1;
1252+
}
1253+
for (const PyMethodDef *m = math_integer_methods; m->ml_name; m++) {
1254+
PyObject *obj = PyObject_GetAttrString(module, m->ml_name);
1255+
if (obj == NULL) {
1256+
Py_DECREF(name);
1257+
return -1;
1258+
}
1259+
if (PyObject_SetAttrString(obj, "__module__", name) < 0) {
1260+
Py_DECREF(name);
1261+
Py_DECREF(obj);
1262+
return -1;
1263+
}
1264+
Py_DECREF(obj);
1265+
}
1266+
Py_DECREF(name);
1267+
return 0;
1268+
}
1269+
12391270
static PyModuleDef_Slot math_integer_slots[] = {
1271+
{Py_mod_exec, math_integer_exec},
12401272
{Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
12411273
{Py_mod_gil, Py_MOD_GIL_NOT_USED},
12421274
{0, NULL}

0 commit comments

Comments
 (0)