This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Bad reference counting in the _warnings module
Type: crash Stage: resolved
Components: Interpreter Core Versions: Python 2.7
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: brett.cannon, python-dev, xdegaye
Priority: normal Keywords:

Created on 2014-07-20 16:24 by xdegaye, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg223519 - (view) Author: Xavier de Gaye (xdegaye) * (Python triager) Date: 2014-07-20 16:24
$ ./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)
msg223520 - (view) Author: Xavier de Gaye (xdegaye) * (Python triager) Date: 2014-07-20 16:28
> The same kind of fix should also be applied to '_once_registry' and '_filters'.

Oops, no they are correctly refcounted.
msg223535 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-07-20 20:04
New changeset 8106f91fccd6 by Benjamin Peterson in branch '2.7':
correct ref counting of default_action (closes #22017)
Date User Action Args
2022-04-11 14:58:06adminsetgithub: 66216
2014-07-20 20:04:49python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg223535

resolution: fixed
stage: resolved
2014-07-20 16:56:59pitrousetnosy: + brett.cannon
2014-07-20 16:28:46xdegayesetmessages: + msg223520
2014-07-20 16:24:34xdegayecreate