Skip to content
This repository was archived by the owner on Nov 3, 2023. It is now read-only.

Commit cce0cbb

Browse files
authored
Merge pull request #214 from c-w/master
Ignore SyntaxError while parsing
2 parents d0d5bec + 6963b60 commit cce0cbb

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

docs/release_notes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ Major Updates
1111

1212
* Support for Python 2.6 has been dropped (#206, #217).
1313

14+
Bug Fixes
15+
16+
* Made parser more robust to bad source files (#168, #214)
17+
1418
1.1.1 - October 4th, 2016
1519
-------------------------
1620

src/pydocstyle/parser.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,22 @@ def __init__(self, filelike):
209209
self._generator = tk.generate_tokens(filelike.readline)
210210
self.current = Token(*next(self._generator, None))
211211
self.line = self.current.start[0]
212+
self.log = logging.getLogger()
212213

213214
def move(self):
214215
previous = self.current
215-
current = next(self._generator, None)
216+
current = self._next_from_generator()
216217
self.current = None if current is None else Token(*current)
217218
self.line = self.current.start[0] if self.current else self.line
218219
return previous
219220

221+
def _next_from_generator(self):
222+
try:
223+
return next(self._generator, None)
224+
except (SyntaxError, tk.TokenError):
225+
self.log.warning('error generating tokens', exc_info=True)
226+
return None
227+
220228
def __iter__(self):
221229
while True:
222230
if self.current is not None:

src/tests/test_definitions.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ def nested_3(self):
126126
from __future__ import (, )
127127
"""
128128

129+
source_invalid_syntax = """
130+
while True:
131+
\ttry:
132+
pass
133+
"""
134+
135+
source_token_error = '['
136+
129137
source_complex_all = '''
130138
import foo
131139
import bar
@@ -222,8 +230,10 @@ def test_import_parser():
222230
source_future_import_invalid6,
223231
source_future_import_invalid7,
224232
source_future_import_invalid8,
233+
source_token_error,
234+
source_invalid_syntax,
225235
), 1):
226-
module = parse(StringIO(source_ucl), 'file_invalid{}.py'.format(i))
236+
module = parse(StringIO(source_ucli), 'file_invalid{}.py'.format(i))
227237

228238
assert Module('file_invalid{}.py'.format(i), _, 1,
229239
_, _, None, _, _,

0 commit comments

Comments
 (0)