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.

classification
Title: Replace PyDict_GetItem() with PyDict_GetItemWithError()
Type: Stage: resolved
Components: Versions: Python 3.3, Python 3.4, Python 3.5
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: rhettinger, serhiy.storchaka, vstinner
Priority: normal Keywords:

Created on 2014-02-13 10:44 by vstinner, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (3)
msg211142 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2014-02-13 10:44
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.
msg211167 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2014-02-13 20:05
Please, no more big red warnings.

Keep the docs factual and straight-forward.  In the docs for PyDict_GetItem(), note that errors will get suppressed.  To
get error reporting use PyDict_GetItemWithError() instead.
msg297122 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-06-28 01:33
Sorry, I lost track of this issue, so I just close it.
History
Date User Action Args
2022-04-11 14:57:58adminsetgithub: 64814
2017-06-28 01:33:58vstinnersetstatus: open -> closed
resolution: out of date
messages: + msg297122

stage: needs patch -> resolved
2014-02-13 20:05:04rhettingersetnosy: + rhettinger
messages: + msg211167
2014-02-13 19:14:31serhiy.storchakasetstage: needs patch
2014-02-13 10:44:13vstinnercreate