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
185e30cd
Commit
185e30cd
authored
Aug 30, 2007
by
Eric Smith
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added format tests.
Fixed bug in alignment of negative numbers. Whitespace normalization.
parent
bc3b345f
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
36 additions
and
24 deletions
+36
-24
Lib/test/test_builtin.py
Lib/test/test_builtin.py
+1
-0
Lib/test/test_long.py
Lib/test/test_long.py
+13
-2
Lib/test/test_unicode.py
Lib/test/test_unicode.py
+8
-8
Objects/stringlib/formatter.h
Objects/stringlib/formatter.h
+14
-14
No files found.
Lib/test/test_builtin.py
View file @
185e30cd
...
...
@@ -540,6 +540,7 @@ class BuiltinTest(unittest.TestCase):
self
.
assertEqual
(
format
(
3.1415e-104
,
""
),
str
(
3.1415e-104
))
self
.
assertEqual
(
format
(
-
3.1415e-104
,
""
),
str
(
-
3.1415e-104
))
self
.
assertEqual
(
format
(
object
,
""
),
str
(
object
))
self
.
assertEqual
(
format
(
None
,
""
),
str
(
None
))
# TypeError because self.__format__ returns the wrong type
self
.
assertRaises
(
TypeError
,
format
,
H
(),
""
)
...
...
Lib/test/test_long.py
View file @
185e30cd
...
...
@@ -497,13 +497,25 @@ class LongTest(unittest.TestCase):
self
.
assertEqual
(
format
(
123456789
,
'd'
),
'123456789'
)
self
.
assertEqual
(
format
(
123456789
,
'd'
),
'123456789'
)
# sign and aligning are interdependent
self
.
assertEqual
(
format
(
1
,
"-"
),
'1'
)
self
.
assertEqual
(
format
(
-
1
,
"-"
),
'-1'
)
self
.
assertEqual
(
format
(
1
,
"-3"
),
' 1'
)
self
.
assertEqual
(
format
(
-
1
,
"-3"
),
' -1'
)
self
.
assertEqual
(
format
(
1
,
"+3"
),
' +1'
)
self
.
assertEqual
(
format
(
-
1
,
"+3"
),
' -1'
)
self
.
assertEqual
(
format
(
1
,
" 3"
),
' 1'
)
self
.
assertEqual
(
format
(
-
1
,
" 3"
),
' -1'
)
self
.
assertEqual
(
format
(
1
,
" "
),
' 1'
)
self
.
assertEqual
(
format
(
-
1
,
" "
),
'-1'
)
# hex
self
.
assertEqual
(
format
(
3
,
"x"
),
"3"
)
self
.
assertEqual
(
format
(
3
,
"X"
),
"3"
)
self
.
assertEqual
(
format
(
1234
,
"x"
),
"4d2"
)
self
.
assertEqual
(
format
(
-
1234
,
"x"
),
"-4d2"
)
self
.
assertEqual
(
format
(
1234
,
"8x"
),
" 4d2"
)
# XXX fix
self.assertEqual(format(-1234, "8x"), " -4d2")
self
.
assertEqual
(
format
(
-
1234
,
"8x"
),
" -4d2"
)
self
.
assertEqual
(
format
(
1234
,
"x"
),
"4d2"
)
self
.
assertEqual
(
format
(
-
1234
,
"x"
),
"-4d2"
)
self
.
assertEqual
(
format
(
-
3
,
"x"
),
"-3"
)
...
...
@@ -530,7 +542,6 @@ class LongTest(unittest.TestCase):
# make sure these are errors
self
.
assertRaises
(
ValueError
,
format
,
3
,
"1.3"
)
# precision disallowed
return
self
.
assertRaises
(
ValueError
,
format
,
3
,
"+c"
)
# sign not allowed
# with 'c'
self
.
assertRaises
(
ValueError
,
format
,
3
,
"R"
)
# bogus format type
...
...
Lib/test/test_unicode.py
View file @
185e30cd
...
...
@@ -379,8 +379,6 @@ class UnicodeTest(
self
.
assertEqual
(
"The year is {0.year}"
.
format
(
d
),
"The year is 2007"
)
#"{0!r:20}".format("Hello")
# classes we'll use for testing
class
C
:
def
__init__
(
self
,
x
=
100
):
...
...
@@ -428,6 +426,10 @@ class UnicodeTest(
def
__format__
(
self
,
format_spec
):
return
self
.
strftime
(
format_spec
)
class
J
(
int
):
def
__format__
(
self
,
format_spec
):
return
int
.
__format__
(
self
*
2
,
format_spec
)
self
.
assertEqual
(
''
.
format
(),
''
)
self
.
assertEqual
(
'abc'
.
format
(),
'abc'
)
...
...
@@ -465,12 +467,6 @@ class UnicodeTest(
self
.
assertEqual
(
'{0[1][0]}'
.
format
([
'abc'
,
[
'def'
]]),
'def'
)
self
.
assertEqual
(
'{0[1][0].x}'
.
format
([
'abc'
,
[
D
(
'def'
)]]),
'def'
)
# I'm not sure if this should work, or if it's a problem if it does work
#'{0[_{foo}]}'.format({'_FOO': 'abc'}, foo='FOO')
#('{0[{foo}{bar}]}'.format({'FOOBAR': 'abc'}, foo='FOO', bar='BAR')
# format specifiers for built in types
# strings
self
.
assertEqual
(
'{0:.3s}'
.
format
(
'abc'
),
'abc'
)
self
.
assertEqual
(
'{0:.3s}'
.
format
(
'ab'
),
'ab'
)
...
...
@@ -524,6 +520,10 @@ class UnicodeTest(
day
=
27
)),
"date: 2007-08-27"
)
# test deriving from a builtin type and overriding __format__
self
.
assertEqual
(
"{0}"
.
format
(
J
(
10
)),
"20"
)
# string format specifiers
self
.
assertEqual
(
'{0:}'
.
format
(
'a'
),
'a'
)
...
...
Objects/stringlib/formatter.h
View file @
185e30cd
...
...
@@ -145,8 +145,7 @@ parse_internal_render_format_spec(PyObject *format_spec,
}
/* The special case for 0-padding (backwards compat) */
if
(
format
->
fill_char
==
'\0'
&&
end
-
ptr
>=
1
&&
ptr
[
0
]
==
'0'
)
{
if
(
format
->
fill_char
==
'\0'
&&
end
-
ptr
>=
1
&&
ptr
[
0
]
==
'0'
)
{
format
->
fill_char
=
'0'
;
if
(
format
->
align
==
'\0'
)
{
format
->
align
=
'='
;
...
...
@@ -298,9 +297,10 @@ calc_number_widths(NumberFieldWidths *r, STRINGLIB_CHAR actual_sign,
r
->
n_lpadding
=
padding
/
2
;
r
->
n_rpadding
=
padding
-
r
->
n_lpadding
;
}
else
/* must be '=' */
else
if
(
format
->
align
==
'='
)
r
->
n_spadding
=
padding
;
else
r
->
n_lpadding
=
padding
;
}
}
r
->
n_total
=
r
->
n_lpadding
+
r
->
n_lsign
+
r
->
n_spadding
+
...
...
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