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

Commit 82c8973

Browse files
Denys Smirnovdennwc
Denys Smirnov
authored andcommitted
handle syntax errors correctly; add syntax error test
Signed-off-by: Denys Smirnov <[email protected]>
1 parent 3130acc commit 82c8973

File tree

5 files changed

+27
-15
lines changed

5 files changed

+27
-15
lines changed

Diff for: Gopkg.lock

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Gopkg.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
[[constraint]]
55
name = "gopkg.in/bblfsh/sdk.v2"
6-
version = "v2.4.x"
6+
version = "v2.5.x"
77

88
[prune]
99
go-tests = true

Diff for: fixtures/_syntax_error.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def testfnc1(a, b):
2+
= b
3+
return 1

Diff for: native/python_package/python_driver/requestprocessor.py

+19-10
Original file line numberDiff line numberDiff line change
@@ -135,22 +135,26 @@ def process_request(self, request: RawRequest) -> None:
135135
codeinfo = resdict['<code_string>']
136136
version = codeinfo['version']
137137

138+
failed = False
139+
138140
if version in (3, 6) and codeinfo['py3ast']:
139141
orig_ast = codeinfo['py3ast']["PY3AST"]
140142
elif version in (1, 2) and codeinfo['py2ast']:
141143
orig_ast = codeinfo['py2ast']["PY2AST"]
142144
else:
143-
raise Exception(
145+
failed = True
146+
self.errors = [
144147
'Errors produced trying to get an AST for both Python versions' +
145148
'\n------ Python2 errors:\n%s' % codeinfo['py2_ast_errors'] +
146149
'\n------ Python3 errors:\n%s' % codeinfo['py3_ast_errors']
147-
)
148-
149-
if not orig_ast:
150-
raise Exception('Empty AST generated from non empty code')
151-
ast = AstImprover(code, orig_ast).parse()
152-
if not ast:
153-
raise Exception('Empty AST generated from non empty code')
150+
]
151+
152+
if not failed:
153+
if not orig_ast:
154+
raise Exception('Empty AST generated from non empty code')
155+
ast = AstImprover(code, orig_ast).parse()
156+
if not ast:
157+
raise Exception('Empty AST generated from non empty code')
154158
else:
155159
# Module with empty code (like __init__.py) return a module-only AST
156160
# since this would still have semantic meaning for Python
@@ -162,16 +166,21 @@ def process_request(self, request: RawRequest) -> None:
162166
version = 3
163167

164168
response = Response({
165-
'status' : 'ok',
166169
'errors' : self.errors,
167-
'ast' : {"PY%dAST" % version: ast},
168170
'metadata' : {
169171
'language' : 'python',
170172
'language_version' : version,
171173
'driver' : 'python23:%s' % __version__,
172174
}
173175
})
174176

177+
if failed:
178+
response['status'] = 'error'
179+
response['ast'] = None
180+
else:
181+
response['status'] = 'ok'
182+
response['ast'] = {"PY%dAST" % version: ast}
183+
175184
if filepath:
176185
response['filepath'] = filepath
177186

Diff for: native/python_package/test/test_python_driver.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def _check_reply_dict(self, response: Response, has_errors: bool=False) -> None:
103103
status = response.get('status')
104104

105105
if has_errors:
106-
assert status in ('ok', 'fatal')
106+
assert status in ('error', 'fatal')
107107
errors = response.get('errors', list)
108108
self.assertIsInstance(errors, list)
109109
self.assertGreater(len(errors), 0)

0 commit comments

Comments
 (0)