1- # Copyright (c) 2019-2024 Rocky Bernstein
1+ # Copyright (c) 2019-2025 Rocky Bernstein
22#
33# This program is free software: you can redistribute it and/or modify
44# it under the terms of the GNU General Public License as published by
2525
2626from spark_parser import DEFAULT_DEBUG as PARSER_DEFAULT_DEBUG
2727from xdis import iscode
28- from xdis .version_info import IS_PYPY , PYTHON_VERSION_TRIPLE , version_tuple_to_str
28+ from xdis .version_info import (
29+ PYTHON_IMPLEMENTATION ,
30+ PYTHON_VERSION_TRIPLE ,
31+ PythonImplementation ,
32+ version_tuple_to_str ,
33+ )
2934
3035from decompyle3 .parsers .p37 .heads import (
3136 Python37ParserEval ,
@@ -63,7 +68,10 @@ def parse(p, tokens, customize, is_lambda: bool) -> SyntaxTree:
6368
6469
6570def get_python_parser (
66- version , debug_parser = PARSER_DEFAULT_DEBUG , compile_mode = "exec" , is_pypy = False
71+ version ,
72+ debug_parser = PARSER_DEFAULT_DEBUG ,
73+ compile_mode = "exec" ,
74+ python_implementation = PythonImplementation .CPython ,
6775):
6876 """
6977 Returns parser object for Python version 3.7, 3.8, etc. depending on the parameters
@@ -105,32 +113,32 @@ def get_python_parser(
105113 p = Python37ParserSingle (debug_parser )
106114 elif version == (3 , 8 ):
107115 if compile_mode == "exec" :
108- if is_pypy :
116+ if python_implementation is PythonImplementation . PyPy :
109117 p = Python38PyPyParserExec (debug_parser = debug_parser )
110118 else :
111119 p = Python38ParserExec (debug_parser = debug_parser )
112120
113121 elif compile_mode == "single" :
114- if is_pypy :
122+ if python_implementation is PythonImplementation . PyPy :
115123 p = Python38PyPyParserSingle (debug_parser = debug_parser )
116124 else :
117125 p = Python38ParserSingle (debug_parser = debug_parser )
118126 elif compile_mode == "lambda" :
119- if is_pypy :
127+ if python_implementation is PythonImplementation . PyPy :
120128 p = Python38PyPyParserLambda (debug_parser = debug_parser )
121129 else :
122130 p = Python38ParserLambda (debug_parser = debug_parser )
123131 elif compile_mode == "eval" :
124- if is_pypy :
132+ if python_implementation is PythonImplementation . PyPy :
125133 p = Python38PyPyParserEval (debug_parser = debug_parser )
126134 else :
127135 p = Python38ParserEval (debug_parser = debug_parser )
128136 elif compile_mode == "expr" :
129- if is_pypy :
137+ if python_implementation is PythonImplementation . PyPy :
130138 p = Python38PyPyParserExpr (debug_parser = debug_parser )
131139 else :
132140 p = Python38ParserExpr (debug_parser = debug_parser )
133- elif is_pypy :
141+ elif python_implementation is PythonImplementation . PyPy :
134142 p = Python38PyPyParserSingle (debug_parser )
135143 else :
136144 p = Python38ParserSingle (debug_parser )
@@ -152,7 +160,7 @@ def python_parser(
152160 showasm : bool = False ,
153161 parser_debug = PARSER_DEFAULT_DEBUG ,
154162 compile_mode : str = "exec" ,
155- is_pypy : bool = False ,
163+ python_implementation : PythonImplementation = PythonImplementation . CPython ,
156164 is_lambda : bool = False ,
157165) -> SyntaxTree :
158166 """
@@ -176,15 +184,18 @@ def python_parser(
176184 assert iscode (co )
177185 from decompyle3 .scanner import get_scanner
178186
179- scanner = get_scanner (version , is_pypy )
187+ scanner = get_scanner (version , python_implementation )
180188 tokens , customize = scanner .ingest (co )
181189 maybe_show_asm (showasm , tokens )
182190
183191 # For heavy grammar debugging
184192 # parser_debug = {'rules': True, 'transition': True, 'reduce' : True,
185193 # 'showstack': 'full'}
186194 p = get_python_parser (
187- version , parser_debug , compile_mode = compile_mode , is_pypy = IS_PYPY
195+ version ,
196+ parser_debug ,
197+ compile_mode = compile_mode ,
198+ python_implementation = PYTHON_IMPLEMENTATION ,
188199 )
189200
190201 # FIXME: have p.insts update in a better way
@@ -199,8 +210,12 @@ def python_parser(
199210if __name__ == "__main__" :
200211
201212 def parse_test (co ) -> None :
202-
203- tree = python_parser (co , (3 , 8 , 2 ), showasm = True , is_pypy = IS_PYPY )
213+ tree = python_parser (
214+ co ,
215+ (3 , 8 , 2 ),
216+ showasm = True ,
217+ python_implementation = PythonImplementation .CPython ,
218+ )
204219 print (tree )
205220 print ("+" * 30 )
206221 return
0 commit comments