mirror of
https://github.com/python/cpython.git
synced 2025-08-22 17:55:18 +00:00
gh-130167: Improve the error case for `textwrap.dedent
` (#132666)
This commit is contained in:
parent
c821b715b3
commit
e7c5f60efc
3 changed files with 13 additions and 5 deletions
|
@ -765,6 +765,13 @@ some (including a hanging indent).'''
|
||||||
# of IndentTestCase!
|
# of IndentTestCase!
|
||||||
class DedentTestCase(unittest.TestCase):
|
class DedentTestCase(unittest.TestCase):
|
||||||
|
|
||||||
|
def test_type_error(self):
|
||||||
|
with self.assertRaisesRegex(TypeError, "expected str object, not"):
|
||||||
|
dedent(0)
|
||||||
|
|
||||||
|
with self.assertRaisesRegex(TypeError, "expected str object, not"):
|
||||||
|
dedent(b'')
|
||||||
|
|
||||||
def assertUnchanged(self, text):
|
def assertUnchanged(self, text):
|
||||||
"""assert that dedent() has no effect on 'text'"""
|
"""assert that dedent() has no effect on 'text'"""
|
||||||
self.assertEqual(text, dedent(text))
|
self.assertEqual(text, dedent(text))
|
||||||
|
|
|
@ -426,10 +426,11 @@ def dedent(text):
|
||||||
|
|
||||||
Entirely blank lines are normalized to a newline character.
|
Entirely blank lines are normalized to a newline character.
|
||||||
"""
|
"""
|
||||||
if not text:
|
try:
|
||||||
return text
|
lines = text.split('\n')
|
||||||
|
except (AttributeError, TypeError):
|
||||||
lines = text.split('\n')
|
msg = f'expected str object, not {type(text).__qualname__!r}'
|
||||||
|
raise TypeError(msg) from None
|
||||||
|
|
||||||
# Get length of leading whitespace, inspired by ``os.path.commonprefix()``.
|
# Get length of leading whitespace, inspired by ``os.path.commonprefix()``.
|
||||||
non_blank_lines = [l for l in lines if l and not l.isspace()]
|
non_blank_lines = [l for l in lines if l and not l.isspace()]
|
||||||
|
|
|
@ -288,7 +288,7 @@ Improve the import time of the :mod:`ast` module by extracting the
|
||||||
.. nonce: 8M-HVz
|
.. nonce: 8M-HVz
|
||||||
.. section: Library
|
.. section: Library
|
||||||
|
|
||||||
Improved performance of :func:`textwrap.dedent` by an average of ~1.3x.
|
Improved performance of :func:`textwrap.indent` by an average of ~1.3x.
|
||||||
Patch by Adam Turner.
|
Patch by Adam Turner.
|
||||||
|
|
||||||
..
|
..
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue