##// END OF EJS Templates
Backport PR #12028: Use separate asyncio event loop for user input in debugger.
Matthias Bussonnier -
Show More
@@ -1,3 +1,4 b''
1 import asyncio
1 import signal
2 import signal
2 import sys
3 import sys
3
4
@@ -67,6 +68,7 b' class TerminalPdb(Pdb):'
67
68
68 if not PTK3:
69 if not PTK3:
69 options['inputhook'] = self.shell.inputhook
70 options['inputhook'] = self.shell.inputhook
71 self.pt_loop = asyncio.new_event_loop()
70 self.pt_app = PromptSession(**options)
72 self.pt_app = PromptSession(**options)
71
73
72 def cmdloop(self, intro=None):
74 def cmdloop(self, intro=None):
@@ -79,6 +81,19 b' class TerminalPdb(Pdb):'
79 if not self.use_rawinput:
81 if not self.use_rawinput:
80 raise ValueError('Sorry ipdb does not support use_rawinput=False')
82 raise ValueError('Sorry ipdb does not support use_rawinput=False')
81
83
84 # In order to make sure that asyncio code written in the
85 # interactive shell doesn't interfere with the prompt, we run the
86 # prompt in a different event loop.
87 # If we don't do this, people could spawn coroutine with a
88 # while/true inside which will freeze the prompt.
89
90 try:
91 old_loop = asyncio.get_event_loop()
92 except RuntimeError:
93 # This happens when the user used `asyncio.run()`.
94 old_loop = None
95
96
82 self.preloop()
97 self.preloop()
83
98
84 try:
99 try:
@@ -93,10 +108,16 b' class TerminalPdb(Pdb):'
93 else:
108 else:
94 self._ptcomp.ipy_completer.namespace = self.curframe_locals
109 self._ptcomp.ipy_completer.namespace = self.curframe_locals
95 self._ptcomp.ipy_completer.global_namespace = self.curframe.f_globals
110 self._ptcomp.ipy_completer.global_namespace = self.curframe.f_globals
111
112 asyncio.set_event_loop(self.pt_loop)
96 try:
113 try:
97 line = self.pt_app.prompt() # reset_current_buffer=True)
114 line = self.pt_app.prompt()
98 except EOFError:
115 except EOFError:
99 line = 'EOF'
116 line = 'EOF'
117 finally:
118 # Restore the original event loop.
119 asyncio.set_event_loop(old_loop)
120
100 line = self.precmd(line)
121 line = self.precmd(line)
101 stop = self.onecmd(line)
122 stop = self.onecmd(line)
102 stop = self.postcmd(stop, line)
123 stop = self.postcmd(stop, line)
General Comments 0
You need to be logged in to leave comments. Login now