bpo-38823: Add a private _PyModule_StealObject API. by brandtbucher · Pull Request #17298 · python/cpython · GitHub
Skip to content
Closed
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
1 change: 1 addition & 0 deletions Include/modsupport.h
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.
15 changes: 12 additions & 3 deletions Modules/_bz2module.c
Original file line number Diff line number Diff line change
Expand Up @@ -755,11 +755,20 @@ PyInit__bz2(void)
return NULL;

Py_INCREF(&BZ2Compressor_Type);
PyModule_AddObject(m, "BZ2Compressor", (PyObject *)&BZ2Compressor_Type);
if (_PyModule_StealObject(m, "BZ2Compressor",
(PyObject *)&BZ2Compressor_Type) < 0) {
goto fail;
}

Py_INCREF(&BZ2Decompressor_Type);
PyModule_AddObject(m, "BZ2Decompressor",
(PyObject *)&BZ2Decompressor_Type);
if (_PyModule_StealObject(m, "BZ2Decompressor",
(PyObject *)&BZ2Decompressor_Type) < 0) {
goto fail;
}

return m;

fail:
Py_DECREF(m);
return NULL;
}
53 changes: 37 additions & 16 deletions Modules/_collectionsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2539,34 +2539,55 @@ PyInit__collections(void)
if (m == NULL)
return NULL;

if (PyType_Ready(&deque_type) < 0)
return NULL;
if (PyType_Ready(&deque_type) < 0) {
goto fail;
}
Py_INCREF(&deque_type);
PyModule_AddObject(m, "deque", (PyObject *)&deque_type);
if (_PyModule_StealObject(m, "deque", (PyObject *)&deque_type) < 0) {
goto fail;
}

defdict_type.tp_base = &PyDict_Type;
if (PyType_Ready(&defdict_type) < 0)
return NULL;
if (PyType_Ready(&defdict_type) < 0) {
goto fail;
}
Py_INCREF(&defdict_type);
PyModule_AddObject(m, "defaultdict", (PyObject *)&defdict_type);
if (_PyModule_StealObject(m, "defaultdict", (PyObject *)&defdict_type) < 0) {
goto fail;
}

Py_INCREF(&PyODict_Type);
PyModule_AddObject(m, "OrderedDict", (PyObject *)&PyODict_Type);
if (_PyModule_StealObject(m, "OrderedDict", (PyObject *)&PyODict_Type) < 0) {
goto fail;
}

if (PyType_Ready(&dequeiter_type) < 0)
return NULL;
if (PyType_Ready(&dequeiter_type) < 0) {
goto fail;
}
Py_INCREF(&dequeiter_type);
PyModule_AddObject(m, "_deque_iterator", (PyObject *)&dequeiter_type);
if (_PyModule_StealObject(m, "_deque_iterator", (PyObject *)&dequeiter_type) < 0) {
goto fail;
}

if (PyType_Ready(&dequereviter_type) < 0)
return NULL;
if (PyType_Ready(&dequereviter_type) < 0) {
goto fail;
}
Py_INCREF(&dequereviter_type);
PyModule_AddObject(m, "_deque_reverse_iterator", (PyObject *)&dequereviter_type);
if (_PyModule_StealObject(m, "_deque_reverse_iterator", (PyObject *)&dequereviter_type) < 0) {
goto fail;
}

if (PyType_Ready(&tuplegetter_type) < 0)
return NULL;
if (PyType_Ready(&tuplegetter_type) < 0) {
goto fail;
}
Py_INCREF(&tuplegetter_type);
PyModule_AddObject(m, "_tuplegetter", (PyObject *)&tuplegetter_type);
if (_PyModule_StealObject(m, "_tuplegetter", (PyObject *)&tuplegetter_type) < 0) {
goto fail;
}

return m;

fail:
Py_DECREF(m);
return NULL;
}
5 changes: 4 additions & 1 deletion Modules/_functoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1457,7 +1457,10 @@ PyInit__functools(void)
}
name = _PyType_Name(typelist[i]);
Py_INCREF(typelist[i]);
PyModule_AddObject(m, name, (PyObject *)typelist[i]);
if (_PyModule_StealObject(m, name, (PyObject *)typelist[i]) < 0) {
Py_DECREF(m);
return NULL;
}
}
return m;
}
5 changes: 4 additions & 1 deletion PC/_msi.c
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,9 @@ PyInit__msi(void)
MSIError = PyErr_NewException ("_msi.MSIError", NULL, NULL);
if (!MSIError)
return NULL;
PyModule_AddObject(m, "MSIError", MSIError);
if (_PyModule_StealObject(m, "MSIError", MSIError) < 0) {
Py_DECREF(m);
return NULL;
}
return m;
}
6 changes: 3 additions & 3 deletions Python/_warnings.c
Original file line number Diff line number Diff line change
Expand Up @@ -1353,17 +1353,17 @@ _PyWarnings_Init(void)
}

Py_INCREF(st->filters);
if (PyModule_AddObject(m, "filters", st->filters) < 0) {
if (_PyModule_StealObject(m, "filters", st->filters) < 0) {
goto error;
}

Py_INCREF(st->once_registry);
if (PyModule_AddObject(m, "_onceregistry", st->once_registry) < 0) {
if (_PyModule_StealObject(m, "_onceregistry", st->once_registry) < 0) {
goto error;
}

Py_INCREF(st->default_action);
if (PyModule_AddObject(m, "_defaultaction", st->default_action) < 0) {
if (_PyModule_StealObject(m, "_defaultaction", st->default_action) < 0) {
goto error;
}

Expand Down
13 changes: 13 additions & 0 deletions Python/modsupport.c