bpo-1635741: Add PyModule_AddObjectRef() function (GH-23122) · python/cpython@8021875 · GitHub
Skip to content

Commit 8021875

Browse files
authored
bpo-1635741: Add PyModule_AddObjectRef() function (GH-23122)
Added PyModule_AddObjectRef() function: similar to PyModule_AddObjectRef() but don't steal a reference to the value on success.
1 parent 3529718 commit 8021875

5 files changed

Lines changed: 146 additions & 46 deletions

File tree

Doc/c-api/module.rst

Lines changed: 90 additions & 14 deletions

Doc/whatsnew/3.10.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,11 @@ New Features
374374
* Added :c:func:`PyUnicode_AsUTF8AndSize` to the limited C API.
375375
(Contributed by Alex Gaynor in :issue:`41784`.)
376376

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

378383
Porting to Python 3.10
379384
----------------------

Include/modsupport.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,15 @@ PyAPI_FUNC(PyObject * const *) _PyArg_UnpackKeywords(
136136
void _PyArg_Fini(void);
137137
#endif /* Py_LIMITED_API */
138138

139-
PyAPI_FUNC(int) PyModule_AddObject(PyObject *, const char *, PyObject *);
139+
// Add an attribute with name 'name' and value 'obj' to the module 'mod.
140+
// On success, return 0 on success.
141+
// On error, raise an exception and return -1.
142+
PyAPI_FUNC(int) PyModule_AddObjectRef(PyObject *mod, const char *name, PyObject *value);
143+
144+
// Similar to PyModule_AddObjectRef() but steal a reference to 'obj'
145+
// (Py_DECREF(obj)) on success (if it returns 0).
146+
PyAPI_FUNC(int) PyModule_AddObject(PyObject *mod, const char *, PyObject *value);
147+
140148
PyAPI_FUNC(int) PyModule_AddIntConstant(PyObject *, const char *, long);
141149
PyAPI_FUNC(int) PyModule_AddStringConstant(PyObject *, const char *, const char *);
142150
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03090000
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Added :c:func:`PyModule_AddObjectRef` function: similar to
2+
:c:func:`PyModule_AddObjectRef` but don't steal a reference to the value on
3+
success. Patch by Victor Stinner.

Python/modsupport.c

Lines changed: 39 additions & 31 deletions

0 commit comments

Comments
 (0)