gh-111696, PEP 737: Add PyType_GetModuleName() function (#116824)

Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
This commit is contained in:
Victor Stinner 2024-03-14 19:17:43 +01:00 committed by GitHub
parent 25cd8730aa
commit c432df6d56
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
15 changed files with 48 additions and 27 deletions

View File

@ -193,6 +193,13 @@ Type Objects
.. versionadded:: 3.13
.. c:function:: PyObject* PyType_GetModuleName(PyTypeObject *type)
Return the type's module name. Equivalent to getting the ``type.__module__``
attribute.
.. versionadded:: 3.13
.. c:function:: void* PyType_GetSlot(PyTypeObject *type, int slot)
Return the function pointer stored in the given slot. If the

View File

@ -679,6 +679,7 @@ function,PyType_GenericNew,3.2,,
function,PyType_GetFlags,3.2,,
function,PyType_GetFullyQualifiedName,3.13,,
function,PyType_GetModule,3.10,,
function,PyType_GetModuleName,3.13,,
function,PyType_GetModuleState,3.10,,
function,PyType_GetName,3.11,,
function,PyType_GetQualName,3.11,,

View File

@ -1664,6 +1664,10 @@ New Features
to ``"builtins"``.
(Contributed by Victor Stinner in :gh:`111696`.)
* Add :c:func:`PyType_GetModuleName` function to get the type's module name.
Equivalent to getting the ``type.__module__`` attribute.
(Contributed by Eric Snow and Victor Stinner in :gh:`111696`.)
Porting to Python 3.13
----------------------

View File

@ -151,10 +151,6 @@ PyAPI_FUNC(PyObject*) _PySuper_Lookup(PyTypeObject *su_type, PyObject *su_obj,
PyObject *name, int *meth_found);
// This is exported for the _testinternalcapi module.
PyAPI_FUNC(PyObject *) _PyType_GetModuleName(PyTypeObject *);
#ifdef __cplusplus
}
#endif

View File

@ -523,7 +523,8 @@ PyAPI_FUNC(PyObject *) PyType_GetName(PyTypeObject *);
PyAPI_FUNC(PyObject *) PyType_GetQualName(PyTypeObject *);
#endif
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030D0000
PyAPI_FUNC(PyObject *) PyType_GetFullyQualifiedName(PyTypeObject *);
PyAPI_FUNC(PyObject *) PyType_GetFullyQualifiedName(PyTypeObject *type);
PyAPI_FUNC(PyObject *) PyType_GetModuleName(PyTypeObject *type);
#endif
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030C0000
PyAPI_FUNC(PyObject *) PyType_FromMetaclass(PyTypeObject*, PyObject*, PyType_Spec*, PyObject*);

View File

@ -1104,8 +1104,9 @@ class CAPITest(unittest.TestCase):
class MyType:
pass
from _testcapi import get_type_name, get_type_qualname, get_type_fullyqualname
from _testinternalcapi import get_type_module_name
from _testcapi import (
get_type_name, get_type_qualname,
get_type_fullyqualname, get_type_module_name)
from collections import OrderedDict
ht = _testcapi.get_heaptype_for_name()

View File

@ -708,6 +708,7 @@ SYMBOL_NAMES = (
"PyType_GetFlags",
"PyType_GetFullyQualifiedName",
"PyType_GetModule",
"PyType_GetModuleName",
"PyType_GetModuleState",
"PyType_GetName",
"PyType_GetQualName",

View File

@ -0,0 +1,3 @@
Add :c:func:`PyType_GetModuleName` function to get the type's module name.
Equivalent to getting the ``type.__module__`` attribute. Patch by Eric Snow
and Victor Stinner.

View File

@ -2498,3 +2498,5 @@
# "abi-only" since 3.10. (Same story as PyCFunctionFast.)
[function.PyType_GetFullyQualifiedName]
added = '3.13'
[function.PyType_GetModuleName]
added = '3.13'

View File

@ -402,7 +402,7 @@ partial_repr(partialobject *pto)
goto done;
}
mod = _PyType_GetModuleName(Py_TYPE(pto));
mod = PyType_GetModuleName(Py_TYPE(pto));
if (mod == NULL) {
goto error;
}

View File

@ -622,6 +622,14 @@ get_type_fullyqualname(PyObject *self, PyObject *type)
}
static PyObject *
get_type_module_name(PyObject *self, PyObject *type)
{
assert(PyType_Check(type));
return PyType_GetModuleName((PyTypeObject *)type);
}
static PyObject *
test_get_type_dict(PyObject *self, PyObject *Py_UNUSED(ignored))
{
@ -3268,6 +3276,7 @@ static PyMethodDef TestMethods[] = {
{"get_type_name", get_type_name, METH_O},
{"get_type_qualname", get_type_qualname, METH_O},
{"get_type_fullyqualname", get_type_fullyqualname, METH_O},
{"get_type_module_name", get_type_module_name, METH_O},
{"test_get_type_dict", test_get_type_dict, METH_NOARGS},
{"_test_thread_state", test_thread_state, METH_VARARGS},
#ifndef MS_WINDOWS

View File

@ -28,7 +28,6 @@
#include "pycore_pathconfig.h" // _PyPathConfig_ClearGlobal()
#include "pycore_pyerrors.h" // _PyErr_ChainExceptions1()
#include "pycore_pystate.h" // _PyThreadState_GET()
#include "pycore_typeobject.h" // _PyType_GetModuleName()
#include "interpreteridobject.h" // PyInterpreterID_LookUp()
@ -1631,13 +1630,6 @@ perf_trampoline_set_persist_after_fork(PyObject *self, PyObject *args)
}
static PyObject *
get_type_module_name(PyObject *self, PyObject *type)
{
assert(PyType_Check(type));
return _PyType_GetModuleName((PyTypeObject *)type);
}
static PyObject *
get_rare_event_counters(PyObject *self, PyObject *type)
{
@ -1741,7 +1733,6 @@ static PyMethodDef module_functions[] = {
{"get_crossinterp_data", get_crossinterp_data, METH_VARARGS},
{"restore_crossinterp_data", restore_crossinterp_data, METH_VARARGS},
_TESTINTERNALCAPI_TEST_LONG_NUMBITS_METHODDEF
{"get_type_module_name", get_type_module_name, METH_O},
{"get_rare_event_counters", get_rare_event_counters, METH_NOARGS},
{"reset_rare_event_counters", reset_rare_event_counters, METH_NOARGS},
#ifdef Py_GIL_DISABLED

View File

@ -1164,10 +1164,9 @@ type_set_qualname(PyTypeObject *type, PyObject *value, void *context)
}
static PyObject *
type_module(PyTypeObject *type, void *context)
type_module(PyTypeObject *type)
{
PyObject *mod;
if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
PyObject *dict = lookup_tp_dict(type);
if (PyDict_GetItemRef(dict, &_Py_ID(__module__), &mod) == 0) {
@ -1189,6 +1188,12 @@ type_module(PyTypeObject *type, void *context)
return mod;
}
static PyObject *
type_get_module(PyTypeObject *type, void *context)
{
return type_module(type);
}
static int
type_set_module(PyTypeObject *type, PyObject *value, void *context)
{
@ -1214,7 +1219,7 @@ PyType_GetFullyQualifiedName(PyTypeObject *type)
return NULL;
}
PyObject *module = type_module(type, NULL);
PyObject *module = type_module(type);
if (module == NULL) {
Py_DECREF(qualname);
return NULL;
@ -1722,7 +1727,7 @@ static PyGetSetDef type_getsets[] = {
{"__qualname__", (getter)type_qualname, (setter)type_set_qualname, NULL},
{"__bases__", (getter)type_get_bases, (setter)type_set_bases, NULL},
{"__mro__", (getter)type_get_mro, NULL, NULL},
{"__module__", (getter)type_module, (setter)type_set_module, NULL},
{"__module__", (getter)type_get_module, (setter)type_set_module, NULL},
{"__abstractmethods__", (getter)type_abstractmethods,
(setter)type_set_abstractmethods, NULL},
{"__dict__", (getter)type_dict, NULL, NULL},
@ -1743,7 +1748,7 @@ type_repr(PyObject *self)
return PyUnicode_FromFormat("<class at %p>", type);
}
PyObject *mod = type_module(type, NULL);
PyObject *mod = type_module(type);
if (mod == NULL) {
PyErr_Clear();
}
@ -4734,9 +4739,9 @@ PyType_GetQualName(PyTypeObject *type)
}
PyObject *
_PyType_GetModuleName(PyTypeObject *type)
PyType_GetModuleName(PyTypeObject *type)
{
return type_module(type, NULL);
return type_module(type);
}
void *
@ -5850,7 +5855,7 @@ object_repr(PyObject *self)
PyObject *mod, *name, *rtn;
type = Py_TYPE(self);
mod = type_module(type, NULL);
mod = type_module(type);
if (mod == NULL)
PyErr_Clear();
else if (!PyUnicode_Check(mod)) {

1
PC/python3dll.c generated
View File

@ -639,6 +639,7 @@ EXPORT_FUNC(PyType_GenericNew)
EXPORT_FUNC(PyType_GetFlags)
EXPORT_FUNC(PyType_GetFullyQualifiedName)
EXPORT_FUNC(PyType_GetModule)
EXPORT_FUNC(PyType_GetModuleName)
EXPORT_FUNC(PyType_GetModuleState)
EXPORT_FUNC(PyType_GetName)
EXPORT_FUNC(PyType_GetQualName)

View File

@ -7,7 +7,6 @@
#include "pycore_initconfig.h" // _PyStatus_OK()
#include "pycore_namespace.h" //_PyNamespace_New()
#include "pycore_pyerrors.h" // _PyErr_Clear()
#include "pycore_typeobject.h" // _PyType_GetModuleName()
#include "pycore_weakref.h" // _PyWeakref_GET_REF()
@ -510,7 +509,7 @@ _excinfo_init_type(struct _excinfo_type *info, PyObject *exc)
}
// __module__
strobj = _PyType_GetModuleName(type);
strobj = PyType_GetModuleName(type);
if (strobj == NULL) {
return -1;
}