cpython: d38e821c1b80 (original) (raw)
Mercurial > cpython
changeset 77050:d38e821c1b80
#13152: Allow to specify a custom tabsize for expanding tabs in textwrap Patch by John Feuerstein. [#13152]
Hynek Schlawack hs@ox.cx | |
---|---|
date | Sat, 19 May 2012 13:33:11 +0200 |
parents | 732d70746fc0 |
children | b78c67665a7f |
files | Doc/library/textwrap.rst Lib/test/test_textwrap.py Lib/textwrap.py Misc/NEWS |
diffstat | 4 files changed, 29 insertions(+), 4 deletions(-)[+] [-] Doc/library/textwrap.rst 9 Lib/test/test_textwrap.py 8 Lib/textwrap.py 13 Misc/NEWS 3 |
line wrap: on
line diff
--- a/Doc/library/textwrap.rst
+++ b/Doc/library/textwrap.rst
@@ -107,6 +107,15 @@ indentation from strings that have unwan
expanded to spaces using the :meth:expandtabs
method of text.
- .. attribute:: tabsize +
(default: ``8``) If :attr:`expand_tabs` is true, then all tab characters[](#l1.9)
in *text* will be expanded to zero or more spaces, depending on the[](#l1.10)
current column and the given tab size.[](#l1.11)
.. versionadded:: 3.3[](#l1.13)
+
+
.. attribute:: replace_whitespace
(default: True
) If true, each whitespace character (as defined by
--- a/Lib/test/test_textwrap.py
+++ b/Lib/test/test_textwrap.py
@@ -91,6 +91,14 @@ What a mess
result = wrapper.fill(text)
self.check(result, '\n'.join(expect))
text = "\tTest\tdefault\t\ttabsize."[](#l2.7)
expect = [" Test default tabsize."][](#l2.8)
self.check_wrap(text, 80, expect)[](#l2.9)
text = "\tTest\tcustom\t\ttabsize."[](#l2.11)
expect = [" Test custom tabsize."][](#l2.12)
self.check_wrap(text, 80, expect, tabsize=4)[](#l2.13)
+ def test_fix_sentence_endings(self): wrapper = TextWrapper(60, fix_sentence_endings=True)
--- a/Lib/textwrap.py +++ b/Lib/textwrap.py @@ -39,8 +39,11 @@ class TextWrapper: of wrapped output; also counts towards each line's width. expand_tabs (default: true) Expand tabs in input text to spaces before further processing.
Each tab will become 1 .. 8 spaces, depending on its position in[](#l3.7)
its line. If false, each tab is treated as a single character.[](#l3.8)
Each tab will become 0 .. 'tabsize' spaces, depending on its position[](#l3.9)
in its line. If false, each tab is treated as a single character.[](#l3.10)
tabsize (default: 8)[](#l3.11)
Expand tabs in input text to 0 .. 'tabsize' spaces, unless[](#l3.12)
'expand_tabs' is false.[](#l3.13) replace_whitespace (default: true)[](#l3.14) Replace all whitespace characters in the input text by spaces[](#l3.15) after tab expansion. Note that if expand_tabs is false and[](#l3.16)
@@ -100,7 +103,8 @@ class TextWrapper: fix_sentence_endings=False, break_long_words=True, drop_whitespace=True,
break_on_hyphens=True):[](#l3.21)
break_on_hyphens=True,[](#l3.22)
tabsize=8):[](#l3.23) self.width = width[](#l3.24) self.initial_indent = initial_indent[](#l3.25) self.subsequent_indent = subsequent_indent[](#l3.26)
@@ -110,6 +114,7 @@ class TextWrapper: self.break_long_words = break_long_words self.drop_whitespace = drop_whitespace self.break_on_hyphens = break_on_hyphens
self.tabsize = tabsize[](#l3.31)
# -- Private methods ----------------------------------------------- @@ -123,7 +128,7 @@ class TextWrapper: becomes " foo bar baz". """ if self.expand_tabs:
text = text.expandtabs()[](#l3.39)
text = text.expandtabs(self.tabsize)[](#l3.40) if self.replace_whitespace:[](#l3.41) text = text.translate(self.unicode_whitespace_trans)[](#l3.42) return text[](#l3.43)
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -38,6 +38,9 @@ Core and Builtins Library ------- +- Issue #13152: Allow to specify a custom tabsize for expanding tabs in