Skip to content

bpo-12022: Change error type for bad objects in "with" and "async with" #26809

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
6 changes: 6 additions & 0 deletions Doc/whatsnew/3.11.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ Other Language Changes
======================


* A :exc:`TypeError` is now raised instead of an :exc:`AttributeError` in
:keyword:`with` and :keyword:`async with` statements for objects which do not
support the :term:`context manager` or :term:`asynchronous context manager`
protocols correspondingly.
(Contributed by Serhiy Storchaka in :issue:`12022`.)


New Modules
===========
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_contextlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ def __unter__(self):
def __exit__(self, *exc):
pass

with self.assertRaises(AttributeError):
with self.assertRaisesRegex(TypeError, 'the context manager'):
with mycontext():
pass

Expand All @@ -503,7 +503,7 @@ def __enter__(self):
def __uxit__(self, *exc):
pass

with self.assertRaises(AttributeError):
with self.assertRaisesRegex(TypeError, 'the context manager.*__exit__'):
with mycontext():
pass

Expand Down
6 changes: 3 additions & 3 deletions Lib/test/test_coroutines.py
Original file line number Diff line number Diff line change
Expand Up @@ -1210,7 +1210,7 @@ async def foo():
async with CM():
body_executed = True

with self.assertRaisesRegex(AttributeError, '__aexit__'):
with self.assertRaisesRegex(TypeError, 'asynchronous context manager.*__aexit__'):
run_async(foo())
self.assertFalse(body_executed)

Expand All @@ -1224,7 +1224,7 @@ async def foo():
async with CM():
body_executed = True

with self.assertRaisesRegex(AttributeError, '__aenter__'):
with self.assertRaisesRegex(TypeError, 'asynchronous context manager'):
run_async(foo())
self.assertFalse(body_executed)

Expand All @@ -1237,7 +1237,7 @@ async def foo():
async with CM():
body_executed = True

with self.assertRaisesRegex(AttributeError, '__aenter__'):
with self.assertRaisesRegex(TypeError, 'asynchronous context manager'):
run_async(foo())
self.assertFalse(body_executed)

Expand Down
6 changes: 3 additions & 3 deletions Lib/test/test_with.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def __exit__(self, type, value, traceback):
def fooLacksEnter():
foo = LacksEnter()
with foo: pass
self.assertRaisesRegex(AttributeError, '__enter__', fooLacksEnter)
self.assertRaisesRegex(TypeError, 'the context manager', fooLacksEnter)

def testEnterAttributeError2(self):
class LacksEnterAndExit(object):
Expand All @@ -126,7 +126,7 @@ class LacksEnterAndExit(object):
def fooLacksEnterAndExit():
foo = LacksEnterAndExit()
with foo: pass
self.assertRaisesRegex(AttributeError, '__enter__', fooLacksEnterAndExit)
self.assertRaisesRegex(TypeError, 'the context manager', fooLacksEnterAndExit)

def testExitAttributeError(self):
class LacksExit(object):
Expand All @@ -136,7 +136,7 @@ def __enter__(self):
def fooLacksExit():
foo = LacksExit()
with foo: pass
self.assertRaisesRegex(AttributeError, '__exit__', fooLacksExit)
self.assertRaisesRegex(TypeError, 'the context manager.*__exit__', fooLacksExit)

def assertRaisesSyntaxError(self, codestr):
def shouldRaiseSyntaxError(s):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
A :exc:`TypeError` is now raised instead of an :exc:`AttributeError` in
:keyword:`with` and :keyword:`async with` statements for objects which do
not support the :term:`context manager` or :term:`asynchronous context
manager` protocols correspondingly.
48 changes: 30 additions & 18 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ static void format_exc_check_arg(PyThreadState *, PyObject *, const char *, PyOb
static void format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg);
static PyObject * unicode_concatenate(PyThreadState *, PyObject *, PyObject *,
PyFrameObject *, const _Py_CODEUNIT *);
static PyObject * special_lookup(PyThreadState *, PyObject *, _Py_Identifier *);
static int check_args_iterable(PyThreadState *, PyObject *func, PyObject *vararg);
static void format_kwargs_error(PyThreadState *, PyObject *func, PyObject *kwargs);
static void format_awaitable_error(PyThreadState *, PyTypeObject *, int, int);
Expand Down Expand Up @@ -3841,13 +3840,26 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
_Py_IDENTIFIER(__aenter__);
_Py_IDENTIFIER(__aexit__);
PyObject *mgr = TOP();
PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
PyObject *res;
PyObject *enter = _PyObject_LookupSpecial(mgr, &PyId___aenter__);
if (enter == NULL) {
if (!_PyErr_Occurred(tstate)) {
_PyErr_Format(tstate, PyExc_TypeError,
"'%.200s' object does not support the "
"asynchronous context manager protocol",
Py_TYPE(mgr)->tp_name);
}
goto error;
}
PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
PyObject *exit = _PyObject_LookupSpecial(mgr, &PyId___aexit__);
if (exit == NULL) {
if (!_PyErr_Occurred(tstate)) {
_PyErr_Format(tstate, PyExc_TypeError,
"'%.200s' object does not support the "
"asynchronous context manager protocol "
"(missed __aexit__ method)",
Py_TYPE(mgr)->tp_name);
}
Py_DECREF(enter);
goto error;
}
Expand All @@ -3866,13 +3878,26 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
_Py_IDENTIFIER(__enter__);
_Py_IDENTIFIER(__exit__);
PyObject *mgr = TOP();
PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
PyObject *res;
PyObject *enter = _PyObject_LookupSpecial(mgr, &PyId___enter__);
if (enter == NULL) {
if (!_PyErr_Occurred(tstate)) {
_PyErr_Format(tstate, PyExc_TypeError,
"'%.200s' object does not support the "
"context manager protocol",
Py_TYPE(mgr)->tp_name);
}
goto error;
}
PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
PyObject *exit = _PyObject_LookupSpecial(mgr, &PyId___exit__);
if (exit == NULL) {
if (!_PyErr_Occurred(tstate)) {
_PyErr_Format(tstate, PyExc_TypeError,
"'%.200s' object does not support the "
"context manager protocol "
"(missed __exit__ method)",
Py_TYPE(mgr)->tp_name);
}
Py_DECREF(enter);
goto error;
}
Expand Down Expand Up @@ -5107,19 +5132,6 @@ PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
}


static PyObject *
special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
{
PyObject *res;
res = _PyObject_LookupSpecial(o, id);
if (res == NULL && !_PyErr_Occurred(tstate)) {
_PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(id));
return NULL;
}
return res;
}


/* Logic for the raise statement (too complicated for inlining).
This *consumes* a reference count to each of its arguments. */
static int
Expand Down