8000 gh-123378: fix a crash in `UnicodeError.__str__` by picnixz · Pull Request #124935 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-123378: fix a crash in UnicodeError.__str__ #124935

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Oct 8, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
address Victor's review
  • Loading branch information
picnixz committed Oct 8, 2024
commit 44dfaeb960454266cc2e96c01d05f2bc6325a673
23 changes: 13 additions & 10 deletions Lib/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1337,25 +1337,28 @@ def test_unicode_errors_no_object(self):
for klass in klasses:
self.assertEqual(str(klass.__new__(klass)), "")

def test_unicode_error_str_gh_123378(self):
for formatter, start, end, obj in product(
(str, repr),
def test_unicode_error_str_does_not_crash(self):
# Test that str(UnicodeError(...)) does not crash.
# See https://github.com/python/cpython/issues/123378.

for start, end, objlen in product(
range(-5, 5),
range(-5, 5),
('', 'a', '123', '1234', '12345', 'abc123'),
range(7),
):
with self.subTest(formatter, obj=obj, start=start, end=end):
obj = 'a' * objlen
with self.subTest('encode', objlen=objlen, start=start, end=end):
exc = UnicodeEncodeError('utf-8', obj, start, end, '')
self.assertIsInstance(formatter(exc), str)
self.assertIsInstance(str(exc), str)

with self.subTest(formatter, obj=obj, start=start, end=end):
with self.subTest('translate', objlen=objlen, start=start, end=end):
exc = UnicodeTranslateError(obj, start, end, '')
self.assertIsInstance(formatter(exc), str)
self.assertIsInstance(str(exc), str)

encoded = obj.encode()
with self.subTest(formatter, obj=encoded, start=start, end=end):
with self.subTest('decode', objlen=objlen, start=start, end=end):
exc = UnicodeDecodeError('utf-8', encoded, start, end, '')
self.assertIsInstance(formatter(exc), str)
self.assertIsInstance(str(exc), str)

@no_tracing
def test_badisinstance(self):
Expand Down
Loading
0