[New-bugs-announce] [issue26871] Change weird behavior of PyModule_AddObject()

Serhiy Storchaka report at bugs.python.org
Wed Apr 27 13:52:51 EDT 2016


New submission from Serhiy Storchaka:

PyModule_AddObject() has weird and counterintuitive behavior. It steals a reference only on success. The caller is responsible to decref it on error. This behavior was not documented and inconsistent with the behavior of other functions stealing a reference (PyList_SetItem() and PyTuple_SetItem()). Seems most developers don't use this function correctly, since only in few places in the stdlib a reference is decrefed explicitly after PyModule_AddObject() failure.

This weird behavior was first reported in issue1782, and changing it was proposed. Related bugs in PyModule_AddIntConstant() and PyModule_AddStringConstant() were fixed, but the behavior of PyModule_AddObject() was not changed and not documented.

This issue is opened for gradual changing the behavior of PyModule_AddObject(). Proposed patch introduces new macros PY_MODULE_ADDOBJECT_CLEAN that controls the behavior of PyModule_AddObject() as PY_SSIZE_T_CLEAN controls the behavior of PyArg_Parse* functions. If the macro is defined before including "Python.h", PyModule_AddObject() steals a reference unconditionally.  Otherwise it steals a reference only on success, and the caller is responsible for decref'ing it on error (current behavior). This needs minimal changes to source code if PyModule_AddObject() was used incorrectly (i.e. as documented), and keep the code that explicitly decref a reference after PyModule_AddObject() working correctly.

Use of PyModule_AddObject() without defining PY_MODULE_ADDOBJECT_CLEAN is declared deprecated (or we can defer this to 3.7). In the distant future (after dropping the support of 2.7) the old behavior will be dropped.

See also a discussion on Python-Dev: http://comments.gmane.org/gmane.comp.python.devel/157545 .

----------
components: Extension Modules, Interpreter Core
files: pymodule_addobject.patch
keywords: patch
messages: 264384
nosy: serhiy.storchaka
priority: normal
severity: normal
stage: patch review
status: open
title: Change weird behavior of PyModule_AddObject()
type: enhancement
versions: Python 3.6
Added file: http://bugs.python.org/file42632/pymodule_addobject.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue26871>
_______________________________________


More information about the New-bugs-announce mailing list