FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-145876: preserve AttributeError in dict unpacking by zakiscoding · Pull Request #145878 · python/cpython · GitHub

/ cpython Public
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .h  (1) .py  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
23 changes: 23 additions & 0 deletions Lib/test/test_unpack_ex.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,29 @@
...
TypeError: 'list' object is not a mapping

>>> class MappingWithKeyErrors:
... def __init__(self, *, keys_error=None, getitem_error=None):
... self.keys_error = keys_error
... self.getitem_error = getitem_error
... def keys(self):
... if self.keys_error is not None:
... raise self.keys_error("error in keys")
... return [1, 2, 3]
... def __getitem__(self, key):
... if self.getitem_error is not None:
... raise self.getitem_error("error in __getitem__")
... return key * 2

>>> {**MappingWithKeyErrors(keys_error=AttributeError)}
Traceback (most recent call last):
...
AttributeError: error in keys

>>> {**MappingWithKeyErrors(getitem_error=AttributeError)}
Traceback (most recent call last):
...
AttributeError: error in __getitem__

>>> len(eval("{" + ", ".join("**{{{}: {}}}".format(i, i)
... for i in range(1000)) + "}"))
1000
Expand Down
21 changes: 18 additions & 3 deletions Python/bytecodes.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2244,9 +2244,24 @@ dummy_func(
if (err < 0) {
int matches = _PyErr_ExceptionMatches(tstate, PyExc_AttributeError);
if (matches) {
_PyErr_Format(tstate, PyExc_TypeError,
"'%.200s' object is not a mapping",
Py_TYPE(update_o)->tp_name);
PyObject *exc = PyErr_GetRaisedException();
PyObject *keys = NULL;
int has_keys = PyObject_GetOptionalAttr(update_o, &_Py_ID(keys), &keys);

if (has_keys < 0) {
Comment on lines +2247 to +2251

Copilot AI Mar 12, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

DICT_UPDATE now preserves the original AttributeError only when update_o has a keys attribute, but the tier-2 interpreter (Python/executor_cases.c.h) is generated from bytecodes.c and currently still contains the old unconditional conversion of AttributeError into TypeError. This will make behavior differ between tier-1 and tier-2 execution (and can break the new tests when tier-2 is enabled). Please regenerate/update the tier-2 cases so DICT_UPDATE matches this new logic everywhere.

Copilot uses AI. Check for mistakes.
Py_XDECREF(keys);
Py_DECREF(exc);
}
else if (has_keys == 0) {
Py_DECREF(exc);
_PyErr_Format(tstate, PyExc_TypeError,
"'%.200s' object is not a mapping",
Py_TYPE(update_o)->tp_name);
}
else {
Py_DECREF(keys);
PyErr_SetRaisedException(exc);
}
}
PyStackRef_CLOSE(update);
ERROR_IF(true);
Expand Down
28 changes: 25 additions & 3 deletions Python/generated_cases.c.h

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

Loading

Back | FazBrowse Home | New Git URL