Commit 977c3326 authored by Stefan Behnel's avatar Stefan Behnel

Include '%a' in the string formatting types that are optimised into f-strings.

parent bc96d01c
...@@ -55,6 +55,9 @@ Features added ...@@ -55,6 +55,9 @@ Features added
* Declarations for the ``pylifecycle`` C-API functions were added in a new .pxd file * Declarations for the ``pylifecycle`` C-API functions were added in a new .pxd file
``cpython.pylifecycle``. ``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 * New C macro ``CYTHON_HEX_VERSION`` to access Cython's version in the same style as
``PY_HEX_VERSION``. ``PY_HEX_VERSION``.
......
...@@ -4311,16 +4311,16 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations): ...@@ -4311,16 +4311,16 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
warning(pos, "Too few arguments for format placeholders", level=1) warning(pos, "Too few arguments for format placeholders", level=1)
can_be_optimised = False can_be_optimised = False
break break
if format_type in u'srfdoxX': if format_type in u'asrfdoxX':
format_spec = s[1:] format_spec = s[1:]
if format_type in u'doxX' and u'.' in format_spec: if format_type in u'doxX' and u'.' in format_spec:
# Precision is not allowed for integers in format(), but ok in %-formatting. # Precision is not allowed for integers in format(), but ok in %-formatting.
can_be_optimised = False can_be_optimised = False
elif format_type in u'rs': elif format_type in u'ars':
format_spec = format_spec[:-1] format_spec = format_spec[:-1]
substrings.append(ExprNodes.FormattedValueNode( substrings.append(ExprNodes.FormattedValueNode(
arg.pos, value=arg, 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'ars' else None,
format_spec=ExprNodes.UnicodeNode( format_spec=ExprNodes.UnicodeNode(
pos, value=EncodedString(format_spec), constant_result=format_spec) pos, value=EncodedString(format_spec), constant_result=format_spec)
if format_spec else None, if format_spec else None,
......
# 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)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment