Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32k
gh-111375: Fix handling of exceptions within@contextmanager
-decorated functions#111676
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Changes fromall commits
0138ff1
b285e9a
ea784d7
bf4ef1e
bbc212b
59ef993
c0e7400
5e8323a
a7e2dc9
90fdbe0
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -306,6 +306,98 @@ def woohoo(): | ||
with woohoo(): | ||
raise StopIteration | ||
def test_contextmanager_handling_exception_resets_exc_info(self): | ||
# Test that sys.exc_info() is correctly unset after handling the error | ||
# when used within a context manager | ||
@contextmanager | ||
def ctx(reraise=False): | ||
try: | ||
self.assertIsNone(sys.exception()) | ||
yield | ||
except: | ||
self.assertIsInstance(sys.exception(), ZeroDivisionError) | ||
if reraise: | ||
raise | ||
else: | ||
self.assertIsNone(sys.exception()) | ||
self.assertIsNone(sys.exception()) | ||
with ctx(): | ||
pass | ||
with ctx(): | ||
1/0 | ||
with self.assertRaises(ZeroDivisionError): | ||
with ctx(reraise=True): | ||
1/0 | ||
iritkatriel marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
def test_contextmanager_while_handling(self): | ||
# test that any exceptions currently being handled are preserved | ||
# through the context manager | ||
@contextmanager | ||
def ctx(reraise=False): | ||
# called while handling an IndexError --> TypeError | ||
self.assertIsInstance(sys.exception(), TypeError) | ||
self.assertIsInstance(sys.exception().__context__, IndexError) | ||
exc_ctx = sys.exception() | ||
try: | ||
# raises a ValueError --> ZeroDivisionError | ||
yield | ||
except: | ||
self.assertIsInstance(sys.exception(), ZeroDivisionError) | ||
self.assertIsInstance(sys.exception().__context__, ValueError) | ||
# original error context is preserved | ||
self.assertIs(sys.exception().__context__.__context__, exc_ctx) | ||
if reraise: | ||
raise | ||
# inner error handled, context should now be the original context | ||
self.assertIs(sys.exception(), exc_ctx) | ||
try: | ||
raise IndexError() | ||
except: | ||
try: | ||
raise TypeError() | ||
except: | ||
with ctx(): | ||
self.assertIsInstance(sys.exception(), TypeError) | ||
self.assertIsInstance(sys.exception().__context__, IndexError) | ||
try: | ||
raise ValueError() | ||
except: | ||
self.assertIsInstance(sys.exception(), ValueError) | ||
self.assertIsInstance(sys.exception().__context__, TypeError) | ||
self.assertIsInstance(sys.exception().__context__.__context__, IndexError) | ||
1/0 | ||
self.assertIsInstance(sys.exception(), TypeError) | ||
pR0Ps marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
self.assertIsInstance(sys.exception().__context__, IndexError) | ||
self.assertIsInstance(sys.exception(), IndexError) | ||
try: | ||
raise IndexError() | ||
except: | ||
try: | ||
raise TypeError() | ||
except: | ||
with self.assertRaises(ZeroDivisionError): | ||
with ctx(reraise=True): | ||
self.assertIsInstance(sys.exception(), TypeError) | ||
self.assertIsInstance(sys.exception().__context__, IndexError) | ||
try: | ||
raise ValueError() | ||
except: | ||
self.assertIsInstance(sys.exception(), ValueError) | ||
pR0Ps marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
self.assertIsInstance(sys.exception().__context__, TypeError) | ||
self.assertIsInstance(sys.exception().__context__.__context__, IndexError) | ||
1/0 | ||
self.assertIsInstance(sys.exception(), TypeError) | ||
self.assertIsInstance(sys.exception().__context__, IndexError) | ||
self.assertIsInstance(sys.exception(), IndexError) | ||
def _create_contextmanager_attribs(self): | ||
def attribs(**kw): | ||
def decorate(func): | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Add sys._set_exception() function that can set/clear the current exception | ||
context. Patch by Carey Metcalfe. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Fix handling of ``sys.exception()`` within ``@contextlib.contextmanager`` | ||
functions. Patch by Carey Metcalfe. |
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.