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

Commit 89a0205

Browse files
committed
Empty module is not an error anymore and returns a module-only AST
- Removed unneeded code
1 parent da7d5b9 commit 89a0205

File tree

2 files changed

+31
-60
lines changed

2 files changed

+31
-60
lines changed

native/python_package/python_driver/requestprocessor.py

Lines changed: 30 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,6 @@
2222
Request = NewType('Request', Dict[str, Any])
2323
Response = NewType('Response', Dict[AnyStr, Any])
2424

25-
class RequestCheckException(Exception):
26-
"""
27-
Exception produced while there is an error during the processing
28-
of the request. It will cause an error reply to be produced on the
29-
output buffer.
30-
"""
31-
pass
32-
3325
class EmptyCodeException(Exception):
3426
"""
3527
Exception produced when the input code is empty. This should
@@ -75,26 +67,6 @@ def _tostr_request(self, request: RawRequest) -> Request:
7567
"""
7668
pass
7769

78-
def _parse_input_request(self, request: RawRequest) -> Tuple[str, str]:
79-
"""
80-
Check the incoming request package and validate that the 'content' and
81-
'language' keys are not missing and that 'language' and 'language_version'
82-
are the right ones for this driver. It will also call _preprare_dict
83-
to covnvert the request bytestrings to str.
84-
85-
:param request: The incoming request, already deserialized.
86-
87-
.. raises::
88-
EmptyCodeException if the code was empty.
89-
"""
90-
str_request = self._tostr_request(request)
91-
code = asstr(str_request.get('content', ''))
92-
93-
if not code:
94-
raise EmptyCodeException('Bad input message, missing content')
95-
96-
return code, asstr(str_request.get('filepath', ''))
97-
9870
@abc.abstractmethod
9971
def _send_response(self, response: Response) -> None:
10072
"""
@@ -118,18 +90,7 @@ def _return_error(self, filepath: AnyStr='', status: AnyStr='error',
11890
:param status: error type, 'error' or 'fatal'
11991
"""
12092

121-
if status == 'fatal':
122-
ret_ast = None
123-
elif ast:
124-
ret_ast = ast
125-
else:
126-
# Empty modules are still modules
127-
ret_ast = {"PY3AST": {
128-
"ast_type" : "Module",
129-
"lineno" : 1,
130-
"col_offset" : 1,
131-
}}
132-
93+
ret_ast = None if status == 'fatal' else ast
13394
logging.error('Filepath: {}, Errors: {}'.format(filepath, self.errors))
13495
response = Response({
13596
'status': status,
@@ -156,24 +117,35 @@ def process_request(self, request: RawRequest) -> None:
156117
self.errors = []
157118

158119
try:
159-
code, filepath = self._parse_input_request(request)
160-
161-
# We want the code detection to be fast and we prefer Python3 AST so using
162-
# the stop_on_ok_ast will avoid running a Python2 subprocess to check the
163-
# AST with Python2 if the Python3 version (which is a better AST anyway) worked
164-
resdict = detector.detect(codestr=code, stop_on_ok_ast=True)
165-
codeinfo = resdict['<code_string>']
166-
version = codeinfo['version']
167-
168-
if version in (3, 6) and codeinfo['py3ast']:
169-
ast = codeinfo['py3ast']
170-
elif version in (1, 2) and codeinfo['py2ast']:
171-
ast = codeinfo['py2ast']
120+
str_request = self._tostr_request(request)
121+
code = asstr(str_request.get('content', ''))
122+
123+
if code:
124+
# We want the code detection to be fast and we prefer Python3 AST so using
125+
# the stop_on_ok_ast will avoid running a Python2 subprocess to check the
126+
# AST with Python2 if the Python3 version (which is a better AST anyway) worked
127+
resdict = detector.detect(codestr=code, stop_on_ok_ast=True)
128+
codeinfo = resdict['<code_string>']
129+
version = codeinfo['version']
130+
131+
if version in (3, 6) and codeinfo['py3ast']:
132+
ast = codeinfo['py3ast']
133+
elif version in (1, 2) and codeinfo['py2ast']:
134+
ast = codeinfo['py2ast']
135+
else:
136+
raise Exception('Could not determine Python version')
137+
138+
if not ast:
139+
raise Exception('Empty AST generated from non empty code')
172140
else:
173-
raise Exception('Could not determine Python version')
174-
175-
if not ast:
176-
raise Exception('Empty AST generated')
141+
# Module with empty code (like __init__.py) return a module-only AST
142+
# since this would still have semantic meaning for Python
143+
ast = {"PY3AST": {
144+
"ast_type" : "Module",
145+
"lineno" : 1,
146+
"col_offset" : 1,
147+
}}
148+
version = 3
177149

178150
response = Response({
179151
'status' : 'ok',
@@ -185,6 +157,7 @@ def process_request(self, request: RawRequest) -> None:
185157
'driver' : 'python23:%s' % __version__,
186158
}
187159
})
160+
188161
if filepath:
189162
response['filepath'] = filepath
190163

native/python_package/test/test_python_driver.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
sys.path.append('..')
1818
from python_driver import __version__, get_processor_instance
1919
from python_driver.requestprocessor import (
20-
Request, Response, RequestProcessorJSON,
21-
InBuffer, RequestCheckException, EmptyCodeException
22-
)
20+
Request, Response, RequestProcessorJSON, InBuffer, EmptyCodeException)
2321

2422
if TEST_MSGPACK:
2523
from python_driver.requestprocessor import RequestProcessorMSGPack

0 commit comments

Comments
 (0)