Skip to content

Commit

Permalink
Add PyImport_AddModuleRef() function
Browse files Browse the repository at this point in the history
  • Loading branch information
vstinner committed Jun 20, 2023
1 parent c10dbfa commit 8ba8db3
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
8 changes: 8 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ Latest version of the header file:
`pythoncapi_compat.h <https://raw.githubusercontent.com/python/pythoncapi-compat/master/pythoncapi_compat.h>`_.


Python 3.13
-----------

.. c:function:: PyObject* PyImport_AddModuleRef(const char *name)
See `PyImport_AddModuleRef() documentation <https://docs.python.org/dev/c-api/import.html#c.PyImport_AddModuleRef>`__.
Python 3.12
-----------
Expand Down
5 changes: 3 additions & 2 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
Changelog
=========

* 2022-11-15: Add experimental operations to the upgrade_pythoncapi script:
``Py_NewRef``, ``Py_CLEAR`` and ``Py_SETREF``.
* 2023-06-20: Add ``PyImport_AddModuleRef()`` function.
* 2022-11-15: Add experimental operations to the ``upgrade_pythoncapi.py``
script: ``Py_NewRef``, ``Py_CLEAR`` and ``Py_SETREF``.
* 2022-11-09: Fix ``Py_SETREF()`` and ``Py_XSETREF()`` macros
for `gh-98724 <https://github.com/python/cpython/issues/98724>`_.
* 2022-11-04: Add ``PyFrame_GetVar()`` and ``PyFrame_GetVarString()``
Expand Down
9 changes: 9 additions & 0 deletions pythoncapi_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,15 @@ PyCode_GetCellvars(PyCodeObject *code)
# endif
#endif

// gh-105922 added PyImport_AddModuleRef() to Python 3.13.0a1
#if PY_VERSION_HEX < 0x030D00A0
PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
PyImport_AddModuleRef(const char *name)
{
return Py_XNewRef(PyImport_AddModule(name));
}
#endif


#ifdef __cplusplus
}
Expand Down
25 changes: 25 additions & 0 deletions tests/test_pythoncapi_compat_cext.c
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,30 @@ test_api_casts(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
}


static PyObject *
test_import(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
{
PyObject *mod = PyImport_ImportModule("sys");
if (mod == NULL) {
return NULL;
}
Py_ssize_t refcnt = Py_REFCNT(mod);

// test PyImport_AddModuleRef()
PyObject *mod2 = PyImport_AddModuleRef("sys");
if (mod2 == NULL) {
return NULL;
}
assert(PyModule_Check(mod2));
assert(Py_REFCNT(mod) == (refcnt + 1));

Py_DECREF(mod2);
Py_DECREF(mod);

Py_RETURN_NONE;
}


static struct PyMethodDef methods[] = {
{"test_object", test_object, METH_NOARGS, _Py_NULL},
{"test_py_is", test_py_is, METH_NOARGS, _Py_NULL},
Expand All @@ -662,6 +686,7 @@ static struct PyMethodDef methods[] = {
{"test_code", test_code, METH_NOARGS, _Py_NULL},
#endif
{"test_api_casts", test_api_casts, METH_NOARGS, _Py_NULL},
{"test_import", test_import, METH_NOARGS, _Py_NULL},
{_Py_NULL, _Py_NULL, 0, _Py_NULL}
};

Expand Down

0 comments on commit 8ba8db3

Please sign in to comment.