Skip to content

Commit 7beaa9f

Browse files
committed
urther work on fstrings for python 3.6 - there is a new opcode build_string which is used to improve fstring performance, but broke the fstring support in uncompyle
1 parent 78ef16e commit 7beaa9f

File tree

4 files changed

+25
-20
lines changed

4 files changed

+25
-20
lines changed

pytest/test_fstring.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ def expressions(draw):
2121
'container',
2222
'self.attribute',
2323
'self.method()',
24-
'sorted(items, key=lambda x: x.name)',
25-
'func(*args, **kwargs)',
26-
'text or default',
24+
#'sorted(items, key=lambda x: x.name)',
25+
#'func(*args, **kwargs)',
26+
#'text or default',
2727
)))
2828

2929

@@ -137,7 +137,7 @@ def test_uncompyle_fstring(fstring):
137137
@pytest.mark.skipif(PYTHON_VERSION < 3.6, reason='need at least python 3.6')
138138
@pytest.mark.parametrize('fstring', [
139139
#"f'{abc}{abc!s}'",
140-
"f'{abc!s}'",
140+
"f'{abc}0'",
141141
])
142142
def test_uncompyle_direct(fstring):
143143
"""useful for debugging"""

uncompyle6/parsers/parse3.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,20 @@ def add_custom_rules(self, tokens, customize):
518518
self.addRule("""
519519
expr ::= fstring_expr
520520
fstring_expr ::= expr FORMAT_VALUE
521+
str ::= LOAD_CONST
522+
fstring_expr_or_str ::= fstring_expr
523+
fstring_expr_or_str ::= str
521524
""", nop_func)
525+
elif opname == 'BUILD_STRING':
526+
# Python 3.6+
527+
v = token.attr
528+
fstring_expr_or_str_n = "fstring_expr_or_str_%s" % v
529+
rule = """
530+
expr ::= fstring
531+
fstring ::= %s BUILD_STRING
532+
%s ::= %sBUILD_STRING
533+
""" % (fstring_expr_or_str_n, fstring_expr_or_str_n, "fstring_expr_or_str " * v)
534+
self.addRule(rule, nop_func)
522535

523536
elif opname in ('CALL_FUNCTION', 'CALL_FUNCTION_VAR',
524537
'CALL_FUNCTION_VAR_KW', 'CALL_FUNCTION_KW'):
@@ -542,14 +555,6 @@ def add_custom_rules(self, tokens, customize):
542555
if opname_base == 'BUILD_TUPLE':
543556
rule = ('load_closure ::= %s%s' % (('LOAD_CLOSURE ' * v), opname))
544557
self.add_unique_rule(rule, opname, token.attr, customize)
545-
if opname_base == 'BUILD_LIST' and saw_format_value:
546-
format_or_str_n = "formatted_value_or_str_%s" % v
547-
self.addRule("""
548-
expr ::= joined_str
549-
joined_str ::= LOAD_CONST LOAD_ATTR %s CALL_FUNCTION_1
550-
%s ::= %s%s
551-
""" % (format_or_str_n, format_or_str_n, ("formatted_value_or_str " *v), opname),
552-
nop_func)
553558

554559
elif opname == 'LOOKUP_METHOD':
555560
# A PyPy speciality - DRY with parse2

uncompyle6/parsers/parse36.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@ def __init__(self, debug_parser=PARSER_DEFAULT_DEBUG):
1616

1717
def p_36misc(self, args):
1818
"""
19-
formatted_value ::= LOAD_FAST FORMAT_VALUE
19+
fstring_expr ::= expr FORMAT_VALUE
2020
str ::= LOAD_CONST
21-
joined_str ::= LOAD_CONST LOAD_ATTR format_value_or_strs
22-
BUILD_LIST CALL_FUNCTION
23-
format_value_or_strs ::= format_value_or_strs format_value_or_str
24-
format_value_or_strs ::= format_value_or_str
25-
format_value_or_str ::= format_value
26-
format_value_or_str ::= str
21+
fstring ::= fstring_expr_or_strs BUILD_STRING
22+
fstring_expr_or_strs ::= fstring_expr_or_strs fstring_expr_or_str
23+
fstring_expr_or_strs ::= fstring_expr_or_str
24+
fstring_expr_or_str ::= fstring_expr
25+
fstring_expr_or_str ::= str
2726
"""
2827

2928
class Python36ParserSingle(Python36Parser, PythonParserSingle):

uncompyle6/semantics/pysource.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,8 @@ def customize_for_version(is_pypy, version):
632632
# Python 3.6+ Additions
633633
#######################
634634
TABLE_DIRECT.update({
635-
'fstring_expr': ( "f'{%c%{conversion}}'", 0),
635+
'fstring_expr': ( "{%c%{conversion}}", 0),
636+
'fstring': ( "f'%c'", 0),
636637
})
637638
return
638639

0 commit comments

Comments
 (0)