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. |