Wacky string at beginning of fn which is not docstring...

3.7.6 test_fstring.py tests this.
This commit is contained in:
rocky
2020-01-12 22:59:06 -05:00
parent d8990c89ae
commit f56ad56021
6 changed files with 38 additions and 0 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -67,3 +67,16 @@ def _repr_fn(fields):
fields = ['a', 'b', 'c']
assert _repr_fn(fields) == ['return xx + f"(a={self.a!r}, b={self.b!r}, c={self.c!r})"']
# From Python 3.7 test_fstring. Why this kind of thing matter seems a bit
# academic, but decompile an equivalent thing. For compatiblity with older
# Python we'll use "%" instead of a format string
def f():
f'''Not a docstring'''
def g():
'''Not a docstring''' \
f''
assert f.__doc__ is None
assert g.__doc__ is None

View File

@@ -353,6 +353,13 @@ TABLE_DIRECT = {
'print_nl_to': ( '%|print >> %c\n', 0 ),
'print_to_items': ( '%C', (0, 2, ', ') ),
# This is only generated by transform
# it is a string at the beginning of a function that is *not* a docstring
# 3.7 test_fstring.py tests for this kind of crap.
# For compatibility with older Python, we'll use "%" instead of
# a format string.
"string_at_beginning": ( '%|"%%s" %% %c\n', 0),
'call_stmt': ( '%|%p\n', (0, 200)),
'break': ( '%|break\n', ),
'continue': ( '%|continue\n', ),

View File

@@ -30,6 +30,13 @@ def is_docstring(node):
except:
return False
def is_not_docstring(call_stmt_node):
try:
return (call_stmt_node == "call_stmt" and
call_stmt_node[0][0] == "LOAD_STR" and
call_stmt_node[1] == "POP_TOP")
except:
return False
class TreeTransform(GenericASTTraversal, object):
def __init__(self, version, show_ast=None, is_pypy=False):
@@ -352,6 +359,17 @@ class TreeTransform(GenericASTTraversal, object):
self.ast = copy(ast)
self.ast = self.traverse(self.ast, is_lambda=False)
try:
# Disambiguate a string (expression) which appears as a "call_stmt" at
# the beginning of a function versus a docstring. Seems pretty academic,
# but this is Python.
call_stmt = ast[0][0][0]
if is_not_docstring(call_stmt):
call_stmt.kind = "string_at_beginning"
call_stmt.transformed_by = "transform"
pass
except:
pass
try:
for i in range(len(self.ast)):
if is_docstring(self.ast[i]):