Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions Include/modsupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ void _PyArg_Fini(void);
#endif /* Py_LIMITED_API */

PyAPI_FUNC(int) PyModule_AddObject(PyObject *, const char *, PyObject *);
PyAPI_FUNC(int) _PyModule_StealObject(PyObject *, const char *, PyObject *);
PyAPI_FUNC(int) PyModule_AddIntConstant(PyObject *, const char *, long);
PyAPI_FUNC(int) PyModule_AddStringConstant(PyObject *, const char *, const char *);
#define PyModule_AddIntMacro(m, c) PyModule_AddIntConstant(m, #c, c)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Add a new private ``_PyModule_StealObject`` API. It is identical to :c:func:`PyModule_AddObject`, but steals a reference to the added object on both success *and* failure.

Patch by Brandt Bucher.
13 changes: 13 additions & 0 deletions Python/modsupport.c
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,19 @@ PyModule_AddObject(PyObject *m, const char *name, PyObject *o)
return 0;
}

/* Like PyModule_AddObject, but steals o on success AND failure.
This is probably what you want! */

int
_PyModule_StealObject(PyObject *m, const char *name, PyObject *o)
{
if (PyModule_AddObject(m, name, o) < 0) {
Py_XDECREF(o);
return -1;
}
return 0;
}

int
PyModule_AddIntConstant(PyObject *m, const char *name, long value)
{
Expand Down