summaryrefslogtreecommitdiffstats
path: root/Python/bytecodes.c
diff options
context:
space:
mode:
authorBénédikt Tran <10796600+picnixz@users.noreply.github.com>2025-04-19 08:44:01 (GMT)
committerGitHub <noreply@github.com>2025-04-19 08:44:01 (GMT)
commit8a9c6c4d16a746eea1e000d6701d1c274c1f331b (patch)
treec7898c8103a3f99297f07151cc3dbf5e5572dfb2 /Python/bytecodes.c
parent95800fe6e719c829acf52fbb00198135b78719b4 (diff)
downloadcpython-8a9c6c4d16a746eea1e000d6701d1c274c1f331b.zip
cpython-8a9c6c4d16a746eea1e000d6701d1c274c1f331b.tar.gz
cpython-8a9c6c4d16a746eea1e000d6701d1c274c1f331b.tar.bz2
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`).
Diffstat (limited to 'Python/bytecodes.c')
-rw-r--r--Python/bytecodes.c9
1 files changed, 6 insertions, 3 deletions
diff --git a/Python/bytecodes.c b/Python/bytecodes.c
index 2796c3f..07df22c 100644
--- a/Python/bytecodes.c
+++ b/Python/bytecodes.c
@@ -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);
}