Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
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
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cython
Commits
dbdd8b14
Commit
dbdd8b14
authored
Aug 21, 2016
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix f-string formatting of '0'
parent
74335c2b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
44 additions
and
6 deletions
+44
-6
Cython/Utility/TypeConversion.c
Cython/Utility/TypeConversion.c
+6
-6
tests/run/fstring.pyx
tests/run/fstring.pyx
+38
-0
No files found.
Cython/Utility/TypeConversion.c
View file @
dbdd8b14
...
...
@@ -634,12 +634,6 @@ static CYTHON_INLINE PyObject* {{TO_PY_FUNCTION}}({{TYPE}} value, Py_ssize_t wid
format_char
=
'x'
;
};
// single character unicode strings are cached in CPython => use PyUnicode_FromOrdinal() for them
if
(
unlikely
((
is_unsigned
||
value
>=
const_zero
)
&&
(
width
<=
1
)
&&
(
(
format_char
==
'o'
)
?
value
<=
7
:
(
format_char
==
'd'
)
?
value
<=
9
:
value
<=
15
)))
{
return
PyUnicode_FromOrdinal
(
hex_digits
[((
int
)
value
)]);
}
// surprise: even trivial sprintf() calls don't get optimised in gcc (4.8)
remaining
=
value
;
/* not using abs(value) to avoid overflow problems */
last_one_off
=
0
;
...
...
@@ -673,6 +667,8 @@ static CYTHON_INLINE PyObject* {{TO_PY_FUNCTION}}({{TYPE}} value, Py_ssize_t wid
if
(
last_one_off
)
{
assert
(
*
dpos
==
'0'
);
dpos
++
;
}
else
if
(
unlikely
(
dpos
==
end
))
{
*
(
--
dpos
)
=
'0'
;
}
length
=
end
-
dpos
;
ulength
=
length
;
...
...
@@ -689,6 +685,10 @@ static CYTHON_INLINE PyObject* {{TO_PY_FUNCTION}}({{TYPE}} value, Py_ssize_t wid
if
(
width
>
ulength
)
{
ulength
=
width
;
}
// single character unicode strings are cached in CPython => use PyUnicode_FromOrdinal() for them
if
(
ulength
==
1
)
{
return
PyUnicode_FromOrdinal
(
*
dpos
);
}
return
__Pyx_PyUnicode_BuildFromAscii
(
ulength
,
dpos
,
length
,
prepend_sign
,
padding_char
);
}
...
...
tests/run/fstring.pyx
View file @
dbdd8b14
...
...
@@ -67,6 +67,16 @@ def format_c_numbers(signed char c, short s, int n, long l, float f, double d):
>>> print(s4)
-C-14-3.14
>>> s1, s2, s3, s4 = format_c_numbers(0, 0, 0, 0, -2.3456, -0.1415926)
>>> print(s1)
0 000-2.35
>>> print(s2)
-0.142-2.3
>>> print(s3)
0f
>>> print(s4)
00000-0.142
"""
s1
=
f"
{
c
}{
s
:
4
}{
l
}{
n
}{
f
:.
3
}
"
assert
isinstance
(
s1
,
unicode
),
type
(
s1
)
...
...
@@ -102,6 +112,34 @@ def format_c_numbers_max(int n, long l):
return
s1
,
s2
def
format_c_number_range
(
int
n
):
"""
>>> for i in range(-1000, 1000):
... assert format_c_number_range(i) == str(i)
"""
return
f'
{
n
}
'
def
format_c_number_range_width
(
int
n
):
"""
>>> for i in range(-1000, 1000):
... assert format_c_number_range_width(i) == '%04d' % i, format_c_number_range_width(i)
"""
return
f'
{
n
:
04
}
'
def
format_c_number_range_dyn_width
(
int
n
,
int
width
):
"""
>>> for i in range(-1000, 1000):
... assert format_c_number_range_dyn_width(i, 0) == str(i), format_c_number_range_dyn_width(i, 0)
... assert format_c_number_range_dyn_width(i, 1) == '%01d' % i, format_c_number_range_dyn_width(i, 1)
... assert format_c_number_range_dyn_width(i, 4) == '%04d' % i, format_c_number_range_dyn_width(i, 4)
... assert format_c_number_range_dyn_width(i, 5) == '%05d' % i, format_c_number_range_dyn_width(i, 5)
... assert format_c_number_range_dyn_width(i, 6) == '%06d' % i, format_c_number_range_dyn_width(i, 6)
"""
return
f'
{
n
:
0
{
width
}}
'
def
format_bool
(
bint
x
):
"""
>>> a, b, c, d = format_bool(1)
...
...
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