Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32.2k
gh-99181: fix except* on unhashable exceptions#99192
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
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
6 commits Select commitHold shift + click to select a range
ec8e680
gh-99181: fix except* on unhashable exceptions
iritkatriel64406d6
fix typo in comment
iritkatriele725f63
rename exc_key --> exc_id
iritkatriel9f8a78d
📜🤖 Added by blurb_it.
blurb-it[bot]094bb0c
hash-->__hash__
iritkatriel9d4bea8
add test with broken __eq__
iritkatrielFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1000,5 +1000,204 @@ def test_exc_info_restored(self): | ||
self.assertEqual(sys.exc_info(), (None, None, None)) | ||
class TestExceptStar_WeirdLeafExceptions(ExceptStarTest): | ||
# Test that except* works when leaf exceptions are | ||
# unhashable or have a bad custom __eq__ | ||
class UnhashableExc(ValueError): | ||
__hash__ = None | ||
class AlwaysEqualExc(ValueError): | ||
def __eq__(self, other): | ||
return True | ||
class NeverEqualExc(ValueError): | ||
iritkatriel marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
def __eq__(self, other): | ||
return False | ||
class BrokenEqualExc(ValueError): | ||
def __eq__(self, other): | ||
raise RuntimeError() | ||
def setUp(self): | ||
self.bad_types = [self.UnhashableExc, | ||
self.AlwaysEqualExc, | ||
self.NeverEqualExc, | ||
self.BrokenEqualExc] | ||
def except_type(self, eg, type): | ||
match, rest = None, None | ||
try: | ||
try: | ||
raise eg | ||
except* type as e: | ||
match = e | ||
except Exception as e: | ||
rest = e | ||
return match, rest | ||
def test_catch_unhashable_leaf_exception(self): | ||
for Bad in self.bad_types: | ||
with self.subTest(Bad): | ||
eg = ExceptionGroup("eg", [TypeError(1), Bad(2)]) | ||
match, rest = self.except_type(eg, Bad) | ||
self.assertExceptionIsLike( | ||
match, ExceptionGroup("eg", [Bad(2)])) | ||
self.assertExceptionIsLike( | ||
rest, ExceptionGroup("eg", [TypeError(1)])) | ||
def test_propagate_unhashable_leaf(self): | ||
for Bad in self.bad_types: | ||
with self.subTest(Bad): | ||
eg = ExceptionGroup("eg", [TypeError(1), Bad(2)]) | ||
match, rest = self.except_type(eg, TypeError) | ||
self.assertExceptionIsLike( | ||
match, ExceptionGroup("eg", [TypeError(1)])) | ||
self.assertExceptionIsLike( | ||
rest, ExceptionGroup("eg", [Bad(2)])) | ||
def test_catch_nothing_unhashable_leaf(self): | ||
for Bad in self.bad_types: | ||
with self.subTest(Bad): | ||
eg = ExceptionGroup("eg", [TypeError(1), Bad(2)]) | ||
match, rest = self.except_type(eg, OSError) | ||
self.assertIsNone(match) | ||
self.assertExceptionIsLike(rest, eg) | ||
def test_catch_everything_unhashable_leaf(self): | ||
for Bad in self.bad_types: | ||
with self.subTest(Bad): | ||
eg = ExceptionGroup("eg", [TypeError(1), Bad(2)]) | ||
match, rest = self.except_type(eg, Exception) | ||
self.assertExceptionIsLike(match, eg) | ||
self.assertIsNone(rest) | ||
def test_reraise_unhashable_leaf(self): | ||
for Bad in self.bad_types: | ||
with self.subTest(Bad): | ||
eg = ExceptionGroup( | ||
"eg", [TypeError(1), Bad(2), ValueError(3)]) | ||
try: | ||
try: | ||
raise eg | ||
except* TypeError: | ||
pass | ||
except* Bad: | ||
raise | ||
except Exception as e: | ||
exc = e | ||
self.assertExceptionIsLike( | ||
exc, ExceptionGroup("eg", [Bad(2), ValueError(3)])) | ||
class TestExceptStar_WeirdExceptionGroupSubclass(ExceptStarTest): | ||
# Test that except* works with exception groups that are | ||
# unhashable or have a bad custom __eq__ | ||
class UnhashableEG(ExceptionGroup): | ||
__hash__ = None | ||
def derive(self, excs): | ||
return type(self)(self.message, excs) | ||
class AlwaysEqualEG(ExceptionGroup): | ||
def __eq__(self, other): | ||
return True | ||
def derive(self, excs): | ||
return type(self)(self.message, excs) | ||
class NeverEqualEG(ExceptionGroup): | ||
def __eq__(self, other): | ||
return False | ||
def derive(self, excs): | ||
return type(self)(self.message, excs) | ||
class BrokenEqualEG(ExceptionGroup): | ||
def __eq__(self, other): | ||
raise RuntimeError() | ||
def derive(self, excs): | ||
return type(self)(self.message, excs) | ||
def setUp(self): | ||
self.bad_types = [self.UnhashableEG, | ||
self.AlwaysEqualEG, | ||
self.NeverEqualEG, | ||
self.BrokenEqualEG] | ||
def except_type(self, eg, type): | ||
match, rest = None, None | ||
try: | ||
try: | ||
raise eg | ||
except* type as e: | ||
match = e | ||
except Exception as e: | ||
rest = e | ||
return match, rest | ||
def test_catch_some_unhashable_exception_group_subclass(self): | ||
for BadEG in self.bad_types: | ||
with self.subTest(BadEG): | ||
eg = BadEG("eg", | ||
[TypeError(1), | ||
BadEG("nested", [ValueError(2)])]) | ||
match, rest = self.except_type(eg, TypeError) | ||
self.assertExceptionIsLike(match, BadEG("eg", [TypeError(1)])) | ||
self.assertExceptionIsLike(rest, | ||
BadEG("eg", [BadEG("nested", [ValueError(2)])])) | ||
def test_catch_none_unhashable_exception_group_subclass(self): | ||
for BadEG in self.bad_types: | ||
with self.subTest(BadEG): | ||
eg = BadEG("eg", | ||
[TypeError(1), | ||
BadEG("nested", [ValueError(2)])]) | ||
match, rest = self.except_type(eg, OSError) | ||
self.assertIsNone(match) | ||
self.assertExceptionIsLike(rest, eg) | ||
def test_catch_all_unhashable_exception_group_subclass(self): | ||
for BadEG in self.bad_types: | ||
with self.subTest(BadEG): | ||
eg = BadEG("eg", | ||
[TypeError(1), | ||
BadEG("nested", [ValueError(2)])]) | ||
match, rest = self.except_type(eg, Exception) | ||
self.assertExceptionIsLike(match, eg) | ||
self.assertIsNone(rest) | ||
def test_reraise_unhashable_eg(self): | ||
for BadEG in self.bad_types: | ||
with self.subTest(BadEG): | ||
eg = BadEG("eg", | ||
[TypeError(1), ValueError(2), | ||
BadEG("nested", [ValueError(3), OSError(4)])]) | ||
try: | ||
try: | ||
raise eg | ||
except* ValueError: | ||
pass | ||
except* OSError: | ||
raise | ||
except Exception as e: | ||
exc = e | ||
self.assertExceptionIsLike( | ||
exc, BadEG("eg", [TypeError(1), | ||
BadEG("nested", [OSError(4)])])) | ||
if __name__ == '__main__': | ||
unittest.main() |
1 change: 1 addition & 0 deletionsMisc/NEWS.d/next/Core and Builtins/2022-11-07-10-29-41.gh-issue-99181.bfG4bI.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fix failure in :keyword:`except* <except_star>` with unhashable exceptions. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.