Skip to content

Commit 50af162

Browse files
committed
YapfBear: Catch parse errors and give result
The YapfBear earlier raised an error when the file was not parseable. Now, that error is caught and a Result is given out mentioning that the file has syntax errors and cannot be parsed. Fixes #750
1 parent ad5a413 commit 50af162

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

bears/python/YapfBear.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,17 @@ def run(self, filename, file,
144144
options += 'use_tabs = ' + str(not use_spaces)
145145
options = options.format(**locals())
146146

147-
with prepare_file(options.splitlines(keepends=True),
148-
None) as (file_, fname):
149-
corrected = FormatFile(filename,
150-
style_config=fname,
151-
verify=False)[0].splitlines(True)
147+
try:
148+
with prepare_file(options.splitlines(keepends=True),
149+
None) as (file_, fname):
150+
corrected = FormatFile(filename,
151+
style_config=fname,
152+
verify=False)[0].splitlines(True)
153+
except SyntaxError:
154+
yield Result.from_values(
155+
self, "The code cannot be parsed due to syntax errors.",
156+
filename)
157+
return
152158
diffs = Diff.from_string_arrays(file, corrected).split_diff()
153159
for diff in diffs:
154160
yield Result(self,

tests/python/YapfBearTest.py

+5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ def test_eof_handling(self):
3131
self.check_validity(self.uut, ['a = 2'], valid=True)
3232
self.check_validity(self.uut, ['\n'], valid=True)
3333

34+
def test_invalid_python(self):
35+
self.check_validity(self.uut, ['cout << "hello";\n'], valid=False)
36+
self.check_validity(self.uut, ['def a():', ' b=1', ' bad indent'],
37+
valid=False)
38+
3439
def test_valid_python_2(self):
3540
self.check_validity(self.uut, ['print 1\n'], valid=True)
3641

0 commit comments

Comments
 (0)