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
2ea9712e
Commit
2ea9712e
authored
Apr 14, 2014
by
Eric V. Smith
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #12546: Allow \x00 as a fill character for builtin type __format__ methods.
parent
70d92a96
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
32 additions
and
11 deletions
+32
-11
Lib/test/test_unicode.py
Lib/test/test_unicode.py
+21
-0
Misc/NEWS
Misc/NEWS
+3
-0
Python/formatter_unicode.c
Python/formatter_unicode.c
+8
-11
No files found.
Lib/test/test_unicode.py
View file @
2ea9712e
...
@@ -845,6 +845,27 @@ class UnicodeTest(string_tests.CommonTest,
...
@@ -845,6 +845,27 @@ class UnicodeTest(string_tests.CommonTest,
self
.
assertEqual
(
'{0:10000}'
.
format
(
''
),
' '
*
10000
)
self
.
assertEqual
(
'{0:10000}'
.
format
(
''
),
' '
*
10000
)
self
.
assertEqual
(
'{0:10000000}'
.
format
(
''
),
' '
*
10000000
)
self
.
assertEqual
(
'{0:10000000}'
.
format
(
''
),
' '
*
10000000
)
# issue 12546: use \x00 as a fill character
self
.
assertEqual
(
'{0:
\
x00
<6s}'
.
format
(
'foo'
),
'foo
\
x00
\
x00
\
x00
'
)
self
.
assertEqual
(
'{0:
\
x01
<6s}'
.
format
(
'foo'
),
'foo
\
x01
\
x01
\
x01
'
)
self
.
assertEqual
(
'{0:
\
x00
^6s}'
.
format
(
'foo'
),
'
\
x00
foo
\
x00
\
x00
'
)
self
.
assertEqual
(
'{0:^6s}'
.
format
(
'foo'
),
' foo '
)
self
.
assertEqual
(
'{0:
\
x00
<6}'
.
format
(
3
),
'3
\
x00
\
x00
\
x00
\
x00
\
x00
'
)
self
.
assertEqual
(
'{0:
\
x01
<6}'
.
format
(
3
),
'3
\
x01
\
x01
\
x01
\
x01
\
x01
'
)
self
.
assertEqual
(
'{0:
\
x00
^6}'
.
format
(
3
),
'
\
x00
\
x00
3
\
x00
\
x00
\
x00
'
)
self
.
assertEqual
(
'{0:<6}'
.
format
(
3
),
'3 '
)
self
.
assertEqual
(
'{0:
\
x00
<6}'
.
format
(
3.14
),
'3.14
\
x00
\
x00
'
)
self
.
assertEqual
(
'{0:
\
x01
<6}'
.
format
(
3.14
),
'3.14
\
x01
\
x01
'
)
self
.
assertEqual
(
'{0:
\
x00
^6}'
.
format
(
3.14
),
'
\
x00
3.14
\
x00
'
)
self
.
assertEqual
(
'{0:^6}'
.
format
(
3.14
),
' 3.14 '
)
self
.
assertEqual
(
'{0:
\
x00
<12}'
.
format
(
3
+
2.0j
),
'(3+2j)
\
x00
\
x00
\
x00
\
x00
\
x00
\
x00
'
)
self
.
assertEqual
(
'{0:
\
x01
<12}'
.
format
(
3
+
2.0j
),
'(3+2j)
\
x01
\
x01
\
x01
\
x01
\
x01
\
x01
'
)
self
.
assertEqual
(
'{0:
\
x00
^12}'
.
format
(
3
+
2.0j
),
'
\
x00
\
x00
\
x00
(3+2j)
\
x00
\
x00
\
x00
'
)
self
.
assertEqual
(
'{0:^12}'
.
format
(
3
+
2.0j
),
' (3+2j) '
)
# format specifiers for user defined type
# format specifiers for user defined type
self
.
assertEqual
(
'{0:abc}'
.
format
(
C
()),
'abc'
)
self
.
assertEqual
(
'{0:abc}'
.
format
(
C
()),
'abc'
)
...
...
Misc/NEWS
View file @
2ea9712e
...
@@ -27,6 +27,9 @@ Core and Builtins
...
@@ -27,6 +27,9 @@ Core and Builtins
- Issue #20637: Key-sharing now also works for instance dictionaries of
- Issue #20637: Key-sharing now also works for instance dictionaries of
subclasses. Patch by Peter Ingebretson.
subclasses. Patch by Peter Ingebretson.
- Issue #12546: Allow \x00 to be used as a fill character when using str, int,
float, and complex __format__ methods.
Library
Library
-------
-------
...
...
Python/formatter_unicode.c
View file @
2ea9712e
...
@@ -156,8 +156,9 @@ parse_internal_render_format_spec(PyObject *format_spec,
...
@@ -156,8 +156,9 @@ parse_internal_render_format_spec(PyObject *format_spec,
Py_ssize_t
consumed
;
Py_ssize_t
consumed
;
int
align_specified
=
0
;
int
align_specified
=
0
;
int
fill_char_specified
=
0
;
format
->
fill_char
=
'
\0
'
;
format
->
fill_char
=
'
'
;
format
->
align
=
default_align
;
format
->
align
=
default_align
;
format
->
alternate
=
0
;
format
->
alternate
=
0
;
format
->
sign
=
'\0'
;
format
->
sign
=
'\0'
;
...
@@ -171,6 +172,7 @@ parse_internal_render_format_spec(PyObject *format_spec,
...
@@ -171,6 +172,7 @@ parse_internal_render_format_spec(PyObject *format_spec,
if
(
end
-
pos
>=
2
&&
is_alignment_token
(
READ_spec
(
pos
+
1
)))
{
if
(
end
-
pos
>=
2
&&
is_alignment_token
(
READ_spec
(
pos
+
1
)))
{
format
->
align
=
READ_spec
(
pos
+
1
);
format
->
align
=
READ_spec
(
pos
+
1
);
format
->
fill_char
=
READ_spec
(
pos
);
format
->
fill_char
=
READ_spec
(
pos
);
fill_char_specified
=
1
;
align_specified
=
1
;
align_specified
=
1
;
pos
+=
2
;
pos
+=
2
;
}
}
...
@@ -194,7 +196,7 @@ parse_internal_render_format_spec(PyObject *format_spec,
...
@@ -194,7 +196,7 @@ parse_internal_render_format_spec(PyObject *format_spec,
}
}
/* The special case for 0-padding (backwards compat) */
/* The special case for 0-padding (backwards compat) */
if
(
format
->
fill_char
==
'\0'
&&
end
-
pos
>=
1
&&
READ_spec
(
pos
)
==
'0'
)
{
if
(
!
fill_char_specified
&&
end
-
pos
>=
1
&&
READ_spec
(
pos
)
==
'0'
)
{
format
->
fill_char
=
'0'
;
format
->
fill_char
=
'0'
;
if
(
!
align_specified
)
{
if
(
!
align_specified
)
{
format
->
align
=
'='
;
format
->
align
=
'='
;
...
@@ -784,9 +786,7 @@ format_string_internal(PyObject *value, const InternalFormatSpec *format,
...
@@ -784,9 +786,7 @@ format_string_internal(PyObject *value, const InternalFormatSpec *format,
goto
done
;
goto
done
;
/* Write into that space. First the padding. */
/* Write into that space. First the padding. */
result
=
fill_padding
(
writer
,
len
,
result
=
fill_padding
(
writer
,
len
,
format
->
fill_char
,
lpad
,
rpad
);
format
->
fill_char
==
'\0'
?
' '
:
format
->
fill_char
,
lpad
,
rpad
);
if
(
result
==
-
1
)
if
(
result
==
-
1
)
goto
done
;
goto
done
;
...
@@ -956,8 +956,7 @@ format_long_internal(PyObject *value, const InternalFormatSpec *format,
...
@@ -956,8 +956,7 @@ format_long_internal(PyObject *value, const InternalFormatSpec *format,
/* Populate the memory. */
/* Populate the memory. */
result
=
fill_number
(
writer
,
&
spec
,
result
=
fill_number
(
writer
,
&
spec
,
tmp
,
inumeric_chars
,
inumeric_chars
+
n_digits
,
tmp
,
inumeric_chars
,
inumeric_chars
+
n_digits
,
tmp
,
prefix
,
tmp
,
prefix
,
format
->
fill_char
,
format
->
fill_char
==
'\0'
?
' '
:
format
->
fill_char
,
&
locale
,
format
->
type
==
'X'
);
&
locale
,
format
->
type
==
'X'
);
done:
done:
...
@@ -1104,8 +1103,7 @@ format_float_internal(PyObject *value,
...
@@ -1104,8 +1103,7 @@ format_float_internal(PyObject *value,
/* Populate the memory. */
/* Populate the memory. */
result
=
fill_number
(
writer
,
&
spec
,
result
=
fill_number
(
writer
,
&
spec
,
unicode_tmp
,
index
,
index
+
n_digits
,
unicode_tmp
,
index
,
index
+
n_digits
,
NULL
,
0
,
NULL
,
0
,
format
->
fill_char
,
format
->
fill_char
==
'\0'
?
' '
:
format
->
fill_char
,
&
locale
,
0
);
&
locale
,
0
);
done:
done:
...
@@ -1311,8 +1309,7 @@ format_complex_internal(PyObject *value,
...
@@ -1311,8 +1309,7 @@ format_complex_internal(PyObject *value,
/* Populate the memory. First, the padding. */
/* Populate the memory. First, the padding. */
result
=
fill_padding
(
writer
,
result
=
fill_padding
(
writer
,
n_re_total
+
n_im_total
+
1
+
add_parens
*
2
,
n_re_total
+
n_im_total
+
1
+
add_parens
*
2
,
format
->
fill_char
==
'\0'
?
' '
:
format
->
fill_char
,
format
->
fill_char
,
lpad
,
rpad
);
lpad
,
rpad
);
if
(
result
==
-
1
)
if
(
result
==
-
1
)
goto
done
;
goto
done
;
...
...
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