Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9af740b

Browse files
committed
merge
2 parents 21fb9f1 + a3fec15 commit 9af740b

3 files changed

Lines changed: 20 additions & 7 deletions

File tree

Lib/test/test_with.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,24 @@ def fooNotDeclared():
109109
with foo: pass
110110
self.assertRaises(NameError, fooNotDeclared)
111111

112-
def testEnterAttributeError(self):
112+
def testEnterAttributeError1(self):
113113
class LacksEnter(object):
114114
def __exit__(self, type, value, traceback):
115115
pass
116116

117117
def fooLacksEnter():
118118
foo = LacksEnter()
119119
with foo: pass
120-
self.assertRaises(AttributeError, fooLacksEnter)
120+
self.assertRaisesRegexp(AttributeError, '__enter__', fooLacksEnter)
121+
122+
def testEnterAttributeError2(self):
123+
class LacksEnterAndExit(object):
124+
pass
125+
126+
def fooLacksEnterAndExit():
127+
foo = LacksEnterAndExit()
128+
with foo: pass
129+
self.assertRaisesRegexp(AttributeError, '__enter__', fooLacksEnterAndExit)
121130

122131
def testExitAttributeError(self):
123132
class LacksExit(object):
@@ -127,7 +136,7 @@ def __enter__(self):
127136
def fooLacksExit():
128137
foo = LacksExit()
129138
with foo: pass
130-
self.assertRaises(AttributeError, fooLacksExit)
139+
self.assertRaisesRegexp(AttributeError, '__exit__', fooLacksExit)
131140

132141
def assertRaisesSyntaxError(self, codestr):
133142
def shouldRaiseSyntaxError(s):

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ Core and Builtins
1515

1616
- Issue #28532: Show sys.version when -V option is supplied twice.
1717

18+
- Issue #27100: The with-statement now checks for __enter__ before it
19+
checks for __exit__. This gives less confusing error messages when
20+
both methods are missing. Patch by Jonathan Ellington.
21+
1822
- Issue #28746: Fix the set_inheritable() file descriptor method on platforms
1923
that do not have the ioctl FIOCLEX and FIONCLEX commands.
2024

Python/ceval.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3141,15 +3141,15 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
31413141
_Py_IDENTIFIER(__exit__);
31423142
_Py_IDENTIFIER(__enter__);
31433143
PyObject *mgr = TOP();
3144-
PyObject *exit = special_lookup(mgr, &PyId___exit__), *enter;
3144+
PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
31453145
PyObject *res;
3146+
if (enter == NULL)
3147+
goto error;
3148+
exit = special_lookup(mgr, &PyId___exit__);
31463149
if (exit == NULL)
31473150
goto error;
31483151
SET_TOP(exit);
3149-
enter = special_lookup(mgr, &PyId___enter__);
31503152
Py_DECREF(mgr);
3151-
if (enter == NULL)
3152-
goto error;
31533153
res = PyObject_CallFunctionObjArgs(enter, NULL);
31543154
Py_DECREF(enter);
31553155
if (res == NULL)

0 commit comments

Comments
 (0)