bpo-31336: Speed up type creation, which is highly dominated by slow dict lookups. by scoder · Pull Request #3279 · python/cpython (original) (raw)
The XMLParser.__init__()
code in _elementtree.c
does this:
self->handle_start = PyObject_GetAttrString(target, "start");
self->handle_data = PyObject_GetAttrString(target, "data");
self->handle_end = PyObject_GetAttrString(target, "end");
self->handle_comment = PyObject_GetAttrString(target, "comment");
self->handle_pi = PyObject_GetAttrString(target, "pi");
self->handle_close = PyObject_GetAttrString(target, "close");
self->handle_doctype = PyObject_GetAttrString(target, "doctype");
PyErr_Clear();
In the failing test, it should find close
but not pi
. Thus, it looks up close
with a live AttributeError
set from the pi
lookup. Since _PyDict_GetItem_KnownHash()
may or may not set an exception, we have to check for a live exception after calling it, and that finds the old exception of the last attribute lookup and decides that its own lookup failed.
The correct place to fix this is obviously _elementtree.c
(please do), but if one module has this bug, it's unlikely to be the only one. What do you think? To me, it would feel wrong to allow this misbehaviour inside of CPython by explicitly handling it somehow.