More Python3 deparsing

- grammar rule genexpr
- More Python3 docstring formatted
This commit is contained in:
rocky
2016-05-06 23:51:25 -04:00
parent 15b2a742e9
commit 039c115679
5 changed files with 43 additions and 11 deletions

Binary file not shown.

View File

@@ -0,0 +1,7 @@
# Tests in Python 3:
#
# load_genexpr ::= BUILD_TUPLE_1 LOAD_GENEXPR LOAD_CONST
# genexpr ::= load_closure load_genexpr MAKE_CLOSURE_0 expr GET_ITER CALL_FUNCTION_1
def __instancecheck__(cls, instance):
return any(cls.__subclasscheck__(c) for c in {subclass, subtype})

View File

@@ -1,7 +1,7 @@
# Copyright (c) 1999 John Aycock
# Copyright (c) 2000-2002 by hartmut Goebel <h.goebel@crazy-compilers.com>
# Copyright (c) 2005 by Dan Pascu <dan@windowmaker.org>
# Copyright (c) 2015 Rocky Bernstein
# Copyright (c) 2015, 2016 Rocky Bernstein
#
# See LICENSE for license
"""
@@ -368,9 +368,13 @@ class Python3Parser(PythonParser):
forelselaststmtl ::= SETUP_LOOP expr _for designator
for_block POP_BLOCK else_suitel COME_FROM
'''
def p_genexpr3(self, args):
'''
load_genexpr ::= LOAD_GENEXPR
load_genexpr ::= BUILD_TUPLE_1 LOAD_GENEXPR LOAD_CONST
'''
def p_expr3(self, args):
'''
expr ::= LOAD_CLASSNAME
@@ -506,7 +510,8 @@ class Python3Parser(PythonParser):
self.add_unique_rule('mklambda ::= %s load_closure LOAD_LAMBDA %s' %
('expr ' * token.attr, opname), opname, token.attr,
customize)
self.add_unique_rule('genexpr ::= %s load_closure LOAD_GENEXPR %s '
self.add_unique_rule('genexpr ::= %s load_closure '
'load_genexpr %s '
'expr GET_ITER CALL_FUNCTION_1' %
('expr ' * token.attr, opname),
opname, token.attr, customize)

View File

@@ -478,12 +478,17 @@ class FragmentsWalker(pysource.SourceWalker, object):
self.indentLess()
self.prune() # stop recursing
def comprehension_walk(self, node, iter_index):
def comprehension_walk(self, node, iter_index, code_index=-5):
p = self.prec
self.prec = 27
code = node[-5].attr
if hasattr(node[code_index], 'attr'):
code = node[code_index].attr
elif hasattr(node[1][1], 'attr'):
code = node[1][1].attr
else:
assert False
assert iscode(co)
assert iscode(code)
code = Code(code, self.scanner, self.currentclass)
# assert isinstance(code, Code)

View File

@@ -436,6 +436,13 @@ escape = re.compile(r'''
( [{] (?P<expr> [^}]* ) [}] ))
''', re.VERBOSE)
def is_docstring(node):
try:
return (node[0][0].type == 'assign' and
node[0][0][1][0].pattr == '__doc__')
except:
return False
class ParserError(python_parser.ParserError):
def __init__(self, error, tokens):
self.error = error # previous exception
@@ -972,7 +979,12 @@ class SourceWalker(GenericASTTraversal, object):
def comprehension_walk(self, node, iter_index, code_index=-5):
p = self.prec
self.prec = 27
code = node[code_index].attr
if hasattr(node[code_index], 'attr'):
code = node[code_index].attr
elif hasattr(node[1][1], 'attr'):
code = node[1][1].attr
else:
assert False
assert iscode(code)
code = Code(code, self.scanner, self.currentclass)
@@ -1587,15 +1599,18 @@ class SourceWalker(GenericASTTraversal, object):
# if docstring exists, dump it
if (code.co_consts and code.co_consts[0] is not None and len(ast) > 0):
do_doc = False
if (ast[0][0] == ASSIGN_DOC_STRING(code.co_consts[0])):
if is_docstring(ast[0]):
i = 0
do_doc = True
elif (len(ast) > 1 and 3.0 <= self.version <= 3.2 and
ast[1][0] == ASSIGN_DOC_STRING(code.co_consts[0])):
elif (len(ast) > 1 and is_docstring(ast[1])):
i = 1
do_doc = True
if do_doc and self.hide_internal:
self.print_docstring(indent, code.co_consts[0])
try:
docstring = ast[i][0][0][0][0].pattr
except:
docstring = code.co_consts[0]
self.print_docstring(indent, docstring)
self.print_()
del ast[i]