Skip to content

Commit d59807a

Browse files
prompt-toolkit compatibility: CommandLineInterface now expects an eventloop in __init__.
1 parent 5ed0020 commit d59807a

4 files changed

Lines changed: 27 additions & 6 deletions

File tree

examples/python-input.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@
33
"""
44
from __future__ import unicode_literals
55

6+
from prompt_toolkit.contrib.shortcuts import create_eventloop
67
from ptpython.python_input import PythonCommandLineInterface
78

89

910
def main():
10-
cli = PythonCommandLineInterface()
11+
eventloop = create_eventloop()
12+
try:
13+
cli = PythonCommandLineInterface(eventloop)
1114

12-
code_obj = cli.cli.read_input()
13-
print('You said: ' + code_obj.text)
15+
code_obj = cli.cli.read_input()
16+
print('You said: ' + code_obj.text)
17+
finally:
18+
eventloop.close()
1419

1520

1621
if __name__ == '__main__':

ptpython/ipython.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from prompt_toolkit.contrib.regular_languages.compiler import compile
1616
from prompt_toolkit.contrib.regular_languages.completion import GrammarCompleter
1717
from prompt_toolkit.contrib.regular_languages.lexer import GrammarLexer
18+
from prompt_toolkit.contrib.shortcuts import create_eventloop
1819
from prompt_toolkit.document import Document
1920
from prompt_toolkit.layout.controls import TokenListControl
2021

@@ -168,8 +169,11 @@ def __init__(self, *a, **kw):
168169
def get_globals():
169170
return self.user_ns
170171

172+
self._eventloop = create_eventloop()
171173
self._cli = IPythonCommandLineInterface(
172-
self, get_globals=get_globals, vi_mode=vi_mode,
174+
self,
175+
eventloop=self._eventloop,
176+
get_globals=get_globals, vi_mode=vi_mode,
173177
history_filename=history_filename)
174178

175179
def raw_input(self, prompt=''):

ptpython/python_input.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def __init__(self, paste_mode=False):
7575

7676
class PythonCommandLineInterface(object):
7777
def __init__(self,
78+
eventloop,
7879
get_globals=None, get_locals=None,
7980
stdin=None, stdout=None,
8081
vi_mode=False, history_filename=None,
@@ -120,6 +121,7 @@ def __init__(self,
120121
buffers.update(_extra_buffers or {})
121122

122123
self.cli = CommandLineInterface(
124+
eventloop=eventloop,
123125
style=style,
124126
key_bindings_registry=self.key_bindings_manager.registry,
125127
buffers=buffers,

ptpython/repl.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from pygments.lexers import PythonTracebackLexer
1818

1919
from prompt_toolkit.utils import DummyContext
20+
from prompt_toolkit.contrib.shortcuts import create_eventloop, create_asyncio_eventloop
2021

2122
from ptpython.python_input import PythonCommandLineInterface, PythonStyle
2223

@@ -54,7 +55,7 @@ def asyncio_start_repl(self):
5455
"""
5556
(coroutine) Start a Read-Eval-print Loop for usage in asyncio. E.g.::
5657
57-
repl = PythonRepl(get_globals=lambda:globals())
58+
repl = PythonRepl(eventloop, get_globals=lambda:globals())
5859
yield from repl.asyncio_start_repl()
5960
"""
6061
try:
@@ -187,9 +188,18 @@ def get_globals():
187188
def get_locals():
188189
return locals
189190

190-
repl = PythonRepl(get_globals, get_locals, vi_mode=vi_mode, history_filename=history_filename,
191+
# Create eventloop.
192+
if return_asyncio_coroutine:
193+
eventloop = create_asyncio_eventloop()
194+
else:
195+
eventloop = create_eventloop()
196+
197+
# Create REPL.
198+
repl = PythonRepl(eventloop, get_globals, get_locals, vi_mode=vi_mode,
199+
history_filename=history_filename,
191200
style=(None if no_colors else PythonStyle))
192201

202+
# Start repl.
193203
patch_context = repl.cli.patch_stdout_context() if patch_stdout else DummyContext()
194204

195205
if return_asyncio_coroutine:

0 commit comments

Comments
 (0)