138138"""
139139
140140import contextlib
141+ import doctest
141142from io import StringIO
142143import itertools
143144import os
@@ -301,14 +302,14 @@ def contains_doctest(text):
301302 return bool (m )
302303
303304
305+ @_api .deprecated ("3.5" , alternative = "doctest.script_from_examples" )
304306def unescape_doctest (text ):
305307 """
306308 Extract code from a piece of text, which contains either Python code
307309 or doctests.
308310 """
309311 if not contains_doctest (text ):
310312 return text
311-
312313 code = ""
313314 for line in text .split ("\n " ):
314315 m = re .match (r'^\s*(>>>|\.\.\.) (.*)$' , line )
@@ -321,11 +322,16 @@ def unescape_doctest(text):
321322 return code
322323
323324
325+ @_api .deprecated ("3.5" )
324326def split_code_at_show (text ):
327+ """Split code at plt.show()."""
328+ return _split_code_at_show (text )[1 ]
329+
330+
331+ def _split_code_at_show (text ):
325332 """Split code at plt.show()."""
326333 parts = []
327334 is_doctest = contains_doctest (text )
328-
329335 part = []
330336 for line in text .split ("\n " ):
331337 if (not is_doctest and line .strip () == 'plt.show()' ) or \
@@ -337,7 +343,7 @@ def split_code_at_show(text):
337343 part .append (line )
338344 if "\n " .join (part ).strip ():
339345 parts .append ("\n " .join (part ))
340- return parts
346+ return is_doctest , parts
341347
342348
343349# -----------------------------------------------------------------------------
@@ -437,11 +443,20 @@ class PlotError(RuntimeError):
437443 pass
438444
439445
446+ @_api .deprecated ("3.5" )
440447def run_code (code , code_path , ns = None , function_name = None ):
441448 """
442449 Import a Python module from a path, and run the function given by
443450 name, if function_name is not None.
444451 """
452+ _run_code (unescape_doctest (code ), code_path , ns , function_name )
453+
454+
455+ def _run_code (code , code_path , ns = None , function_name = None ):
456+ """
457+ Import a Python module from a path, and run the function given by
458+ name, if function_name is not None.
459+ """
445460
446461 # Change the working directory to the directory of the example, so
447462 # it can get at its data files, if any. Add its path to sys.path
@@ -466,7 +481,6 @@ def run_code(code, code_path, ns=None, function_name=None):
466481 sys , argv = [code_path ], path = [os .getcwd (), * sys .path ]), \
467482 contextlib .redirect_stdout (StringIO ()):
468483 try :
469- code = unescape_doctest (code )
470484 if ns is None :
471485 ns = {}
472486 if not ns :
@@ -529,7 +543,7 @@ def render_figures(code, code_path, output_dir, output_base, context,
529543
530544 # Try to determine if all images already exist
531545
532- code_pieces = split_code_at_show (code )
546+ is_doctest , code_pieces = _split_code_at_show (code )
533547
534548 # Look for single-figure output files first
535549 all_exists = True
@@ -593,7 +607,9 @@ def render_figures(code, code_path, output_dir, output_base, context,
593607 elif close_figs :
594608 plt .close ('all' )
595609
596- run_code (code_piece , code_path , ns , function_name )
610+ _run_code (doctest .script_from_examples (code_piece ) if is_doctest
611+ else code_piece ,
612+ code_path , ns , function_name )
597613
598614 images = []
599615 fig_managers = _pylab_helpers .Gcf .get_all_fig_managers ()
@@ -816,7 +832,9 @@ def run(arguments, content, options, state_machine, state, lineno):
816832
817833 # copy script (if necessary)
818834 Path (dest_dir , output_base + source_ext ).write_text (
819- unescape_doctest (code ) if source_file_name == rst_file else code ,
835+ doctest .script_from_examples (code )
836+ if source_file_name == rst_file and is_doctest
837+ else code ,
820838 encoding = 'utf-8' )
821839
822840 return errors
0 commit comments