X Tutup
Skip to content

Commit 695ba7c

Browse files
factor out checking if buffer could be complete and is valid
1 parent b2a3bf2 commit 695ba7c

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

bpython/curtsiesfrontend/repl.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -594,11 +594,8 @@ def push(self, line, insert_into_history=True):
594594
code_to_run = '\n'.join(self.buffer)
595595

596596
logger.debug('running %r in interpreter', self.buffer)
597-
try:
598-
c = bool(code.compile_command('\n'.join(self.buffer)))
599-
self.saved_predicted_parse_error = False
600-
except (ValueError, SyntaxError, OverflowError):
601-
c = self.saved_predicted_parse_error = True
597+
c, code_will_parse = self.buffer_finished_will_parse()
598+
self.saved_predicted_parse_error = not code_will_parse
602599
if c:
603600
logger.debug('finished - buffer cleared')
604601
self.display_lines.extend(self.display_buffer_lines)
@@ -609,6 +606,21 @@ def push(self, line, insert_into_history=True):
609606
self.coderunner.load_code(code_to_run)
610607
self.run_code_and_maybe_finish()
611608

609+
def buffer_finished_will_parse(self):
610+
"""Returns a tuple of whether the buffer could be complete and whether it will parse
611+
612+
True, True means code block is finished and no predicted parse error
613+
True, False means code block is finished because a parse error is predicted
614+
False, True means code block is unfinished
615+
False, False isn't possible - an predicted error makes code block done"""
616+
try:
617+
finished = bool(code.compile_command('\n'.join(self.buffer)))
618+
code_will_parse = True
619+
except (ValueError, SyntaxError, OverflowError):
620+
finished = True
621+
code_will_parse = False
622+
return finished, code_will_parse
623+
612624
def run_code_and_maybe_finish(self, for_code=None):
613625
r = self.coderunner.run_code(for_code=for_code)
614626
if r:

bpython/test/test_curtsies_repl.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import unittest
2+
import sys
3+
py3 = (sys.version_info[0] == 3)
4+
5+
from bpython.curtsiesfrontend import repl
6+
7+
class TestCurtsiesRepl(unittest.TestCase):
8+
9+
def setUp(self):
10+
self.repl = repl.Repl()
11+
12+
def test_buffer_finished_will_parse(self):
13+
self.repl.buffer = ['1 + 1']
14+
self.assertTrue(self.repl.buffer_finished_will_parse(), (True, True))
15+
self.repl.buffer = ['def foo(x):']
16+
self.assertTrue(self.repl.buffer_finished_will_parse(), (False, True))
17+
self.repl.buffer = ['def foo(x)']
18+
self.assertTrue(self.repl.buffer_finished_will_parse(), (True, False))
19+
self.repl.buffer = ['def foo(x):', 'return 1']
20+
self.assertTrue(self.repl.buffer_finished_will_parse(), (True, False))
21+
self.repl.buffer = ['def foo(x):', ' return 1']
22+
self.assertTrue(self.repl.buffer_finished_will_parse(), (True, True))
23+
self.repl.buffer = ['def foo(x):', ' return 1', '']
24+
self.assertTrue(self.repl.buffer_finished_will_parse(), (True, True))
25+
26+
if __name__ == '__main__':
27+
unittest.main()

0 commit comments

Comments
 (0)
X Tutup