You've already forked python-uncompyle6
mirror of
https://github.com/rocky/python-uncompyle6.git
synced 2025-08-04 01:09:52 +08:00
Add simgle-mode compilation
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
import pytest
|
import pytest
|
||||||
from uncompyle6 import PYTHON_VERSION, PYTHON3, deparse_code
|
from uncompyle6 import PYTHON_VERSION, PYTHON3, deparse_code
|
||||||
|
|
||||||
@pytest.mark.skip(reason="Reinstate when we have compiilation single")
|
|
||||||
def test_single_mode():
|
def test_single_mode():
|
||||||
single_expressions = (
|
single_expressions = (
|
||||||
'i = 1',
|
'i = 1',
|
||||||
|
@@ -69,6 +69,25 @@ class PythonParser(GenericASTBuilder):
|
|||||||
# print >> sys.stderr, 'resolve', str(list)
|
# print >> sys.stderr, 'resolve', str(list)
|
||||||
return GenericASTBuilder.resolve(self, list)
|
return GenericASTBuilder.resolve(self, list)
|
||||||
|
|
||||||
|
##############################################
|
||||||
|
## Common Python 2 and Python 3 grammar rules
|
||||||
|
##############################################
|
||||||
|
def p_start(self, args):
|
||||||
|
'''
|
||||||
|
# The start or goal symbol
|
||||||
|
stmts ::= stmts sstmt
|
||||||
|
stmts ::= sstmt
|
||||||
|
'''
|
||||||
|
|
||||||
|
def p_call_stmt(self, args):
|
||||||
|
'''
|
||||||
|
# eval-mode compilation. Single-mode interactive compilation
|
||||||
|
# adds another rule.
|
||||||
|
call_stmt ::= expr POP_TOP
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def parse(p, tokens, customize):
|
def parse(p, tokens, customize):
|
||||||
p.add_custom_rules(tokens, customize)
|
p.add_custom_rules(tokens, customize)
|
||||||
ast = p.parse(tokens)
|
ast = p.parse(tokens)
|
||||||
@@ -84,12 +103,19 @@ def get_python_parser(version, debug_parser, compile_mode='exec'):
|
|||||||
https://docs.python.org/3.6/library/functions.html#compile for an explanation
|
https://docs.python.org/3.6/library/functions.html#compile for an explanation
|
||||||
of the different modes.
|
of the different modes.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if version < 3.0:
|
if version < 3.0:
|
||||||
import uncompyle6.parsers.parse2 as parse2
|
import uncompyle6.parsers.parse2 as parse2
|
||||||
|
if compile_mode == 'exec':
|
||||||
p = parse2.Python2Parser(debug_parser)
|
p = parse2.Python2Parser(debug_parser)
|
||||||
|
else:
|
||||||
|
p = parse2.Python2ParserSingle(debug_parser)
|
||||||
else:
|
else:
|
||||||
import uncompyle6.parsers.parse3 as parse3
|
import uncompyle6.parsers.parse3 as parse3
|
||||||
|
if compile_mode == 'exec':
|
||||||
p = parse3.Python3Parser(debug_parser)
|
p = parse3.Python3Parser(debug_parser)
|
||||||
|
else:
|
||||||
|
p = parse3.Python3ParserSingle(debug_parser)
|
||||||
p.version = version
|
p.version = version
|
||||||
return p
|
return p
|
||||||
|
|
||||||
|
@@ -209,12 +209,6 @@ class Python2Parser(PythonParser):
|
|||||||
load_attrs ::= load_attrs LOAD_ATTR
|
load_attrs ::= load_attrs LOAD_ATTR
|
||||||
'''
|
'''
|
||||||
|
|
||||||
def p_start(self, args):
|
|
||||||
'''
|
|
||||||
stmts ::= stmts sstmt
|
|
||||||
stmts ::= sstmt
|
|
||||||
'''
|
|
||||||
|
|
||||||
def p_grammar(self, args):
|
def p_grammar(self, args):
|
||||||
'''
|
'''
|
||||||
sstmt ::= stmt
|
sstmt ::= stmt
|
||||||
@@ -716,6 +710,11 @@ class Python2Parser(PythonParser):
|
|||||||
self.addRule(rule, nop_func)
|
self.addRule(rule, nop_func)
|
||||||
|
|
||||||
class Python2ParserSingle(Python2Parser):
|
class Python2ParserSingle(Python2Parser):
|
||||||
# Add:
|
def p_call_stmt(self, args):
|
||||||
# call_stmt ::= expr PRINT_EXPR
|
'''
|
||||||
pass
|
# single-mode compilation. eval-mode interactive compilation
|
||||||
|
# drops the last rule.
|
||||||
|
|
||||||
|
call_stmt ::= expr POP_TOP
|
||||||
|
call_stmt ::= expr PRINT_EXPR
|
||||||
|
'''
|
||||||
|
@@ -228,8 +228,7 @@ class Python3Parser(PythonParser):
|
|||||||
'''
|
'''
|
||||||
|
|
||||||
def p_grammar(self, args):
|
def p_grammar(self, args):
|
||||||
'''stmts ::= stmts sstmt
|
'''
|
||||||
stmts ::= sstmt
|
|
||||||
sstmt ::= stmt
|
sstmt ::= stmt
|
||||||
sstmt ::= ifelsestmtr
|
sstmt ::= ifelsestmtr
|
||||||
sstmt ::= return_stmt RETURN_LAST
|
sstmt ::= return_stmt RETURN_LAST
|
||||||
@@ -301,7 +300,6 @@ class Python3Parser(PythonParser):
|
|||||||
|
|
||||||
stmt ::= classdef
|
stmt ::= classdef
|
||||||
stmt ::= call_stmt
|
stmt ::= call_stmt
|
||||||
call_stmt ::= expr POP_TOP
|
|
||||||
|
|
||||||
stmt ::= return_stmt
|
stmt ::= return_stmt
|
||||||
return_stmt ::= ret_expr RETURN_VALUE
|
return_stmt ::= ret_expr RETURN_VALUE
|
||||||
@@ -820,6 +818,11 @@ class Python3Parser(PythonParser):
|
|||||||
return
|
return
|
||||||
|
|
||||||
class Python3ParserSingle(Python3Parser):
|
class Python3ParserSingle(Python3Parser):
|
||||||
# Add:
|
def p_call_stmt(self, args):
|
||||||
# call_stmt ::= expr PRINT_EXPR
|
'''
|
||||||
pass
|
# single-mode compilation. Eval-mode interactive compilation
|
||||||
|
# drops the last rule.
|
||||||
|
|
||||||
|
call_stmt ::= expr POP_TOP
|
||||||
|
call_stmt ::= expr PRINT_EXPR
|
||||||
|
'''
|
||||||
|
Reference in New Issue
Block a user