Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/IPython/core/displayhook.py: 22%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

172 statements  

1"""Displayhook for IPython. 

2 

3This defines a callable class that IPython uses for `sys.displayhook`. 

4""" 

5 

6# Copyright (c) IPython Development Team. 

7# Distributed under the terms of the Modified BSD License. 

8 

9import builtins as builtin_mod 

10import sys 

11import io as _io 

12import tokenize 

13 

14from traitlets.config.configurable import Configurable 

15from traitlets import Instance, Float 

16from warnings import warn 

17 

18from .history import HistoryOutput 

19 

20# TODO: Move the various attributes (cache_size, [others now moved]). Some 

21# of these are also attributes of InteractiveShell. They should be on ONE object 

22# only and the other objects should ask that one object for their values. 

23 

24class DisplayHook(Configurable): 

25 """The custom IPython displayhook to replace sys.displayhook. 

26 

27 This class does many things, but the basic idea is that it is a callable 

28 that gets called anytime user code returns a value. 

29 """ 

30 

31 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', 

32 allow_none=True) 

33 exec_result = Instance('IPython.core.interactiveshell.ExecutionResult', 

34 allow_none=True) 

35 cull_fraction = Float(0.2) 

36 

37 def __init__(self, shell=None, cache_size=1000, **kwargs): 

38 super().__init__(shell=shell, **kwargs) 

39 self._is_active = False 

40 cache_size_min = 3 

41 if cache_size <= 0: 

42 self.do_full_cache = 0 

43 cache_size = 0 

44 elif cache_size < cache_size_min: 

45 self.do_full_cache = 0 

46 cache_size = 0 

47 warn('caching was disabled (min value for cache size is %s).' % 

48 cache_size_min,stacklevel=3) 

49 else: 

50 self.do_full_cache = 1 

51 

52 self.cache_size = cache_size 

53 

54 # we need a reference to the user-level namespace 

55 self.shell = shell 

56 

57 self._,self.__,self.___ = '','','' 

58 

59 # these are deliberately global: 

60 to_user_ns = {'_':self._,'__':self.__,'___':self.___} 

61 self.shell.user_ns.update(to_user_ns) 

62 

63 @property 

64 def prompt_count(self): 

65 return self.shell.execution_count - 1 

66 

67 #------------------------------------------------------------------------- 

68 # Methods used in __call__. Override these methods to modify the behavior 

69 # of the displayhook. 

70 #------------------------------------------------------------------------- 

71 

72 def check_for_underscore(self): 

73 """Check if the user has set the '_' variable by hand.""" 

74 # If something injected a '_' variable in __builtin__, delete 

75 # ipython's automatic one so we don't clobber that. gettext() in 

76 # particular uses _, so we need to stay away from it. 

77 if '_' in builtin_mod.__dict__: 

78 try: 

79 user_value = self.shell.user_ns['_'] 

80 if user_value is not self._: 

81 return 

82 del self.shell.user_ns['_'] 

83 except KeyError: 

84 pass 

85 

86 def quiet(self): 

87 """Should we silence the display hook because of ';'?""" 

88 # do not print output if input ends in ';' 

89 

90 try: 

91 cell = self.shell.history_manager.input_hist_parsed[-1] 

92 except IndexError: 

93 # some uses of ipshellembed may fail here 

94 return False 

95 

96 return self.semicolon_at_end_of_expression(cell) 

97 

98 @staticmethod 

99 def semicolon_at_end_of_expression(expression): 

100 """Parse Python expression and detects whether last token is ';'""" 

101 

102 sio = _io.StringIO(expression) 

103 tokens = list(tokenize.generate_tokens(sio.readline)) 

104 

105 for token in reversed(tokens): 

106 if token[0] in (tokenize.ENDMARKER, tokenize.NL, tokenize.NEWLINE, tokenize.COMMENT): 

107 continue 

108 if (token[0] == tokenize.OP) and (token[1] == ';'): 

109 return True 

110 else: 

111 return False 

112 

113 def start_displayhook(self): 

114 """Start the displayhook, initializing resources.""" 

115 self._is_active = True 

116 

117 @property 

118 def is_active(self): 

119 return self._is_active 

120 

121 def write_output_prompt(self): 

122 """Write the output prompt. 

123 

124 The default implementation simply writes the prompt to 

125 ``sys.stdout``. 

126 """ 

127 # Use write, not print which adds an extra space. 

128 sys.stdout.write(self.shell.separate_out) 

129 outprompt = f'Out[{self.shell.execution_count - 1}]: ' 

130 if self.do_full_cache: 

131 sys.stdout.write(outprompt) 

132 

133 def compute_format_data(self, result): 

134 """Compute format data of the object to be displayed. 

135 

136 The format data is a generalization of the :func:`repr` of an object. 

137 In the default implementation the format data is a :class:`dict` of 

138 key value pair where the keys are valid MIME types and the values 

139 are JSON'able data structure containing the raw data for that MIME 

140 type. It is up to frontends to determine pick a MIME to to use and 

141 display that data in an appropriate manner. 

142 

143 This method only computes the format data for the object and should 

144 NOT actually print or write that to a stream. 

145 

146 Parameters 

147 ---------- 

148 result : object 

149 The Python object passed to the display hook, whose format will be 

150 computed. 

151 

152 Returns 

153 ------- 

154 (format_dict, md_dict) : dict 

155 format_dict is a :class:`dict` whose keys are valid MIME types and values are 

156 JSON'able raw data for that MIME type. It is recommended that 

157 all return values of this should always include the "text/plain" 

158 MIME type representation of the object. 

159 md_dict is a :class:`dict` with the same MIME type keys 

160 of metadata associated with each output. 

161 

162 """ 

163 return self.shell.display_formatter.format(result) 

164 

165 # This can be set to True by the write_output_prompt method in a subclass 

166 prompt_end_newline = False 

167 

168 def write_format_data(self, format_dict, md_dict=None) -> None: 

169 """Write the format data dict to the frontend. 

170 

171 This default version of this method simply writes the plain text 

172 representation of the object to ``sys.stdout``. Subclasses should 

173 override this method to send the entire `format_dict` to the 

174 frontends. 

175 

176 Parameters 

177 ---------- 

178 format_dict : dict 

179 The format dict for the object passed to `sys.displayhook`. 

180 md_dict : dict (optional) 

181 The metadata dict to be associated with the display data. 

182 """ 

183 if 'text/plain' not in format_dict: 

184 # nothing to do 

185 return 

186 # We want to print because we want to always make sure we have a 

187 # newline, even if all the prompt separators are ''. This is the 

188 # standard IPython behavior. 

189 result_repr = format_dict['text/plain'] 

190 if '\n' in result_repr: 

191 # So that multi-line strings line up with the left column of 

192 # the screen, instead of having the output prompt mess up 

193 # their first line. 

194 # We use the prompt template instead of the expanded prompt 

195 # because the expansion may add ANSI escapes that will interfere 

196 # with our ability to determine whether or not we should add 

197 # a newline. 

198 if not self.prompt_end_newline: 

199 # But avoid extraneous empty lines. 

200 result_repr = '\n' + result_repr 

201 

202 try: 

203 print(result_repr) 

204 except UnicodeEncodeError: 

205 # If a character is not supported by the terminal encoding replace 

206 # it with its \u or \x representation 

207 print(result_repr.encode(sys.stdout.encoding,'backslashreplace').decode(sys.stdout.encoding)) 

208 

209 def update_user_ns(self, result): 

210 """Update user_ns with various things like _, __, _1, etc.""" 

211 

212 # Avoid recursive reference when displaying _oh/Out 

213 if self.cache_size and result is not self.shell.user_ns['_oh']: 

214 if len(self.shell.user_ns['_oh']) >= self.cache_size and self.do_full_cache: 

215 self.cull_cache() 

216 

217 # Don't overwrite '_' and friends if '_' is in __builtin__ 

218 # (otherwise we cause buggy behavior for things like gettext). and 

219 # do not overwrite _, __ or ___ if one of these has been assigned 

220 # by the user. 

221 update_unders = True 

222 for unders in ['_'*i for i in range(1,4)]: 

223 if unders not in self.shell.user_ns: 

224 continue 

225 if getattr(self, unders) is not self.shell.user_ns.get(unders): 

226 update_unders = False 

227 

228 self.___ = self.__ 

229 self.__ = self._ 

230 self._ = result 

231 

232 if ('_' not in builtin_mod.__dict__) and (update_unders): 

233 self.shell.push({'_':self._, 

234 '__':self.__, 

235 '___':self.___}, interactive=False) 

236 

237 # hackish access to top-level namespace to create _1,_2... dynamically 

238 to_main = {} 

239 if self.do_full_cache: 

240 new_result = '_%s' % self.prompt_count 

241 to_main[new_result] = result 

242 self.shell.push(to_main, interactive=False) 

243 self.shell.user_ns['_oh'][self.prompt_count] = result 

244 

245 def fill_exec_result(self, result): 

246 if self.exec_result is not None: 

247 self.exec_result.result = result 

248 

249 def log_output(self, format_dict): 

250 """Log the output.""" 

251 self.shell.history_manager.outputs[self.prompt_count].append( 

252 HistoryOutput(output_type="execute_result", bundle=format_dict) 

253 ) 

254 if "text/plain" not in format_dict: 

255 # nothing to do 

256 return 

257 if self.shell.logger.log_output: 

258 self.shell.logger.log_write(format_dict['text/plain'], 'output') 

259 self.shell.history_manager.output_hist_reprs[self.prompt_count] = \ 

260 format_dict['text/plain'] 

261 

262 def finish_displayhook(self): 

263 """Finish up all displayhook activities.""" 

264 sys.stdout.write(self.shell.separate_out2) 

265 sys.stdout.flush() 

266 self._is_active = False 

267 

268 def __call__(self, result=None): 

269 """Printing with history cache management. 

270 

271 This is invoked every time the interpreter needs to print, and is 

272 activated by setting the variable sys.displayhook to it. 

273 """ 

274 self.check_for_underscore() 

275 if result is not None and not self.quiet(): 

276 self.start_displayhook() 

277 self.write_output_prompt() 

278 format_dict, md_dict = self.compute_format_data(result) 

279 self.update_user_ns(result) 

280 self.fill_exec_result(result) 

281 if format_dict: 

282 self.write_format_data(format_dict, md_dict) 

283 self.log_output(format_dict) 

284 self.finish_displayhook() 

285 

286 def cull_cache(self): 

287 """Output cache is full, cull the oldest entries""" 

288 oh = self.shell.user_ns.get('_oh', {}) 

289 sz = len(oh) 

290 cull_count = max(int(sz * self.cull_fraction), 2) 

291 warn('Output cache limit (currently {sz} entries) hit.\n' 

292 'Flushing oldest {cull_count} entries.'.format(sz=sz, cull_count=cull_count)) 

293 

294 for i, n in enumerate(sorted(oh)): 

295 if i >= cull_count: 

296 break 

297 self.shell.user_ns.pop('_%i' % n, None) 

298 oh.pop(n, None) 

299 

300 def flush(self): 

301 if not self.do_full_cache: 

302 raise ValueError("You shouldn't have reached the cache flush " 

303 "if full caching is not enabled!") 

304 # delete auto-generated vars from global namespace 

305 

306 for n in range(1,self.prompt_count + 1): 

307 key = '_'+repr(n) 

308 try: 

309 del self.shell.user_ns_hidden[key] 

310 except KeyError: 

311 pass 

312 try: 

313 del self.shell.user_ns[key] 

314 except KeyError: 

315 pass 

316 # In some embedded circumstances, the user_ns doesn't have the 

317 # '_oh' key set up. 

318 oh = self.shell.user_ns.get('_oh', None) 

319 if oh is not None: 

320 oh.clear() 

321 

322 # Release our own references to objects: 

323 self._, self.__, self.___ = '', '', '' 

324 

325 if '_' not in builtin_mod.__dict__: 

326 self.shell.user_ns.update({'_':self._,'__':self.__,'___':self.___}) 

327 import gc 

328 # TODO: Is this really needed? 

329 # IronPython blocks here forever 

330 if sys.platform != "cli": 

331 gc.collect() 

332 

333 

334class CapturingDisplayHook: 

335 def __init__(self, shell, outputs=None): 

336 self.shell = shell 

337 if outputs is None: 

338 outputs = [] 

339 self.outputs = outputs 

340 

341 def __call__(self, result=None): 

342 if result is None: 

343 return 

344 format_dict, md_dict = self.shell.display_formatter.format(result) 

345 self.outputs.append({ 'data': format_dict, 'metadata': md_dict })