##// END OF EJS Templates
Merge pull request #12141 from jonathanslenders/ipdb-in-asyncio-fix...
Matthias Bussonnier -
r25540:a97ae46d merge
parent child Browse files
Show More
@@ -1,6 +1,7 b''
1 import asyncio
1 import asyncio
2 import signal
2 import signal
3 import sys
3 import sys
4 import threading
4
5
5 from IPython.core.debugger import Pdb
6 from IPython.core.debugger import Pdb
6
7
@@ -70,18 +71,11 b' class TerminalPdb(Pdb):'
70 if not self.use_rawinput:
71 if not self.use_rawinput:
71 raise ValueError('Sorry ipdb does not support use_rawinput=False')
72 raise ValueError('Sorry ipdb does not support use_rawinput=False')
72
73
73 # In order to make sure that asyncio code written in the
74 # In order to make sure that prompt, which uses asyncio doesn't
74 # interactive shell doesn't interfere with the prompt, we run the
75 # interfere with applications in which it's used, we always run the
75 # prompt in a different event loop.
76 # prompt itself in a different thread (we can't start an event loop
76 # If we don't do this, people could spawn coroutine with a
77 # within an event loop). This new thread won't have any event loop
77 # while/true inside which will freeze the prompt.
78 # running, and here we run our prompt-loop.
78
79 try:
80 old_loop = asyncio.get_event_loop()
81 except RuntimeError:
82 # This happens when the user used `asyncio.run()`.
83 old_loop = None
84
85
79
86 self.preloop()
80 self.preloop()
87
81
@@ -98,14 +92,25 b' class TerminalPdb(Pdb):'
98 self._ptcomp.ipy_completer.namespace = self.curframe_locals
92 self._ptcomp.ipy_completer.namespace = self.curframe_locals
99 self._ptcomp.ipy_completer.global_namespace = self.curframe.f_globals
93 self._ptcomp.ipy_completer.global_namespace = self.curframe.f_globals
100
94
101 asyncio.set_event_loop(self.pt_loop)
95 # Run the prompt in a different thread.
102 try:
96 line = ''
103 line = self.pt_app.prompt()
97 keyboard_interrupt = False
104 except EOFError:
98
105 line = 'EOF'
99 def in_thread():
106 finally:
100 nonlocal line, keyboard_interrupt
107 # Restore the original event loop.
101 try:
108 asyncio.set_event_loop(old_loop)
102 line = self.pt_app.prompt()
103 except EOFError:
104 line = 'EOF'
105 except KeyboardInterrupt:
106 keyboard_interrupt = True
107
108 th = threading.Thread(target=in_thread)
109 th.start()
110 th.join()
111
112 if keyboard_interrupt:
113 raise KeyboardInterrupt
109
114
110 line = self.precmd(line)
115 line = self.precmd(line)
111 stop = self.onecmd(line)
116 stop = self.onecmd(line)
General Comments 0
You need to be logged in to leave comments. Login now