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
6b3e87d3
Commit
6b3e87d3
authored
Apr 14, 2014
by
Senthil Kumaran
Browse files
Options
Browse Files
Download
Plain Diff
merge heads
parents
b735f523
7ce90743
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
42 additions
and
2 deletions
+42
-2
Lib/string.py
Lib/string.py
+21
-2
Lib/test/test_string.py
Lib/test/test_string.py
+17
-0
Misc/NEWS
Misc/NEWS
+4
-0
No files found.
Lib/string.py
View file @
6b3e87d3
...
...
@@ -169,7 +169,8 @@ class Formatter:
self.check_unused_args(used_args, args, kwargs)
return result
def _vformat(self, format_string, args, kwargs, used_args, recursion_depth):
def _vformat(self, format_string, args, kwargs, used_args, recursion_depth,
auto_arg_index=0):
if recursion_depth < 0:
raise ValueError('Max string recursion exceeded')
result = []
...
...
@@ -185,6 +186,23 @@ class Formatter:
# this is some markup, find the object and do
# the formatting
# handle arg indexing when empty field_names are given.
if field_name == '':
if auto_arg_index is False:
raise ValueError('cannot switch from manual field '
'specification to automatic field '
'numbering')
field_name = str(auto_arg_index)
auto_arg_index += 1
elif field_name.isdigit():
if auto_arg_index:
raise ValueError('cannot switch from manual field '
'specification to automatic field '
'numbering')
# disable auto arg incrementing, if it gets
# used later on, then an exception will be raised
auto_arg_index = False
# given the field_name, find the object it references
# and the argument it came from
obj, arg_used = self.get_field(field_name, args, kwargs)
...
...
@@ -195,7 +213,8 @@ class Formatter:
# expand the format spec, if needed
format_spec = self._vformat(format_spec, args, kwargs,
used_args, recursion_depth-1)
used_args, recursion_depth-1,
auto_arg_index=auto_arg_index)
# format the object and append to the result
result.append(self.format_field(obj, format_spec))
...
...
Lib/test/test_string.py
View file @
6b3e87d3
...
...
@@ -32,6 +32,23 @@ class ModuleTest(unittest.TestCase):
self
.
assertEqual
(
fmt
.
format
(
"foo{0}"
,
"bar"
),
"foobar"
)
self
.
assertEqual
(
fmt
.
format
(
"foo{1}{0}-{1}"
,
"bar"
,
6
),
"foo6bar-6"
)
def
test_auto_numbering
(
self
):
fmt
=
string
.
Formatter
()
self
.
assertEqual
(
fmt
.
format
(
'foo{}{}'
,
'bar'
,
6
),
'foo{}{}'
.
format
(
'bar'
,
6
))
self
.
assertEqual
(
fmt
.
format
(
'foo{1}{num}{1}'
,
None
,
'bar'
,
num
=
6
),
'foo{1}{num}{1}'
.
format
(
None
,
'bar'
,
num
=
6
))
self
.
assertEqual
(
fmt
.
format
(
'{:^{}}'
,
'bar'
,
6
),
'{:^{}}'
.
format
(
'bar'
,
6
))
self
.
assertEqual
(
fmt
.
format
(
'{:^{pad}}{}'
,
'foo'
,
'bar'
,
pad
=
6
),
'{:^{pad}}{}'
.
format
(
'foo'
,
'bar'
,
pad
=
6
))
with
self
.
assertRaises
(
ValueError
):
fmt
.
format
(
'foo{1}{}'
,
'bar'
,
6
)
with
self
.
assertRaises
(
ValueError
):
fmt
.
format
(
'foo{}{1}'
,
'bar'
,
6
)
def
test_conversion_specifiers
(
self
):
fmt
=
string
.
Formatter
()
self
.
assertEqual
(
fmt
.
format
(
"-{arg!r}-"
,
arg
=
'test'
),
"-'test'-"
)
...
...
Misc/NEWS
View file @
6b3e87d3
...
...
@@ -30,6 +30,10 @@ Core and Builtins
- Issue #12546: Allow \x00 to be used as a fill character when using str, int,
float, and complex __format__ methods.
- Issue #13598: Modify string.Formatter to support auto-numbering of
replacement fields. It now matches the behavior of str.format() in
this regard.
Library
-------
...
...
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