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

A TypeError is now raised instead of an AttributeError in
"with" and "async with" statements for objects which do not
support the context manager or asynchronous context manager
protocols correspondingly.
This commit is contained in:
Serhiy Storchaka 2021-06-29 11:27:04 +03:00 committed by GitHub
parent 48e3a1d95a
commit 20a88004ba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 48 additions and 26 deletions

View file

@ -117,7 +117,7 @@ class FailureTestCase(unittest.TestCase):
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):
@ -126,7 +126,7 @@ class FailureTestCase(unittest.TestCase):
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):
@ -136,7 +136,7 @@ class FailureTestCase(unittest.TestCase):
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):