Skip to content

Commit c432df6

Browse files
gh-111696, PEP 737: Add PyType_GetModuleName() function (#116824)
Co-authored-by: Eric Snow <[email protected]>
1 parent 25cd873 commit c432df6

15 files changed

+48
-27
lines changed

Doc/c-api/type.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,13 @@ Type Objects
193193
194194
.. versionadded:: 3.13
195195
196+
.. c:function:: PyObject* PyType_GetModuleName(PyTypeObject *type)
197+
198+
Return the type's module name. Equivalent to getting the ``type.__module__``
199+
attribute.
200+
201+
.. versionadded:: 3.13
202+
196203
.. c:function:: void* PyType_GetSlot(PyTypeObject *type, int slot)
197204
198205
Return the function pointer stored in the given slot. If the

Doc/data/stable_abi.dat

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

Doc/whatsnew/3.13.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1664,6 +1664,10 @@ New Features
16641664
to ``"builtins"``.
16651665
(Contributed by Victor Stinner in :gh:`111696`.)
16661666

1667+
* Add :c:func:`PyType_GetModuleName` function to get the type's module name.
1668+
Equivalent to getting the ``type.__module__`` attribute.
1669+
(Contributed by Eric Snow and Victor Stinner in :gh:`111696`.)
1670+
16671671

16681672
Porting to Python 3.13
16691673
----------------------

Include/internal/pycore_typeobject.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,6 @@ PyAPI_FUNC(PyObject*) _PySuper_Lookup(PyTypeObject *su_type, PyObject *su_obj,
151151
PyObject *name, int *meth_found);
152152

153153

154-
// This is exported for the _testinternalcapi module.
155-
PyAPI_FUNC(PyObject *) _PyType_GetModuleName(PyTypeObject *);
156-
157-
158154
#ifdef __cplusplus
159155
}
160156
#endif

Include/object.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,8 @@ PyAPI_FUNC(PyObject *) PyType_GetName(PyTypeObject *);
523523
PyAPI_FUNC(PyObject *) PyType_GetQualName(PyTypeObject *);
524524
#endif
525525
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030D0000
526-
PyAPI_FUNC(PyObject *) PyType_GetFullyQualifiedName(PyTypeObject *);
526+
PyAPI_FUNC(PyObject *) PyType_GetFullyQualifiedName(PyTypeObject *type);
527+
PyAPI_FUNC(PyObject *) PyType_GetModuleName(PyTypeObject *type);
527528
#endif
528529
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030C0000
529530
PyAPI_FUNC(PyObject *) PyType_FromMetaclass(PyTypeObject*, PyObject*, PyType_Spec*, PyObject*);

Lib/test/test_capi/test_misc.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,8 +1104,9 @@ def test_get_type_name(self):
11041104
class MyType:
11051105
pass
11061106

1107-
from _testcapi import get_type_name, get_type_qualname, get_type_fullyqualname
1108-
from _testinternalcapi import get_type_module_name
1107+
from _testcapi import (
1108+
get_type_name, get_type_qualname,
1109+
get_type_fullyqualname, get_type_module_name)
11091110

11101111
from collections import OrderedDict
11111112
ht = _testcapi.get_heaptype_for_name()

Lib/test/test_stable_abi_ctypes.py

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Add :c:func:`PyType_GetModuleName` function to get the type's module name.
2+
Equivalent to getting the ``type.__module__`` attribute. Patch by Eric Snow
3+
and Victor Stinner.

Misc/stable_abi.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2498,3 +2498,5 @@
24982498
# "abi-only" since 3.10. (Same story as PyCFunctionFast.)
24992499
[function.PyType_GetFullyQualifiedName]
25002500
added = '3.13'
2501+
[function.PyType_GetModuleName]
2502+
added = '3.13'

Modules/_functoolsmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ partial_repr(partialobject *pto)
402402
goto done;
403403
}
404404

405-
mod = _PyType_GetModuleName(Py_TYPE(pto));
405+
mod = PyType_GetModuleName(Py_TYPE(pto));
406406
if (mod == NULL) {
407407
goto error;
408408
}

Modules/_testcapimodule.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,14 @@ get_type_fullyqualname(PyObject *self, PyObject *type)
622622
}
623623

624624

625+
static PyObject *
626+
get_type_module_name(PyObject *self, PyObject *type)
627+
{
628+
assert(PyType_Check(type));
629+
return PyType_GetModuleName((PyTypeObject *)type);
630+
}
631+
632+
625633
static PyObject *
626634
test_get_type_dict(PyObject *self, PyObject *Py_UNUSED(ignored))
627635
{
@@ -3268,6 +3276,7 @@ static PyMethodDef TestMethods[] = {
32683276
{"get_type_name", get_type_name, METH_O},
32693277
{"get_type_qualname", get_type_qualname, METH_O},
32703278
{"get_type_fullyqualname", get_type_fullyqualname, METH_O},
3279+
{"get_type_module_name", get_type_module_name, METH_O},
32713280
{"test_get_type_dict", test_get_type_dict, METH_NOARGS},
32723281
{"_test_thread_state", test_thread_state, METH_VARARGS},
32733282
#ifndef MS_WINDOWS

Modules/_testinternalcapi.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#include "pycore_pathconfig.h" // _PyPathConfig_ClearGlobal()
2929
#include "pycore_pyerrors.h" // _PyErr_ChainExceptions1()
3030
#include "pycore_pystate.h" // _PyThreadState_GET()
31-
#include "pycore_typeobject.h" // _PyType_GetModuleName()
3231

3332
#include "interpreteridobject.h" // PyInterpreterID_LookUp()
3433

@@ -1631,13 +1630,6 @@ perf_trampoline_set_persist_after_fork(PyObject *self, PyObject *args)
16311630
}
16321631

16331632

1634-
static PyObject *
1635-
get_type_module_name(PyObject *self, PyObject *type)
1636-
{
1637-
assert(PyType_Check(type));
1638-
return _PyType_GetModuleName((PyTypeObject *)type);
1639-
}
1640-
16411633
static PyObject *
16421634
get_rare_event_counters(PyObject *self, PyObject *type)
16431635
{
@@ -1741,7 +1733,6 @@ static PyMethodDef module_functions[] = {
17411733
{"get_crossinterp_data", get_crossinterp_data, METH_VARARGS},
17421734
{"restore_crossinterp_data", restore_crossinterp_data, METH_VARARGS},
17431735
_TESTINTERNALCAPI_TEST_LONG_NUMBITS_METHODDEF
1744-
{"get_type_module_name", get_type_module_name, METH_O},
17451736
{"get_rare_event_counters", get_rare_event_counters, METH_NOARGS},
17461737
{"reset_rare_event_counters", reset_rare_event_counters, METH_NOARGS},
17471738
#ifdef Py_GIL_DISABLED

Objects/typeobject.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,10 +1164,9 @@ type_set_qualname(PyTypeObject *type, PyObject *value, void *context)
11641164
}
11651165

11661166
static PyObject *
1167-
type_module(PyTypeObject *type, void *context)
1167+
type_module(PyTypeObject *type)
11681168
{
11691169
PyObject *mod;
1170-
11711170
if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
11721171
PyObject *dict = lookup_tp_dict(type);
11731172
if (PyDict_GetItemRef(dict, &_Py_ID(__module__), &mod) == 0) {
@@ -1189,6 +1188,12 @@ type_module(PyTypeObject *type, void *context)
11891188
return mod;
11901189
}
11911190

1191+
static PyObject *
1192+
type_get_module(PyTypeObject *type, void *context)
1193+
{
1194+
return type_module(type);
1195+
}
1196+
11921197
static int
11931198
type_set_module(PyTypeObject *type, PyObject *value, void *context)
11941199
{
@@ -1214,7 +1219,7 @@ PyType_GetFullyQualifiedName(PyTypeObject *type)
12141219
return NULL;
12151220
}
12161221

1217-
PyObject *module = type_module(type, NULL);
1222+
PyObject *module = type_module(type);
12181223
if (module == NULL) {
12191224
Py_DECREF(qualname);
12201225
return NULL;
@@ -1722,7 +1727,7 @@ static PyGetSetDef type_getsets[] = {
17221727
{"__qualname__", (getter)type_qualname, (setter)type_set_qualname, NULL},
17231728
{"__bases__", (getter)type_get_bases, (setter)type_set_bases, NULL},
17241729
{"__mro__", (getter)type_get_mro, NULL, NULL},
1725-
{"__module__", (getter)type_module, (setter)type_set_module, NULL},
1730+
{"__module__", (getter)type_get_module, (setter)type_set_module, NULL},
17261731
{"__abstractmethods__", (getter)type_abstractmethods,
17271732
(setter)type_set_abstractmethods, NULL},
17281733
{"__dict__", (getter)type_dict, NULL, NULL},
@@ -1743,7 +1748,7 @@ type_repr(PyObject *self)
17431748
return PyUnicode_FromFormat("<class at %p>", type);
17441749
}
17451750

1746-
PyObject *mod = type_module(type, NULL);
1751+
PyObject *mod = type_module(type);
17471752
if (mod == NULL) {
17481753
PyErr_Clear();
17491754
}
@@ -4734,9 +4739,9 @@ PyType_GetQualName(PyTypeObject *type)
47344739
}
47354740

47364741
PyObject *
4737-
_PyType_GetModuleName(PyTypeObject *type)
4742+
PyType_GetModuleName(PyTypeObject *type)
47384743
{
4739-
return type_module(type, NULL);
4744+
return type_module(type);
47404745
}
47414746

47424747
void *
@@ -5850,7 +5855,7 @@ object_repr(PyObject *self)
58505855
PyObject *mod, *name, *rtn;
58515856

58525857
type = Py_TYPE(self);
5853-
mod = type_module(type, NULL);
5858+
mod = type_module(type);
58545859
if (mod == NULL)
58555860
PyErr_Clear();
58565861
else if (!PyUnicode_Check(mod)) {

PC/python3dll.c

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

Python/crossinterp.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include "pycore_initconfig.h" // _PyStatus_OK()
88
#include "pycore_namespace.h" //_PyNamespace_New()
99
#include "pycore_pyerrors.h" // _PyErr_Clear()
10-
#include "pycore_typeobject.h" // _PyType_GetModuleName()
1110
#include "pycore_weakref.h" // _PyWeakref_GET_REF()
1211

1312

@@ -510,7 +509,7 @@ _excinfo_init_type(struct _excinfo_type *info, PyObject *exc)
510509
}
511510

512511
// __module__
513-
strobj = _PyType_GetModuleName(type);
512+
strobj = PyType_GetModuleName(type);
514513
if (strobj == NULL) {
515514
return -1;
516515
}

0 commit comments

Comments
 (0)