Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
d527259f
Commit
d527259f
authored
May 19, 2012
by
Hynek Schlawack
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#13152: Allow to specify a custom tabsize for expanding tabs in textwrap
Patch by John Feuerstein.
parent
d34b57a9
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
29 additions
and
4 deletions
+29
-4
Doc/library/textwrap.rst
Doc/library/textwrap.rst
+9
-0
Lib/test/test_textwrap.py
Lib/test/test_textwrap.py
+8
-0
Lib/textwrap.py
Lib/textwrap.py
+9
-4
Misc/NEWS
Misc/NEWS
+3
-0
No files found.
Doc/library/textwrap.rst
View file @
d527259f
...
...
@@ -107,6 +107,15 @@ indentation from strings that have unwanted whitespace to the left of the text.
expanded to spaces using the :meth:`expandtabs` method of *text*.
.. attribute:: tabsize
(default: ``8``) If :attr:`expand_tabs` is true, then all tab characters
in *text* will be expanded to zero or more spaces, depending on the
current column and the given tab size.
.. versionadded:: 3.3
.. attribute:: replace_whitespace
(default: ``True``) If true, each whitespace character (as defined by
...
...
Lib/test/test_textwrap.py
View file @
d527259f
...
...
@@ -91,6 +91,14 @@ What a mess!
result
=
wrapper
.
fill
(
text
)
self
.
check
(
result
,
'
\
n
'
.
join
(
expect
))
text
=
"
\
t
Test
\
t
default
\
t
\
t
tabsize."
expect
=
[
" Test default tabsize."
]
self
.
check_wrap
(
text
,
80
,
expect
)
text
=
"
\
t
Test
\
t
custom
\
t
\
t
tabsize."
expect
=
[
" Test custom tabsize."
]
self
.
check_wrap
(
text
,
80
,
expect
,
tabsize
=
4
)
def
test_fix_sentence_endings
(
self
):
wrapper
=
TextWrapper
(
60
,
fix_sentence_endings
=
True
)
...
...
Lib/textwrap.py
View file @
d527259f
...
...
@@ -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
its line. If false, each tab is treated as a single character.
Each tab will become 0 .. 'tabsize' spaces, depending on its position
in its line. If false, each tab is treated as a single character.
tabsize (default: 8)
Expand tabs in input text to 0 .. 'tabsize' spaces, unless
'expand_tabs' is false.
replace_whitespace (default: true)
Replace all whitespace characters in the input text by spaces
after tab expansion. Note that if expand_tabs is false and
...
...
@@ -100,7 +103,8 @@ class TextWrapper:
fix_sentence_endings=False,
break_long_words=True,
drop_whitespace=True,
break_on_hyphens=True):
break_on_hyphens=True,
tabsize=8):
self.width = width
self.initial_indent = initial_indent
self.subsequent_indent = subsequent_indent
...
...
@@ -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
# -- Private methods -----------------------------------------------
...
...
@@ -123,7 +128,7 @@ class TextWrapper:
becomes "
foo
bar
baz
".
"""
if self.expand_tabs:
text = text.expandtabs()
text = text.expandtabs(
self.tabsize
)
if self.replace_whitespace:
text = text.translate(self.unicode_whitespace_trans)
return text
...
...
Misc/NEWS
View file @
d527259f
...
...
@@ -38,6 +38,9 @@ Core and Builtins
Library
-------
- Issue #13152: Allow to specify a custom tabsize for expanding tabs in
textwrap. Patch by John Feuerstein.
- Issue #14721: Send the correct '
Content
-
length
:
0
' header when the body is an
empty string ''. Initial Patch contributed by Arve Knudsen.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment