MCPcopy Index your code
hub / github.com/ipython/ipython / doctest_mode

Method doctest_mode

IPython/core/magics/basic.py:396–472  ·  view source on GitHub ↗

Toggle doctest mode on and off. This mode is intended to make IPython behave as much as possible like a plain Python shell, from the perspective of how its prompts, exceptions and output look. This makes it easy to copy and paste parts of a session into doctests. I

(self, parameter_s='')

Source from the content-addressed store, hash-verified

394
395 @line_magic
396 def doctest_mode(self, parameter_s=''):
397 """Toggle doctest mode on and off.
398
399 This mode is intended to make IPython behave as much as possible like a
400 plain Python shell, from the perspective of how its prompts, exceptions
401 and output look. This makes it easy to copy and paste parts of a
402 session into doctests. It does so by:
403
404 - Changing the prompts to the classic ``>>>`` ones.
405 - Changing the exception reporting mode to 'Plain'.
406 - Disabling pretty-printing of output.
407
408 Note that IPython also supports the pasting of code snippets that have
409 leading '>>>' and '...' prompts in them. This means that you can paste
410 doctests from files or docstrings (even if they have leading
411 whitespace), and the code will execute correctly. You can then use
412 '%history -t' to see the translated history; this will give you the
413 input after removal of all the leading prompts and whitespace, which
414 can be pasted back into an editor.
415
416 With these features, you can switch into this mode easily whenever you
417 need to do testing and changes to doctests, without having to leave
418 your existing IPython session.
419 """
420
421 # Shorthands
422 shell = self.shell
423 meta = shell.meta
424 disp_formatter = self.shell.display_formatter
425 ptformatter = disp_formatter.formatters['text/plain']
426 # dstore is a data store kept in the instance metadata bag to track any
427 # changes we make, so we can undo them later.
428 dstore = meta.setdefault('doctest_mode',Struct())
429 save_dstore = dstore.setdefault
430
431 # save a few values we'll need to recover later
432 mode = save_dstore('mode',False)
433 save_dstore('rc_pprint',ptformatter.pprint)
434 save_dstore('xmode',shell.InteractiveTB.mode)
435 save_dstore('rc_separate_out',shell.separate_out)
436 save_dstore('rc_separate_out2',shell.separate_out2)
437 save_dstore('rc_separate_in',shell.separate_in)
438 save_dstore('rc_active_types',disp_formatter.active_types)
439
440 if not mode:
441 # turn on
442
443 # Prompt separators like plain python
444 shell.separate_in = ''
445 shell.separate_out = ''
446 shell.separate_out2 = ''
447
448
449 ptformatter.pprint = False
450 disp_formatter.active_types = ['text/plain']
451
452 shell.run_line_magic("xmode", "Plain")
453 else:

Callers

nothing calls this directly

Calls 3

StructClass · 0.90
run_line_magicMethod · 0.80
switch_doctest_modeMethod · 0.45

Tested by

no test coverage detected