Skip to content

Commit f1c4906

Browse files
committed
Remove duplicated code in PythonRepl.run()
1 parent b13a69d commit f1c4906

File tree

1 file changed

+18
-37
lines changed

1 file changed

+18
-37
lines changed

ptpython/repl.py

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -55,43 +55,24 @@ def _load_start_paths(self) -> None:
5555
output.write("WARNING | File not found: {}\n\n".format(path))
5656

5757
def run(self) -> None:
58-
if self.terminal_title:
59-
set_title(self.terminal_title)
60-
61-
def prompt() -> str:
62-
# In order to make sure that asyncio code written in the
63-
# interactive shell doesn't interfere with the prompt, we run the
64-
# prompt in a different event loop.
65-
# If we don't do this, people could spawn coroutine with a
66-
# while/true inside which will freeze the prompt.
67-
68-
try:
69-
old_loop: Optional[asyncio.AbstractEventLoop] = asyncio.get_event_loop()
70-
except RuntimeError:
71-
# This happens when the user used `asyncio.run()`.
72-
old_loop = None
73-
74-
asyncio.set_event_loop(self.pt_loop)
75-
try:
76-
return self.app.run() # inputhook=inputhook)
77-
finally:
78-
# Restore the original event loop.
79-
asyncio.set_event_loop(old_loop)
80-
81-
while True:
82-
# Run the UI.
83-
try:
84-
text = prompt()
85-
except EOFError:
86-
return
87-
except KeyboardInterrupt:
88-
# Abort - try again.
89-
self.default_buffer.document = Document()
90-
else:
91-
self._process_text(text)
92-
93-
if self.terminal_title:
94-
clear_title()
58+
# In order to make sure that asyncio code written in the
59+
# interactive shell doesn't interfere with the prompt, we run the
60+
# prompt in a different event loop.
61+
# If we don't do this, people could spawn coroutine with a
62+
# while/true inside which will freeze the prompt.
63+
64+
try:
65+
old_loop: Optional[asyncio.AbstractEventLoop] = asyncio.get_event_loop()
66+
except RuntimeError:
67+
# This happens when the user used `asyncio.run()`.
68+
old_loop = None
69+
70+
asyncio.set_event_loop(self.pt_loop)
71+
try:
72+
return self.pt_loop.run_until_complete(self.run_async())
73+
finally:
74+
# Restore the original event loop.
75+
asyncio.set_event_loop(old_loop)
9576

9677
async def run_async(self) -> None:
9778
if self.terminal_title:

0 commit comments

Comments
 (0)