Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Doc/c-api/type.rst
Original file line numberDiff line numberDiff line change
Expand Up@@ -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
Expand Down
1 change: 1 addition & 0 deletions Doc/data/stable_abi.dat

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Doc/whatsnew/3.13.rst
Original file line numberDiff line numberDiff line change
Expand Up@@ -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
----------------------
Expand Down
4 changes: 0 additions & 4 deletions Include/internal/pycore_typeobject.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -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
Expand Down
3 changes: 2 additions & 1 deletion Include/object.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -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*);
Expand Down
5 changes: 3 additions & 2 deletions Lib/test/test_capi/test_misc.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -1104,8 +1104,9 @@ def test_get_type_name(self):
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()
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_stable_abi_ctypes.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line numberDiff line numberDiff line change
@@ -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.
2 changes: 2 additions & 0 deletions Misc/stable_abi.toml
Original file line numberDiff line numberDiff line change
Expand Up@@ -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'
2 changes: 1 addition & 1 deletion Modules/_functoolsmodule.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -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;
}
Expand Down
9 changes: 9 additions & 0 deletions Modules/_testcapimodule.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -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))
{
Expand DownExpand Up@@ -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
Expand Down
9 changes: 0 additions & 9 deletions Modules/_testinternalcapi.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -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()

Expand DownExpand Up@@ -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)
{
Expand DownExpand Up@@ -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
Expand Down
21 changes: 13 additions & 8 deletions Objects/typeobject.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -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) {
Expand All@@ -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)
{
Expand All@@ -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;
Expand DownExpand Up@@ -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},
Expand All@@ -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();
}
Expand DownExpand Up@@ -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 *
Expand DownExpand Up@@ -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)) {
Expand Down
1 change: 1 addition & 0 deletions PC/python3dll.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Python/crossinterp.c
Original file line numberDiff line numberDiff line change
Expand Up@@ -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()


Expand DownExpand Up@@ -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;
}
Expand Down