This commit is contained in:
Raymond Hettinger 2016-11-21 17:24:58 -08:00
commit 9af740b99a
3 changed files with 20 additions and 7 deletions

View File

@ -109,7 +109,7 @@ class FailureTestCase(unittest.TestCase):
with foo: pass with foo: pass
self.assertRaises(NameError, fooNotDeclared) self.assertRaises(NameError, fooNotDeclared)
def testEnterAttributeError(self): def testEnterAttributeError1(self):
class LacksEnter(object): class LacksEnter(object):
def __exit__(self, type, value, traceback): def __exit__(self, type, value, traceback):
pass pass
@ -117,7 +117,16 @@ class FailureTestCase(unittest.TestCase):
def fooLacksEnter(): def fooLacksEnter():
foo = LacksEnter() foo = LacksEnter()
with foo: pass with foo: pass
self.assertRaises(AttributeError, fooLacksEnter) self.assertRaisesRegexp(AttributeError, '__enter__', fooLacksEnter)
def testEnterAttributeError2(self):
class LacksEnterAndExit(object):
pass
def fooLacksEnterAndExit():
foo = LacksEnterAndExit()
with foo: pass
self.assertRaisesRegexp(AttributeError, '__enter__', fooLacksEnterAndExit)
def testExitAttributeError(self): def testExitAttributeError(self):
class LacksExit(object): class LacksExit(object):
@ -127,7 +136,7 @@ class FailureTestCase(unittest.TestCase):
def fooLacksExit(): def fooLacksExit():
foo = LacksExit() foo = LacksExit()
with foo: pass with foo: pass
self.assertRaises(AttributeError, fooLacksExit) self.assertRaisesRegexp(AttributeError, '__exit__', fooLacksExit)
def assertRaisesSyntaxError(self, codestr): def assertRaisesSyntaxError(self, codestr):
def shouldRaiseSyntaxError(s): def shouldRaiseSyntaxError(s):

View File

@ -15,6 +15,10 @@ Core and Builtins
- Issue #28532: Show sys.version when -V option is supplied twice. - Issue #28532: Show sys.version when -V option is supplied twice.
- Issue #27100: The with-statement now checks for __enter__ before it
checks for __exit__. This gives less confusing error messages when
both methods are missing. Patch by Jonathan Ellington.
- Issue #28746: Fix the set_inheritable() file descriptor method on platforms - Issue #28746: Fix the set_inheritable() file descriptor method on platforms
that do not have the ioctl FIOCLEX and FIONCLEX commands. that do not have the ioctl FIOCLEX and FIONCLEX commands.

View File

@ -3141,15 +3141,15 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
_Py_IDENTIFIER(__exit__); _Py_IDENTIFIER(__exit__);
_Py_IDENTIFIER(__enter__); _Py_IDENTIFIER(__enter__);
PyObject *mgr = TOP(); PyObject *mgr = TOP();
PyObject *exit = special_lookup(mgr, &PyId___exit__), *enter; PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
PyObject *res; PyObject *res;
if (enter == NULL)
goto error;
exit = special_lookup(mgr, &PyId___exit__);
if (exit == NULL) if (exit == NULL)
goto error; goto error;
SET_TOP(exit); SET_TOP(exit);
enter = special_lookup(mgr, &PyId___enter__);
Py_DECREF(mgr); Py_DECREF(mgr);
if (enter == NULL)
goto error;
res = PyObject_CallFunctionObjArgs(enter, NULL); res = PyObject_CallFunctionObjArgs(enter, NULL);
Py_DECREF(enter); Py_DECREF(enter);
if (res == NULL) if (res == NULL)