Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e7c5f60

Browse filesBrowse files
authored
gh-130167: Improve the error case for textwrap.dedent (#132666)
1 parent c821b71 commit e7c5f60
Copy full SHA for e7c5f60

File tree

3 files changed

+13
-5
lines changed
Filter options

3 files changed

+13
-5
lines changed

‎Lib/test/test_textwrap.py

Copy file name to clipboardExpand all lines: Lib/test/test_textwrap.py
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,13 @@ def test_subsequent_indent(self):
765765
# of IndentTestCase!
766766
class DedentTestCase(unittest.TestCase):
767767

768+
def test_type_error(self):
769+
with self.assertRaisesRegex(TypeError, "expected str object, not"):
770+
dedent(0)
771+
772+
with self.assertRaisesRegex(TypeError, "expected str object, not"):
773+
dedent(b'')
774+
768775
def assertUnchanged(self, text):
769776
"""assert that dedent() has no effect on 'text'"""
770777
self.assertEqual(text, dedent(text))

‎Lib/textwrap.py

Copy file name to clipboardExpand all lines: Lib/textwrap.py
+5-4Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -426,10 +426,11 @@ def dedent(text):
426426
427427
Entirely blank lines are normalized to a newline character.
428428
"""
429-
if not text:
430-
return text
431-
432-
lines = text.split('\n')
429+
try:
430+
lines = text.split('\n')
431+
except (AttributeError, TypeError):
432+
msg = f'expected str object, not {type(text).__qualname__!r}'
433+
raise TypeError(msg) from None
433434

434435
# Get length of leading whitespace, inspired by ``os.path.commonprefix()``.
435436
non_blank_lines = [l for l in lines if l and not l.isspace()]

‎Misc/NEWS.d/3.14.0a7.rst

Copy file name to clipboardExpand all lines: Misc/NEWS.d/3.14.0a7.rst
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ Improve the import time of the :mod:`ast` module by extracting the
288288
.. nonce: 8M-HVz
289289
.. section: Library
290290
291-
Improved performance of :func:`textwrap.dedent` by an average of ~1.3x.
291+
Improved performance of :func:`textwrap.indent` by an average of ~1.3x.
292292
Patch by Adam Turner.
293293

294294
..

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.