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 <rep...@bugs.python.org>
<http://bugs.python.org/issue26871>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to