.. 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
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,,
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
----------------------
PyObject *name, int *meth_found);
-// This is exported for the _testinternalcapi module.
-PyAPI_FUNC(PyObject *) _PyType_GetModuleName(PyTypeObject *);
-
-
#ifdef __cplusplus
}
#endif
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*);
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()
"PyType_GetFlags",
"PyType_GetFullyQualifiedName",
"PyType_GetModule",
+ "PyType_GetModuleName",
"PyType_GetModuleState",
"PyType_GetName",
"PyType_GetQualName",
--- /dev/null
+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.
# "abi-only" since 3.10. (Same story as PyCFunctionFast.)
[function.PyType_GetFullyQualifiedName]
added = '3.13'
+[function.PyType_GetModuleName]
+ added = '3.13'
goto done;
}
- mod = _PyType_GetModuleName(Py_TYPE(pto));
+ mod = PyType_GetModuleName(Py_TYPE(pto));
if (mod == NULL) {
goto error;
}
}
+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))
{
{"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
#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()
}
-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)
{
{"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
}
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) {
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)
{
return NULL;
}
- PyObject *module = type_module(type, NULL);
+ PyObject *module = type_module(type);
if (module == NULL) {
Py_DECREF(qualname);
return NULL;
{"__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},
return PyUnicode_FromFormat("<class at %p>", type);
}
- PyObject *mod = type_module(type, NULL);
+ PyObject *mod = type_module(type);
if (mod == NULL) {
PyErr_Clear();
}
}
PyObject *
-_PyType_GetModuleName(PyTypeObject *type)
+PyType_GetModuleName(PyTypeObject *type)
{
- return type_module(type, NULL);
+ return type_module(type);
}
void *
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)) {
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)
#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()
}
// __module__
- strobj = _PyType_GetModuleName(type);
+ strobj = PyType_GetModuleName(type);
if (strobj == NULL) {
return -1;
}