[New-bugs-announce] [issue22017] Bad reference counting in the _warnings module

Xavier de Gaye report at bugs.python.org
Sun Jul 20 18:24:34 CEST 2014

New submission from Xavier de Gaye:

$ ./python
Python 2.7.8+ (2.7:5563f895b215, Jul 20 2014, 18:10:28) 
[GCC 4.9.0 20140521 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import warnings
>>> warnings.defaultaction = 'default'
>>> warnings.warn('some warning')
__main__:1: UserWarning: some warning
>>> exit()
Fatal Python error: Objects/dictobject.c:519 object at 0x7fce2013e2e0 has negative ref count -2604246222170760230
Aborted (core dumped)

The following patch fixes this: the new string object is referenced both by the static variable '_default_action' and the module attribute 'default_action'.
Python 3.5 handles this correctly.
The same kind of fix should also be applied to '_once_registry' and '_filters'.

diff --git a/Python/_warnings.c b/Python/_warnings.c
--- a/Python/_warnings.c
+++ b/Python/_warnings.c
@@ -903,6 +903,7 @@
     _default_action = PyString_FromString("default");
+    Py_INCREF(_default_action);
     if (_default_action == NULL)
     if (PyModule_AddObject(m, "default_action", _default_action) < 0)

components: Interpreter Core
messages: 223519
nosy: xdegaye
priority: normal
severity: normal
status: open
title: Bad reference counting in the _warnings module
type: crash
versions: Python 2.7

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list