mirror of
https://github.com/python/cpython.git
synced 2025-08-25 19:24:42 +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
|
@ -3425,9 +3425,12 @@ dummy_func(
|
|||
PyObject *attr_o = _PyObject_LookupSpecialMethod(owner_o, name, &self_or_null_o);
|
||||
if (attr_o == NULL) {
|
||||
if (!_PyErr_Occurred(tstate)) {
|
||||
_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;
|
||||
assert(!_PyErr_Occurred(tstate));
|
||||
assert(errfmt != NULL);
|
||||
_PyErr_Format(tstate, PyExc_TypeError, errfmt, owner_o);
|
||||
}
|
||||
ERROR_IF(true, error);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue