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
Gwenaël Samain
cython
Commits
977c3326
Commit
977c3326
authored
Sep 08, 2018
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Include '%a' in the string formatting types that are optimised into f-strings.
parent
bc96d01c
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
42 additions
and
3 deletions
+42
-3
CHANGES.rst
CHANGES.rst
+3
-0
Cython/Compiler/Optimize.py
Cython/Compiler/Optimize.py
+3
-3
tests/run/unicode_formatting.pyx
tests/run/unicode_formatting.pyx
+36
-0
No files found.
CHANGES.rst
View file @
977c3326
...
...
@@ -55,6 +55,9 @@ Features added
* Declarations for the ``pylifecycle`` C-API functions were added in a new .pxd file
``cpython.pylifecycle``.
* ``%a`` is included in the string formatting types that are optimised into f-strings.
In this case, it is also automatically mapped to ``%r`` in Python 2.x.
* New C macro ``CYTHON_HEX_VERSION`` to access Cython'
s
version
in
the
same
style
as
``
PY_HEX_VERSION
``.
...
...
Cython/Compiler/Optimize.py
View file @
977c3326
...
...
@@ -4311,16 +4311,16 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
warning
(
pos
,
"Too few arguments for format placeholders"
,
level
=
1
)
can_be_optimised
=
False
break
if
format_type
in
u'srfdoxX'
:
if
format_type
in
u'
a
srfdoxX'
:
format_spec
=
s
[
1
:]
if
format_type
in
u'doxX'
and
u'.'
in
format_spec
:
# Precision is not allowed for integers in format(), but ok in %-formatting.
can_be_optimised
=
False
elif
format_type
in
u'rs'
:
elif
format_type
in
u'
a
rs'
:
format_spec
=
format_spec
[:
-
1
]
substrings
.
append
(
ExprNodes
.
FormattedValueNode
(
arg
.
pos
,
value
=
arg
,
conversion_char
=
format_type
if
format_type
in
u'rs'
else
None
,
conversion_char
=
format_type
if
format_type
in
u'
a
rs'
else
None
,
format_spec
=
ExprNodes
.
UnicodeNode
(
pos
,
value
=
EncodedString
(
format_spec
),
constant_result
=
format_spec
)
if
format_spec
else
None
,
...
...
tests/run/unicode_formatting.pyx
0 → 100644
View file @
977c3326
# mode: run
# tag: stringformat
from
__future__
import
unicode_literals
def
ascii_format
(
a
,
int
b
,
list
c
):
"""
>>> print(ascii_format('x', 2, [1]))
-'x'-2-[1]-
"""
return
'-%a-%a-%a-'
%
(
a
,
b
,
c
)
def
repr_format
(
a
,
int
b
,
list
c
):
"""
>>> print(repr_format('x', 2, [1]))
-'x'-2-[1]-
"""
return
'-%r-%r-%r-'
%
(
a
,
b
,
c
)
def
str_format
(
a
,
int
b
,
list
c
):
"""
>>> print(str_format('x', 2, [1]))
-x-2-[1]-
"""
return
'-%s-%s-%s-'
%
(
a
,
b
,
c
)
def
mix_format
(
a
,
int
b
,
list
c
):
"""
>>> print(mix_format('x', 2, [1]))
-x-2-[1]-
"""
return
'-%s-%r-%a-'
%
(
a
,
b
,
c
)
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