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

Skip to content

Commit d527259

Browse files
committed
#13152: Allow to specify a custom tabsize for expanding tabs in textwrap
Patch by John Feuerstein.
1 parent d34b57a commit d527259

4 files changed

Lines changed: 29 additions & 4 deletions

File tree

Doc/library/textwrap.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ indentation from strings that have unwanted whitespace to the left of the text.
107107
expanded to spaces using the :meth:`expandtabs` method of *text*.
108108

109109

110+
.. attribute:: tabsize
111+
112+
(default: ``8``) If :attr:`expand_tabs` is true, then all tab characters
113+
in *text* will be expanded to zero or more spaces, depending on the
114+
current column and the given tab size.
115+
116+
.. versionadded:: 3.3
117+
118+
110119
.. attribute:: replace_whitespace
111120

112121
(default: ``True``) If true, each whitespace character (as defined by

Lib/test/test_textwrap.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ def test_whitespace(self):
9191
result = wrapper.fill(text)
9292
self.check(result, '\n'.join(expect))
9393

94+
text = "\tTest\tdefault\t\ttabsize."
95+
expect = [" Test default tabsize."]
96+
self.check_wrap(text, 80, expect)
97+
98+
text = "\tTest\tcustom\t\ttabsize."
99+
expect = [" Test custom tabsize."]
100+
self.check_wrap(text, 80, expect, tabsize=4)
101+
94102
def test_fix_sentence_endings(self):
95103
wrapper = TextWrapper(60, fix_sentence_endings=True)
96104

Lib/textwrap.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,11 @@ class TextWrapper:
3939
of wrapped output; also counts towards each line's width.
4040
expand_tabs (default: true)
4141
Expand tabs in input text to spaces before further processing.
42-
Each tab will become 1 .. 8 spaces, depending on its position in
43-
its line. If false, each tab is treated as a single character.
42+
Each tab will become 0 .. 'tabsize' spaces, depending on its position
43+
in its line. If false, each tab is treated as a single character.
44+
tabsize (default: 8)
45+
Expand tabs in input text to 0 .. 'tabsize' spaces, unless
46+
'expand_tabs' is false.
4447
replace_whitespace (default: true)
4548
Replace all whitespace characters in the input text by spaces
4649
after tab expansion. Note that if expand_tabs is false and
@@ -100,7 +103,8 @@ def __init__(self,
100103
fix_sentence_endings=False,
101104
break_long_words=True,
102105
drop_whitespace=True,
103-
break_on_hyphens=True):
106+
break_on_hyphens=True,
107+
tabsize=8):
104108
self.width = width
105109
self.initial_indent = initial_indent
106110
self.subsequent_indent = subsequent_indent
@@ -110,6 +114,7 @@ def __init__(self,
110114
self.break_long_words = break_long_words
111115
self.drop_whitespace = drop_whitespace
112116
self.break_on_hyphens = break_on_hyphens
117+
self.tabsize = tabsize
113118

114119

115120
# -- Private methods -----------------------------------------------
@@ -123,7 +128,7 @@ def _munge_whitespace(self, text):
123128
becomes " foo bar baz".
124129
"""
125130
if self.expand_tabs:
126-
text = text.expandtabs()
131+
text = text.expandtabs(self.tabsize)
127132
if self.replace_whitespace:
128133
text = text.translate(self.unicode_whitespace_trans)
129134
return text

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ Core and Builtins
3838
Library
3939
-------
4040

41+
- Issue #13152: Allow to specify a custom tabsize for expanding tabs in
42+
textwrap. Patch by John Feuerstein.
43+
4144
- Issue #14721: Send the correct 'Content-length: 0' header when the body is an
4245
empty string ''. Initial Patch contributed by Arve Knudsen.
4346

0 commit comments

Comments
 (0)