Skip to content

Commit 8ba8db3

Browse files
committed
Add PyImport_AddModuleRef() function
1 parent c10dbfa commit 8ba8db3

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

docs/api.rst

+8
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ Latest version of the header file:
2424
`pythoncapi_compat.h <https://github.com/raw/python/pythoncapi-compat/master/pythoncapi_compat.h>`_.
2525

2626

27+
Python 3.13
28+
-----------
29+
30+
.. c:function:: PyObject* PyImport_AddModuleRef(const char *name)
31+
32+
See `PyImport_AddModuleRef() documentation <https://docs.python.org/dev/c-api/import.html#c.PyImport_AddModuleRef>`__.
33+
34+
2735
Python 3.12
2836
-----------
2937

docs/changelog.rst

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
Changelog
22
=========
33

4-
* 2022-11-15: Add experimental operations to the upgrade_pythoncapi script:
5-
``Py_NewRef``, ``Py_CLEAR`` and ``Py_SETREF``.
4+
* 2023-06-20: Add ``PyImport_AddModuleRef()`` function.
5+
* 2022-11-15: Add experimental operations to the ``upgrade_pythoncapi.py``
6+
script: ``Py_NewRef``, ``Py_CLEAR`` and ``Py_SETREF``.
67
* 2022-11-09: Fix ``Py_SETREF()`` and ``Py_XSETREF()`` macros
78
for `gh-98724 <https://github.com/python/cpython/issues/98724>`_.
89
* 2022-11-04: Add ``PyFrame_GetVar()`` and ``PyFrame_GetVarString()``

pythoncapi_compat.h

+9
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,15 @@ PyCode_GetCellvars(PyCodeObject *code)
570570
# endif
571571
#endif
572572

573+
// gh-105922 added PyImport_AddModuleRef() to Python 3.13.0a1
574+
#if PY_VERSION_HEX < 0x030D00A0
575+
PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
576+
PyImport_AddModuleRef(const char *name)
577+
{
578+
return Py_XNewRef(PyImport_AddModule(name));
579+
}
580+
#endif
581+
573582

574583
#ifdef __cplusplus
575584
}

tests/test_pythoncapi_compat_cext.c

+25
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,30 @@ test_api_casts(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
644644
}
645645

646646

647+
static PyObject *
648+
test_import(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
649+
{
650+
PyObject *mod = PyImport_ImportModule("sys");
651+
if (mod == NULL) {
652+
return NULL;
653+
}
654+
Py_ssize_t refcnt = Py_REFCNT(mod);
655+
656+
// test PyImport_AddModuleRef()
657+
PyObject *mod2 = PyImport_AddModuleRef("sys");
658+
if (mod2 == NULL) {
659+
return NULL;
660+
}
661+
assert(PyModule_Check(mod2));
662+
assert(Py_REFCNT(mod) == (refcnt + 1));
663+
664+
Py_DECREF(mod2);
665+
Py_DECREF(mod);
666+
667+
Py_RETURN_NONE;
668+
}
669+
670+
647671
static struct PyMethodDef methods[] = {
648672
{"test_object", test_object, METH_NOARGS, _Py_NULL},
649673
{"test_py_is", test_py_is, METH_NOARGS, _Py_NULL},
@@ -662,6 +686,7 @@ static struct PyMethodDef methods[] = {
662686
{"test_code", test_code, METH_NOARGS, _Py_NULL},
663687
#endif
664688
{"test_api_casts", test_api_casts, METH_NOARGS, _Py_NULL},
689+
{"test_import", test_import, METH_NOARGS, _Py_NULL},
665690
{_Py_NULL, _Py_NULL, 0, _Py_NULL}
666691
};
667692

0 commit comments

Comments
 (0)