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

Commit cc19741

Browse files
authoredFeb 14, 2019
Merge pull request #180 from juanjux/fix_177
Correctly handle python2 kwargs
2 parents fe965ac + d395809 commit cc19741

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed
 

‎driver/normalizer/normalizer.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ func mapStr(nativeType string) Mapping {
8888
{Name: uast.KeyType, Op: String("Boxed" + nativeType)},
8989
{Name: "boxed_value", Op: UASTType(uast.String{}, Obj{
9090
uast.KeyPos: Var("pos_"),
91-
"Value": Var("s"),
92-
"Format": String(""),
91+
"Value": Var("s"),
92+
//"Format": String(""),
9393
})},
9494
{Name: "noops_previous", Optional: "np_opt", Op: Var("noops_previous")},
9595
{Name: "noops_sameline", Optional: "ns_opt", Op: Var("noops_sameline")},
@@ -114,7 +114,7 @@ var Normalizers = []Mapping{
114114
{Name: uast.KeyType, Op: String("BoxedName")},
115115
{Name: "boxed_value", Op: UASTType(uast.Identifier{}, Obj{
116116
uast.KeyPos: Var("pos_"),
117-
"Name": Var("id"),
117+
"Name": Var("id"),
118118
})},
119119
{Name: "noops_previous", Optional: "np_opt", Op: Var("noops_previous")},
120120
{Name: "noops_sameline", Optional: "ns_opt", Op: Var("noops_sameline")},
@@ -136,7 +136,7 @@ var Normalizers = []Mapping{
136136
{Name: uast.KeyType, Op: String("BoxedBoolLiteral")},
137137
{Name: "boxed_value", Op: UASTType(uast.Bool{}, Obj{
138138
uast.KeyPos: Var("pos_"),
139-
"Value": Var("lv"),
139+
"Value": Var("lv"),
140140
})},
141141
{Name: "noops_previous", Optional: "np_opt", Op: Var("noops_previous")},
142142
{Name: "noops_sameline", Optional: "ns_opt", Op: Var("noops_sameline")},
@@ -225,8 +225,8 @@ var Normalizers = []Mapping{
225225
uast.KeyToken: Var("name"),
226226
},
227227
Obj{
228-
"Name": identifierWithPos("name"),
229-
"MapVariadic": Bool(true),
228+
"Name": identifierWithPos("name"),
229+
"MapVariadic": Bool(true),
230230
},
231231
)),
232232

‎native/python_package/python_driver/astimprove.py

+43-4
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,18 @@ 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 = {
225+
"arg": kwarg,
226+
"annotation": None,
227+
# the tokenizer will fix the positions later
228+
"lineno": 1,
229+
"end_lineno": 1,
230+
"col_offset": 0,
231+
"end_col_offset": 0
232+
}
221233
kwarg["ast_type"] = "kwarg"
222234
norm_args.append(self.visit(kwarg))
223235

@@ -264,13 +276,40 @@ def visit_other_field(self, node: Node) -> VisitResult:
264276
from pprint import pprint
265277

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

276315
if spec.loader:

0 commit comments

Comments
 (0)
This repository has been archived.