[New-bugs-announce] [issue13521] Make dict.setdefault() atomic

Raymond Hettinger report at bugs.python.org
Sat Dec 3 01:30:28 CET 2011

New submission from Raymond Hettinger <raymond.hettinger at gmail.com>:

A dialog on python-dev suggests that dict.setdefault() was intended to be atomic and that a number of experienced developers have deployed code relying on its atomicity:  http://mail.python.org/pipermail/python-3000/2007-July/008693.html

The actual implementation shows a second call to PyDict_Setitem() which can call PyObject_Hash() which can call arbitrary Python code.   http://hg.python.org/cpython/file/2.7/Objects/dictobject.c#l1937

This fix is straight-forward, use the results of the initial lookup to insert the default object.  This will make the operation atomic and it will make it faster.

components: Interpreter Core
messages: 148782
nosy: rhettinger
priority: normal
severity: normal
status: open
title: Make dict.setdefault() atomic
type: behavior
versions: Python 2.7, Python 3.1, Python 3.2, Python 3.3

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list