MCPcopy Create free account
hub / github.com/ipython/ipython / doctest_mode

Method doctest_mode

IPython/core/magics/basic.py:401–477  ·  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

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

Callers

nothing calls this directly

Calls 3

StructClass · 0.90
magicMethod · 0.45
switch_doctest_modeMethod · 0.45

Tested by

no test coverage detected