Skip to content

Commit ae608c2

Browse files
Don't crash when trying to complete broken mappings.
Show the traceback when something goes wrong while reading input in the REPL due to completer bugs or other bugs. Don't crash the REPL.
1 parent 52705a7 commit ae608c2

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

ptpython/completer.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,10 @@ def abbr_meta(text: str) -> str:
505505
display=f"[{k_repr}]",
506506
display_meta=abbr_meta(self._do_repr(result[k])),
507507
)
508+
except KeyError:
509+
# `result[k]` lookup failed. Trying to complete
510+
# broken object.
511+
pass
508512
except ReprFailedError:
509513
pass
510514

@@ -521,6 +525,10 @@ def abbr_meta(text: str) -> str:
521525
display=f"[{k_repr}]",
522526
display_meta=abbr_meta(self._do_repr(result[k])),
523527
)
528+
except KeyError:
529+
# `result[k]` lookup failed. Trying to complete
530+
# broken object.
531+
pass
524532
except ReprFailedError:
525533
pass
526534

ptpython/repl.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@ def run(self) -> None:
135135
text = self.read()
136136
except EOFError:
137137
return
138+
except BaseException as e:
139+
# Something went wrong while reading input.
140+
# (E.g., a bug in the completer that propagates. Don't
141+
# crash the REPL.)
142+
traceback.print_exc()
143+
continue
138144

139145
# Run it; display the result (or errors if applicable).
140146
self.run_and_show_expression(text)
@@ -192,6 +198,12 @@ async def run_async(self) -> None:
192198
text = await loop.run_in_executor(None, self.read)
193199
except EOFError:
194200
return
201+
except BaseException:
202+
# Something went wrong while reading input.
203+
# (E.g., a bug in the completer that propagates. Don't
204+
# crash the REPL.)
205+
traceback.print_exc()
206+
continue
195207

196208
# Eval.
197209
await self.run_and_show_expression_async(text)

0 commit comments

Comments
 (0)