138
138
"""
139
139
140
140
import contextlib
141
+ import doctest
141
142
from io import StringIO
142
143
import itertools
143
144
import os
@@ -301,14 +302,14 @@ def contains_doctest(text):
301
302
return bool (m )
302
303
303
304
305
+ @_api .deprecated ("3.5" , alternative = "doctest.script_from_examples" )
304
306
def unescape_doctest (text ):
305
307
"""
306
308
Extract code from a piece of text, which contains either Python code
307
309
or doctests.
308
310
"""
309
311
if not contains_doctest (text ):
310
312
return text
311
-
312
313
code = ""
313
314
for line in text .split ("\n " ):
314
315
m = re .match (r'^\s*(>>>|\.\.\.) (.*)$' , line )
@@ -321,11 +322,16 @@ def unescape_doctest(text):
321
322
return code
322
323
323
324
325
+ @_api .deprecated ("3.5" )
324
326
def 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 ):
325
332
"""Split code at plt.show()."""
326
333
parts = []
327
334
is_doctest = contains_doctest (text )
328
-
329
335
part = []
330
336
for line in text .split ("\n " ):
331
337
if (not is_doctest and line .strip () == 'plt.show()' ) or \
@@ -337,7 +343,7 @@ def split_code_at_show(text):
337
343
part .append (line )
338
344
if "\n " .join (part ).strip ():
339
345
parts .append ("\n " .join (part ))
340
- return parts
346
+ return is_doctest , parts
341
347
342
348
343
349
# -----------------------------------------------------------------------------
@@ -437,11 +443,20 @@ class PlotError(RuntimeError):
437
443
pass
438
444
439
445
446
+ @_api .deprecated ("3.5" )
440
447
def run_code (code , code_path , ns = None , function_name = None ):
441
448
"""
442
449
Import a Python module from a path, and run the function given by
443
450
name, if function_name is not None.
444
451
"""
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
+ """
445
460
446
461
# Change the working directory to the directory of the example, so
447
462
# 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):
466
481
sys , argv = [code_path ], path = [os .getcwd (), * sys .path ]), \
467
482
contextlib .redirect_stdout (StringIO ()):
468
483
try :
469
- code = unescape_doctest (code )
470
484
if ns is None :
471
485
ns = {}
472
486
if not ns :
@@ -529,7 +543,7 @@ def render_figures(code, code_path, output_dir, output_base, context,
529
543
530
544
# Try to determine if all images already exist
531
545
532
- code_pieces = split_code_at_show (code )
546
+ is_doctest , code_pieces = _split_code_at_show (code )
533
547
534
548
# Look for single-figure output files first
535
549
all_exists = True
@@ -593,7 +607,9 @@ def render_figures(code, code_path, output_dir, output_base, context,
593
607
elif close_figs :
594
608
plt .close ('all' )
595
609
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 )
597
613
598
614
images = []
599
615
fig_managers = _pylab_helpers .Gcf .get_all_fig_managers ()
@@ -816,7 +832,9 @@ def run(arguments, content, options, state_machine, state, lineno):
816
832
817
833
# copy script (if necessary)
818
834
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 ,
820
838
encoding = 'utf-8' )
821
839
822
840
return errors
0 commit comments