Issue 22017: Bad reference counting in the _warnings module (original) (raw)
$ ./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 @@ return;
_default_action = PyString_FromString("default");
- Py_INCREF(_default_action); if (_default_action == NULL) return; if (PyModule_AddObject(m, "default_action", _default_action) < 0)