Skip to content

bpo-38823: Add a private _PyModule_StealObject API. #17298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
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