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
e56bf97e
Commit
e56bf97e
authored
Aug 19, 2012
by
R David Murray
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#13579: teach string.Formatter about 'a'.
Patch by Francisco Martín Brugué.
parent
82860717
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
26 additions
and
9 deletions
+26
-9
Doc/library/string.rst
Doc/library/string.rst
+6
-5
Lib/string.py
Lib/string.py
+6
-4
Lib/test/test_string.py
Lib/test/test_string.py
+12
-0
Misc/NEWS
Misc/NEWS
+2
-0
No files found.
Doc/library/string.rst
View file @
e56bf97e
...
@@ -91,8 +91,8 @@ implementation as the built-in :meth:`format` method.
...
@@ -91,8 +91,8 @@ implementation as the built-in :meth:`format` method.
.. method:: format(format_string, *args, **kwargs)
.. method:: format(format_string, *args, **kwargs)
:meth:`format` is the primary API method. It takes a format
template
:meth:`format` is the primary API method. It takes a format
string and
string, and an arbitrary set of positional and keyword argument
.
an arbitrary set of positional and keyword arguments
.
:meth:`format` is just a wrapper that calls :meth:`vformat`.
:meth:`format` is just a wrapper that calls :meth:`vformat`.
.. method:: vformat(format_string, args, kwargs)
.. method:: vformat(format_string, args, kwargs)
...
@@ -101,8 +101,8 @@ implementation as the built-in :meth:`format` method.
...
@@ -101,8 +101,8 @@ implementation as the built-in :meth:`format` method.
separate function for cases where you want to pass in a predefined
separate function for cases where you want to pass in a predefined
dictionary of arguments, rather than unpacking and repacking the
dictionary of arguments, rather than unpacking and repacking the
dictionary as individual arguments using the ``*args`` and ``**kwds``
dictionary as individual arguments using the ``*args`` and ``**kwds``
syntax. :meth:`vformat` does the work of breaking up the format
template
syntax. :meth:`vformat` does the work of breaking up the format
string
string
into character data and replacement fields. It calls the various
into character data and replacement fields. It calls the various
methods described below.
methods described below.
In addition, the :class:`Formatter` defines a number of methods that are
In addition, the :class:`Formatter` defines a number of methods that are
...
@@ -173,7 +173,8 @@ implementation as the built-in :meth:`format` method.
...
@@ -173,7 +173,8 @@ implementation as the built-in :meth:`format` method.
Converts the value (returned by :meth:`get_field`) given a conversion type
Converts the value (returned by :meth:`get_field`) given a conversion type
(as in the tuple returned by the :meth:`parse` method). The default
(as in the tuple returned by the :meth:`parse` method). The default
version understands 'r' (repr) and 's' (str) conversion types.
version understands 's' (str), 'r' (repr) and 'a' (ascii) conversion
types.
.. _formatstrings:
.. _formatstrings:
...
...
Lib/string.py
View file @
e56bf97e
...
@@ -236,12 +236,14 @@ class Formatter:
...
@@ -236,12 +236,14 @@ class Formatter:
def convert_field(self, value, conversion):
def convert_field(self, value, conversion):
# do any conversion on the resulting object
# do any conversion on the resulting object
if conversion
== 'r'
:
if conversion
is None
:
return
repr(value)
return
value
elif conversion == 's':
elif conversion == 's':
return str(value)
return str(value)
elif conversion is None:
elif conversion == 'r':
return value
return repr(value)
elif conversion == 'a':
return ascii(value)
raise ValueError("Unknown conversion specifier {0!s}".format(conversion))
raise ValueError("Unknown conversion specifier {0!s}".format(conversion))
...
...
Lib/test/test_string.py
View file @
e56bf97e
...
@@ -26,6 +26,18 @@ class ModuleTest(unittest.TestCase):
...
@@ -26,6 +26,18 @@ class ModuleTest(unittest.TestCase):
self
.
assertEqual
(
string
.
capwords
(
'
\
t
aBc
\
t
DeF
\
t
'
),
'Abc Def'
)
self
.
assertEqual
(
string
.
capwords
(
'
\
t
aBc
\
t
DeF
\
t
'
),
'Abc Def'
)
self
.
assertEqual
(
string
.
capwords
(
'
\
t
aBc
\
t
DeF
\
t
'
,
'
\
t
'
),
'
\
t
Abc
\
t
Def
\
t
'
)
self
.
assertEqual
(
string
.
capwords
(
'
\
t
aBc
\
t
DeF
\
t
'
,
'
\
t
'
),
'
\
t
Abc
\
t
Def
\
t
'
)
def
test_conversion_specifiers
(
self
):
fmt
=
string
.
Formatter
()
self
.
assertEqual
(
fmt
.
format
(
"-{arg!r}-"
,
arg
=
'test'
),
"-'test'-"
)
self
.
assertEqual
(
fmt
.
format
(
"{0!s}"
,
'test'
),
'test'
)
self
.
assertRaises
(
ValueError
,
fmt
.
format
,
"{0!h}"
,
'test'
)
# issue13579
self
.
assertEqual
(
fmt
.
format
(
"{0!a}"
,
42
),
'42'
)
self
.
assertEqual
(
fmt
.
format
(
"{0!a}"
,
string
.
ascii_letters
),
"'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'"
)
self
.
assertEqual
(
fmt
.
format
(
"{0!a}"
,
chr
(
255
)),
"'
\
\
xff'"
)
self
.
assertEqual
(
fmt
.
format
(
"{0!a}"
,
chr
(
256
)),
"'
\
\
u0100'"
)
def
test_formatter
(
self
):
def
test_formatter
(
self
):
fmt
=
string
.
Formatter
()
fmt
=
string
.
Formatter
()
self
.
assertEqual
(
fmt
.
format
(
"foo"
),
"foo"
)
self
.
assertEqual
(
fmt
.
format
(
"foo"
),
"foo"
)
...
...
Misc/NEWS
View file @
e56bf97e
...
@@ -104,6 +104,8 @@ Core and Builtins
...
@@ -104,6 +104,8 @@ Core and Builtins
Library
Library
-------
-------
- Issue #13579: string.Formatter now understands the 'a' conversion specifier.
- Issue #15595: Fix subprocess.Popen(universal_newlines=True)
- Issue #15595: Fix subprocess.Popen(universal_newlines=True)
for certain locales (utf-16 and utf-32 family). Patch by Chris Jerdonek.
for certain locales (utf-16 and utf-32 family). Patch by Chris Jerdonek.
...
...
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