[Python-Dev] Inconsistency of PyModule_AddObject()

Serhiy Storchaka storchaka at gmail.com
Thu Apr 28 09:55:32 EDT 2016

On 28.04.16 11:38, Stefan Krah wrote:
> Serhiy Storchaka <storchaka <at> gmail.com> writes:
>> No impact except emitting a deprecation warning at build time. But we
>> can remove a deprecation warning and add it in future release if this is
>> annoying.
>> But are you sure, that your code uses PyModule_AddObject() correctly?
>> Only two modules in the stdlib (_json and _tkinter) used it correctly.
>> Other modules have bugs even in tries to use PyModule_AddObject()
>> correctly for some operations.
> Could you perhaps stop labeling this as a bug? Usually we are talking
> about a *single* "leak" that a) does not even show up in Valgrind and
> b) only occurs under severe memory pressure when the OOM-killer is
> already waiting.
> I'm honestly mystified by your terminology and it's beginning to feel
> that you need to justify this patch at all costs.

I say this is a bug because

1. PyModule_AddObject() behavior doesn't match the documentation.

2. Most code that use PyModule_AddObject() doesn't work as intended. 
Since the bahavior of PyModule_AddObject() contradicts the documentation 
and is contrintuitive, we can't blame authors in this.

I don't say this is a high-impacting bug, I even agree that there is no 
need to fix the second part in maintained releases. But this is a bug 
unless you propose different definition for a bug.

What can we do with this?

1. Change the documentation of PyModule_AddObject(). I think this is not 
questionable, and Berker provided a patch in
http://bugs.python.org/issue26868 .

2. Update examples in the documentation to correctly handle errors of 
PyModule_AddObject(). This is more questionable, due to the case (3c) 
below and because correct error handling code distracts attention from 
main purpose of examples.

3. One of alternatives:

3a) Fix almost all usages of PyModule_AddObject() in stdlib extension 
modules. This is hundreds occurrences in over a half-hundred files.

3b) Allow to change the behavior of PyModule_AddObject() to match most 
authors expectations. This needs to add only one line to switch on new 
behavior in most files.

3c) Ignore issue. In this case we can not check the result of 
PyModule_AddObject() at all. But I afraid that correct fixing issues 
with subinterpreters will need us to return to this issue.

More information about the Python-Dev mailing list