Skip to content

Commit 70a94b1

Browse files
author
gward
committed
SF #1149508: ensure textwrap handles hyphenated numbers correctly,
eg. "2004-03-04" is not broken across lines. (Merged from 2.4 branch.) git-svn-id: http://svn.python.org/projects/python/trunk@38575 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 9361683 commit 70a94b1

3 files changed

Lines changed: 26 additions & 3 deletions

File tree

Lib/test/test_textwrap.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,24 @@ def test_hyphenated(self):
165165
["this-is-a-useful-feature-for-reformatting-",
166166
"posts-from-tim-peters'ly"])
167167

168+
def test_hyphenated_numbers(self):
169+
# Test that hyphenated numbers (eg. dates) are not broken like words.
170+
text = ("Python 1.0.0 was released on 1994-01-26. Python 1.0.1 was\n"
171+
"released on 1994-02-15.")
172+
173+
self.check_wrap(text, 30, ['Python 1.0.0 was released on',
174+
'1994-01-26. Python 1.0.1 was',
175+
'released on 1994-02-15.'])
176+
self.check_wrap(text, 40, ['Python 1.0.0 was released on 1994-01-26.',
177+
'Python 1.0.1 was released on 1994-02-15.'])
178+
179+
text = "I do all my shopping at 7-11."
180+
self.check_wrap(text, 25, ["I do all my shopping at",
181+
"7-11."])
182+
self.check_wrap(text, 27, ["I do all my shopping at",
183+
"7-11."])
184+
self.check_wrap(text, 29, ["I do all my shopping at 7-11."])
185+
168186
def test_em_dash(self):
169187
# Test text with em-dashes
170188
text = "Em-dashes should be written -- thus."

Lib/textwrap.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,10 @@ class TextWrapper:
7878
# splits into
7979
# Hello/ /there/ /--/ /you/ /goof-/ball,/ /use/ /the/ /-b/ /option!
8080
# (after stripping out empty strings).
81-
wordsep_re = re.compile(r'(\s+|' # any whitespace
82-
r'[^\s\w]*\w{2,}-(?=\w{2,})|' # hyphenated words
83-
r'(?<=[\w\!\"\'\&\.\,\?])-{2,}(?=\w))') # em-dash
81+
wordsep_re = re.compile(
82+
r'(\s+|' # any whitespace
83+
r'[^\s\w]*\w+[a-zA-Z]-(?=\w+[a-zA-Z])|' # hyphenated words
84+
r'(?<=[\w\!\"\'\&\.\,\?])-{2,}(?=\w))') # em-dash
8485

8586
# XXX this is not locale- or charset-aware -- string.lowercase
8687
# is US-ASCII only (and therefore English-only)

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@ Library
206206
- ``UserString.MutableString`` now supports negative indices in
207207
``__setitem__`` and ``__delitem__``
208208

209+
- Bug #1149508: ``textwrap`` now handles hyphenated numbers (eg. "2004-03-05")
210+
correctly.
211+
212+
209213
Build
210214
-----
211215

0 commit comments

Comments
 (0)