[New-bugs-announce] [issue20615] Replace PyDict_GetItem() with PyDict_GetItemWithError()

STINNER Victor report at bugs.python.org
Thu Feb 13 11:44:13 CET 2014


New submission from STINNER Victor:

See this huge warning in the definition of PyDict_GetItem:

/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
 * that may occur (originally dicts supported only string keys, and exceptions
 * weren't possible).  So, while the original intent was that a NULL return
 * meant the key wasn't present, in reality it can mean that, or that an error
 * (suppressed) occurred while computing the key's hash, or that some error
 * (suppressed) occurred when comparing keys in the dict's internal probe
 * sequence.  A nasty example of the latter is when a Python-coded comparison
 * function hits a stack-depth error, which can cause this to return NULL
 * even if the key is present.
 */
PyObject *
PyDict_GetItem(PyObject *op, PyObject *key)
{ ... }

PyDict_GetItem() should avoided because it may hide important exceptions like KeyboardInterrupt or MemoryError.

See for example #14537 for a specific error.

By the way, the PyDict_GetItem() documentation should contain a big red warning and suggest to use PyDict_GetItemWithError() instead.

----------
messages: 211142
nosy: haypo, serhiy.storchaka
priority: normal
severity: normal
status: open
title: Replace PyDict_GetItem() with PyDict_GetItemWithError()
versions: Python 3.3, Python 3.4, Python 3.5

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue20615>
_______________________________________


More information about the New-bugs-announce mailing list