Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/IPython/terminal/debugger.py: 27%

92 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-20 06:09 +0000

1import asyncio 

2import os 

3import sys 

4 

5from IPython.core.debugger import Pdb 

6from IPython.core.completer import IPCompleter 

7from .ptutils import IPythonPTCompleter 

8from .shortcuts import create_ipython_shortcuts 

9from . import embed 

10 

11from pathlib import Path 

12from pygments.token import Token 

13from prompt_toolkit.shortcuts.prompt import PromptSession 

14from prompt_toolkit.enums import EditingMode 

15from prompt_toolkit.formatted_text import PygmentsTokens 

16from prompt_toolkit.history import InMemoryHistory, FileHistory 

17from concurrent.futures import ThreadPoolExecutor 

18 

19from prompt_toolkit import __version__ as ptk_version 

20PTK3 = ptk_version.startswith('3.') 

21 

22 

23# we want to avoid ptk as much as possible when using subprocesses 

24# as it uses cursor positioning requests, deletes color .... 

25_use_simple_prompt = "IPY_TEST_SIMPLE_PROMPT" in os.environ 

26 

27 

28class TerminalPdb(Pdb): 

29 """Standalone IPython debugger.""" 

30 

31 def __init__(self, *args, pt_session_options=None, **kwargs): 

32 Pdb.__init__(self, *args, **kwargs) 

33 self._ptcomp = None 

34 self.pt_init(pt_session_options) 

35 self.thread_executor = ThreadPoolExecutor(1) 

36 

37 def pt_init(self, pt_session_options=None): 

38 """Initialize the prompt session and the prompt loop 

39 and store them in self.pt_app and self.pt_loop. 

40 

41 Additional keyword arguments for the PromptSession class 

42 can be specified in pt_session_options. 

43 """ 

44 if pt_session_options is None: 

45 pt_session_options = {} 

46 

47 def get_prompt_tokens(): 

48 return [(Token.Prompt, self.prompt)] 

49 

50 if self._ptcomp is None: 

51 compl = IPCompleter( 

52 shell=self.shell, namespace={}, global_namespace={}, parent=self.shell 

53 ) 

54 # add a completer for all the do_ methods 

55 methods_names = [m[3:] for m in dir(self) if m.startswith("do_")] 

56 

57 def gen_comp(self, text): 

58 return [m for m in methods_names if m.startswith(text)] 

59 import types 

60 newcomp = types.MethodType(gen_comp, compl) 

61 compl.custom_matchers.insert(0, newcomp) 

62 # end add completer. 

63 

64 self._ptcomp = IPythonPTCompleter(compl) 

65 

66 # setup history only when we start pdb 

67 if self.shell.debugger_history is None: 

68 if self.shell.debugger_history_file is not None: 

69 p = Path(self.shell.debugger_history_file).expanduser() 

70 if not p.exists(): 

71 p.touch() 

72 self.debugger_history = FileHistory(os.path.expanduser(str(p))) 

73 else: 

74 self.debugger_history = InMemoryHistory() 

75 else: 

76 self.debugger_history = self.shell.debugger_history 

77 

78 options = dict( 

79 message=(lambda: PygmentsTokens(get_prompt_tokens())), 

80 editing_mode=getattr(EditingMode, self.shell.editing_mode.upper()), 

81 key_bindings=create_ipython_shortcuts(self.shell), 

82 history=self.debugger_history, 

83 completer=self._ptcomp, 

84 enable_history_search=True, 

85 mouse_support=self.shell.mouse_support, 

86 complete_style=self.shell.pt_complete_style, 

87 style=getattr(self.shell, "style", None), 

88 color_depth=self.shell.color_depth, 

89 ) 

90 

91 if not PTK3: 

92 options['inputhook'] = self.shell.inputhook 

93 options.update(pt_session_options) 

94 if not _use_simple_prompt: 

95 self.pt_loop = asyncio.new_event_loop() 

96 self.pt_app = PromptSession(**options) 

97 

98 def cmdloop(self, intro=None): 

99 """Repeatedly issue a prompt, accept input, parse an initial prefix 

100 off the received input, and dispatch to action methods, passing them 

101 the remainder of the line as argument. 

102 

103 override the same methods from cmd.Cmd to provide prompt toolkit replacement. 

104 """ 

105 if not self.use_rawinput: 

106 raise ValueError('Sorry ipdb does not support use_rawinput=False') 

107 

108 # In order to make sure that prompt, which uses asyncio doesn't 

109 # interfere with applications in which it's used, we always run the 

110 # prompt itself in a different thread (we can't start an event loop 

111 # within an event loop). This new thread won't have any event loop 

112 # running, and here we run our prompt-loop. 

113 self.preloop() 

114 

115 try: 

116 if intro is not None: 

117 self.intro = intro 

118 if self.intro: 

119 print(self.intro, file=self.stdout) 

120 stop = None 

121 while not stop: 

122 if self.cmdqueue: 

123 line = self.cmdqueue.pop(0) 

124 else: 

125 self._ptcomp.ipy_completer.namespace = self.curframe_locals 

126 self._ptcomp.ipy_completer.global_namespace = self.curframe.f_globals 

127 

128 # Run the prompt in a different thread. 

129 if not _use_simple_prompt: 

130 try: 

131 line = self.thread_executor.submit( 

132 self.pt_app.prompt 

133 ).result() 

134 except EOFError: 

135 line = "EOF" 

136 else: 

137 line = input("ipdb> ") 

138 

139 line = self.precmd(line) 

140 stop = self.onecmd(line) 

141 stop = self.postcmd(stop, line) 

142 self.postloop() 

143 except Exception: 

144 raise 

145 

146 def do_interact(self, arg): 

147 ipshell = embed.InteractiveShellEmbed( 

148 config=self.shell.config, 

149 banner1="*interactive*", 

150 exit_msg="*exiting interactive console...*", 

151 ) 

152 global_ns = self.curframe.f_globals 

153 ipshell( 

154 module=sys.modules.get(global_ns["__name__"], None), 

155 local_ns=self.curframe_locals, 

156 ) 

157 

158 

159def set_trace(frame=None): 

160 """ 

161 Start debugging from `frame`. 

162 

163 If frame is not specified, debugging starts from caller's frame. 

164 """ 

165 TerminalPdb().set_trace(frame or sys._getframe().f_back) 

166 

167 

168if __name__ == '__main__': 

169 import pdb 

170 # IPython.core.debugger.Pdb.trace_dispatch shall not catch 

171 # bdb.BdbQuit. When started through __main__ and an exception 

172 # happened after hitting "c", this is needed in order to 

173 # be able to quit the debugging session (see #9950). 

174 old_trace_dispatch = pdb.Pdb.trace_dispatch 

175 pdb.Pdb = TerminalPdb # type: ignore 

176 pdb.Pdb.trace_dispatch = old_trace_dispatch # type: ignore 

177 pdb.main()