mirror of
https://github.com/python/cpython.git
synced 2025-07-07 19:35:27 +00:00
gh-128398: improve error messages when incorrectly using with
and async with
(#132218)
Improve the error message with a suggestion when an object supporting the synchronous (resp. asynchronous) context manager protocol is entered using `async with` (resp. `with`) instead of `with` (resp. `async with`).
This commit is contained in:
parent
95800fe6e7
commit
8a9c6c4d16
10 changed files with 211 additions and 47 deletions
11
Python/generated_cases.c.h
generated
11
Python/generated_cases.c.h
generated
|
@ -9358,9 +9358,14 @@
|
|||
if (attr_o == NULL) {
|
||||
if (!_PyErr_Occurred(tstate)) {
|
||||
_PyFrame_SetStackPointer(frame, stack_pointer);
|
||||
_PyErr_Format(tstate, PyExc_TypeError,
|
||||
_Py_SpecialMethods[oparg].error,
|
||||
Py_TYPE(owner_o)->tp_name);
|
||||
const char *errfmt = _PyEval_SpecialMethodCanSuggest(owner_o, oparg)
|
||||
? _Py_SpecialMethods[oparg].error_suggestion
|
||||
: _Py_SpecialMethods[oparg].error;
|
||||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
assert(!_PyErr_Occurred(tstate));
|
||||
assert(errfmt != NULL);
|
||||
_PyFrame_SetStackPointer(frame, stack_pointer);
|
||||
_PyErr_Format(tstate, PyExc_TypeError, errfmt, owner_o);
|
||||
stack_pointer = _PyFrame_GetStackPointer(frame);
|
||||
}
|
||||
JUMP_TO_LABEL(error);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue