Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Lib/test/test_extcall.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,30 @@
...
TypeError: dir() got multiple values for keyword argument 'b'

AttributeError raised inside keys() or __getitem__() should not be masked
(see https://github.com/python/cpython/issues/145876)

>>> class KeysRaisesAttributeError:
... def keys(self):
... raise AttributeError("error in keys")
... def __getitem__(self, key):
... return key
>>> def f(**kwargs): pass
>>> f(**KeysRaisesAttributeError())
Traceback (most recent call last):
...
AttributeError: error in keys

>>> class GetitemRaisesAttributeError:
... def keys(self):
... return ['a', 'b']
... def __getitem__(self, key):
... raise AttributeError("error in __getitem__")
>>> f(**GetitemRaisesAttributeError())
Traceback (most recent call last):
...
AttributeError: error in __getitem__

Test a kwargs mapping with duplicated keys.

>>> from collections.abc import Mapping
Expand Down
23 changes: 23 additions & 0 deletions Lib/test/test_unpack_ex.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,29 @@
...
TypeError: 'list' object is not a mapping

AttributeError raised inside keys() or __getitem__() should not be masked
(see https://github.com/python/cpython/issues/145876)

>>> class KeysRaisesAttributeError:
... def keys(self):
... raise AttributeError("error in keys")
... def __getitem__(self, key):
... return key
>>> {**KeysRaisesAttributeError()}
Traceback (most recent call last):
...
AttributeError: error in keys

>>> class GetitemRaisesAttributeError:
... def keys(self):
... return ['a', 'b']
... def __getitem__(self, key):
... raise AttributeError("error in __getitem__")
>>> {**GetitemRaisesAttributeError()}
Traceback (most recent call last):
...
AttributeError: error in __getitem__

>>> len(eval("{" + ", ".join("**{{{}: {}}}".format(i, i)
... for i in range(1000)) + "}"))
1000
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix :exc:`AttributeError` raised inside ``.keys()`` or ``.__getitem__()``
being incorrectly masked as :exc:`TypeError` during ``{**mapping}`` and
``f(**mapping)`` unpacking. Patched by Shamil Abdulaev.
56 changes: 50 additions & 6 deletions Modules/_testinternalcapi/test_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 28 additions & 4 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2240,14 +2240,23 @@ dummy_func(
PyObject *dict_o = PyStackRef_AsPyObjectBorrow(dict);
PyObject *update_o = PyStackRef_AsPyObjectBorrow(update);

int err = PyDict_Update(dict_o, update_o);
if (err < 0) {
int matches = _PyErr_ExceptionMatches(tstate, PyExc_AttributeError);
if (matches) {
if (!PyAnyDict_Check(update_o)) {
int has_keys = PyObject_HasAttrWithError(
update_o, &_Py_ID(keys));
if (has_keys < 0) {
PyStackRef_CLOSE(update);
ERROR_IF(true);
}
if (!has_keys) {
_PyErr_Format(tstate, PyExc_TypeError,
"'%.200s' object is not a mapping",
Py_TYPE(update_o)->tp_name);
PyStackRef_CLOSE(update);
ERROR_IF(true);
}
}
int err = PyDict_Update(dict_o, update_o);
if (err < 0) {
PyStackRef_CLOSE(update);
ERROR_IF(true);
}
Expand All @@ -2259,6 +2268,21 @@ dummy_func(
PyObject *dict_o = PyStackRef_AsPyObjectBorrow(dict);
PyObject *update_o = PyStackRef_AsPyObjectBorrow(update);

if (!PyAnyDict_Check(update_o)) {
int has_keys = PyObject_HasAttrWithError(
update_o, &_Py_ID(keys));
if (has_keys < 0) {
PyStackRef_CLOSE(update);
ERROR_IF(true);
}
if (!has_keys) {
_PyErr_Format(tstate, PyExc_TypeError,
"Value after ** must be a mapping, not %.200s",
Py_TYPE(update_o)->tp_name);
PyStackRef_CLOSE(update);
ERROR_IF(true);
}
}
int err = _PyDict_MergeEx(dict_o, update_o, 2);
if (err < 0) {
_PyEval_FormatKwargsError(tstate, callable_o, update_o);
Expand Down
14 changes: 1 addition & 13 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -3403,19 +3403,7 @@ _Py_Check_ArgsIterable(PyThreadState *tstate, PyObject *func, PyObject *args)
void
_PyEval_FormatKwargsError(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
{
/* _PyDict_MergeEx raises attribute
* error (percolated from an attempt
* to get 'keys' attribute) instead of
* a type error if its second argument
* is not a mapping.
*/
if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
_PyErr_Format(
tstate, PyExc_TypeError,
"Value after ** must be a mapping, not %.200s",
Py_TYPE(kwargs)->tp_name);
}
else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
PyObject *exc = _PyErr_GetRaisedException(tstate);
PyObject *args = PyException_GetArgs(exc);
if (PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1) {
Expand Down
74 changes: 65 additions & 9 deletions Python/executor_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 50 additions & 6 deletions Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading