Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.

Commit 680963e

Browse files
author
Juanjo Alvarez
committed
Correctly handle python2 kwargs
Signed-off-by: Juanjo Alvarez <[email protected]> Fix code indent Signed-off-by: Juanjo Alvarez <[email protected]>
1 parent fe965ac commit 680963e

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

native/python_package/python_driver/astimprove.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ def name2arg(node: Node):
218218

219219
kwarg = deepcopy(node.get("kwarg"))
220220
if kwarg:
221+
if isinstance(kwarg, str):
222+
# Python2 kwargs are just strings; convert to same format
223+
# as Python3
224+
kwarg = {"arg": kwarg, "annotation": None}
221225
kwarg["ast_type"] = "kwarg"
222226
norm_args.append(self.visit(kwarg))
223227

@@ -264,13 +268,39 @@ def visit_other_field(self, node: Node) -> VisitResult:
264268
from pprint import pprint
265269

266270
if len(sys.argv) > 1:
267-
from pydetector.ast2dict import ast2dict
271+
from pydetector import detector
268272
codestr = open(sys.argv[1]).read()
269-
testdict = ast2dict(codestr)
273+
resdict = detector.detect(codestr=codestr, stop_on_ok_ast=True)
274+
codeinfo = resdict['<code_string>']
275+
version = codeinfo['version']
276+
277+
failed = False
278+
279+
if version in (3, 6) and codeinfo['py3ast']:
280+
testdict = codeinfo['py3ast']["PY3AST"]
281+
print("Using Python3")
282+
elif version in (1, 2) and codeinfo['py2ast']:
283+
testdict = codeinfo['py2ast']["PY2AST"]
284+
print("Using Python2")
285+
else:
286+
failed = True
287+
errors = [
288+
'Errors produced trying to get an AST for both Python versions' +
289+
'\n------ Python2 errors:\n%s' % codeinfo['py2_ast_errors'] +
290+
'\n------ Python3 errors:\n%s' % codeinfo['py3_ast_errors']
291+
]
292+
293+
if not failed and not testdict:
294+
raise Exception('Empty AST generated from non empty code')
295+
ast = AstImprover(codestr, testdict).parse()
296+
if not ast:
297+
raise Exception('Empty AST generated from non empty code')
270298
else:
271299
codestr = open("../test/fixtures/detector.py").read()
272-
spec = importlib.util.spec_from_file_location("module.testmod",
273-
"../test/fixtures/exported_dict.py")
300+
spec = importlib.util.spec_from_file_location(
301+
"module.testmod",
302+
"../test/fixtures/exported_dict.py"
303+
)
274304
testmod = importlib.util.module_from_spec(spec)
275305

276306
if spec.loader:

0 commit comments

Comments
 (0)