mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
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:
parent
48e3a1d95a
commit
20a88004ba
6 changed files with 48 additions and 26 deletions
|
@ -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):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue