Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f0ba764

Browse files
committed
SF #847346: merge from release23-maint branch: remove misguided
optimization for short input; beef up tests for fix_sentence_endings feature.
1 parent 10c6606 commit f0ba764

2 files changed

Lines changed: 50 additions & 14 deletions

File tree

Lib/test/test_textwrap.py

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def check_split(self, text, expect):
4747
class WrapTestCase(BaseTestCase):
4848

4949
def setUp(self):
50-
self.wrapper = TextWrapper(width=45, fix_sentence_endings=True)
50+
self.wrapper = TextWrapper(width=45)
5151

5252
def test_simple(self):
5353
# Simple case: just words, spaces, and a bit of punctuation
@@ -84,13 +84,51 @@ def test_whitespace(self):
8484
"wrapped. Some lines are tabbed too. What a",
8585
"mess!"]
8686

87-
result = self.wrapper.wrap(text)
87+
wrapper = TextWrapper(45, fix_sentence_endings=True)
88+
result = wrapper.wrap(text)
8889
self.check(result, expect)
8990

90-
result = self.wrapper.fill(text)
91+
result = wrapper.fill(text)
9192
self.check(result, '\n'.join(expect))
9293

93-
94+
def test_fix_sentence_endings(self):
95+
wrapper = TextWrapper(60, fix_sentence_endings=True)
96+
97+
# SF #847346: ensure that fix_sentence_endings=True does the
98+
# right thing even on input short enough that it doesn't need to
99+
# be wrapped.
100+
text = "A short line. Note the single space."
101+
expect = ["A short line. Note the single space."]
102+
self.check(wrapper.wrap(text), expect)
103+
104+
# Test some of the hairy end cases that _fix_sentence_endings()
105+
# is supposed to handle (the easy stuff is tested in
106+
# test_whitespace() above).
107+
text = "Well, Doctor? What do you think?"
108+
expect = ["Well, Doctor? What do you think?"]
109+
self.check(wrapper.wrap(text), expect)
110+
111+
text = "Well, Doctor?\nWhat do you think?"
112+
self.check(wrapper.wrap(text), expect)
113+
114+
text = 'I say, chaps! Anyone for "tennis?"\nHmmph!'
115+
expect = ['I say, chaps! Anyone for "tennis?" Hmmph!']
116+
self.check(wrapper.wrap(text), expect)
117+
118+
wrapper.width = 20
119+
expect = ['I say, chaps!', 'Anyone for "tennis?"', 'Hmmph!']
120+
self.check(wrapper.wrap(text), expect)
121+
122+
text = 'And she said, "Go to hell!"\nCan you believe that?'
123+
expect = ['And she said, "Go to',
124+
'hell!" Can you',
125+
'believe that?']
126+
self.check(wrapper.wrap(text), expect)
127+
128+
wrapper.width = 60
129+
expect = ['And she said, "Go to hell!" Can you believe that?']
130+
self.check(wrapper.wrap(text), expect)
131+
94132
def test_wrap_short(self):
95133
# Wrapping to make short lines longer
96134

Lib/textwrap.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,14 @@ class TextWrapper:
9090
% string.lowercase)
9191

9292

93-
def __init__ (self,
94-
width=70,
95-
initial_indent="",
96-
subsequent_indent="",
97-
expand_tabs=True,
98-
replace_whitespace=True,
99-
fix_sentence_endings=False,
100-
break_long_words=True):
93+
def __init__(self,
94+
width=70,
95+
initial_indent="",
96+
subsequent_indent="",
97+
expand_tabs=True,
98+
replace_whitespace=True,
99+
fix_sentence_endings=False,
100+
break_long_words=True):
101101
self.width = width
102102
self.initial_indent = initial_indent
103103
self.subsequent_indent = subsequent_indent
@@ -268,8 +268,6 @@ def wrap(self, text):
268268
"""
269269
text = self._munge_whitespace(text)
270270
indent = self.initial_indent
271-
if len(text) + len(indent) <= self.width:
272-
return [indent + text]
273271
chunks = self._split(text)
274272
if self.fix_sentence_endings:
275273
self._fix_sentence_endings(chunks)

0 commit comments

Comments
 (0)