bpo-1635741: Add PyModule_AddObjectRef() function by vstinner · Pull Request #23122 · python/cpython · GitHub
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
104 changes: 90 additions & 14 deletions Doc/c-api/module.rst
5 changes: 5 additions & 0 deletions Doc/whatsnew/3.10.rst
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,11 @@ New Features
* Added :c:func:`PyUnicode_AsUTF8AndSize` to the limited C API.
(Contributed by Alex Gaynor in :issue:`41784`.)

* Added :c:func:`PyModule_AddObjectRef` function: similar to
:c:func:`PyModule_AddObjectRef` but don't steal a reference to the value on
success.
(Contributed by Victor Stinner in :issue:`1635741`.)


Porting to Python 3.10
----------------------
Expand Down
10 changes: 9 additions & 1 deletion Include/modsupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,15 @@ PyAPI_FUNC(PyObject * const *) _PyArg_UnpackKeywords(
void _PyArg_Fini(void);
#endif /* Py_LIMITED_API */

PyAPI_FUNC(int) PyModule_AddObject(PyObject *, const char *, PyObject *);
// Add an attribute with name 'name' and value 'obj' to the module 'mod.
// On success, return 0 on success.
// On error, raise an exception and return -1.
PyAPI_FUNC(int) PyModule_AddObjectRef(PyObject *mod, const char *name, PyObject *value);

// Similar to PyModule_AddObjectRef() but steal a reference to 'obj'
// (Py_DECREF(obj)) on success (if it returns 0).
PyAPI_FUNC(int) PyModule_AddObject(PyObject *mod, const char *, PyObject *value);

PyAPI_FUNC(int) PyModule_AddIntConstant(PyObject *, const char *, long);
PyAPI_FUNC(int) PyModule_AddStringConstant(PyObject *, const char *, const char *);
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03090000
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Added :c:func:`PyModule_AddObjectRef` function: similar to
:c:func:`PyModule_AddObjectRef` but don't steal a reference to the value on
success. Patch by Victor Stinner.
70 changes: 39 additions & 31 deletions Python/modsupport.c