Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8ac6bcb

Browse files
authored
Merge pull request #20109 from anntzer/plot_directive_doctest_trailing_text
Fix trailing text in doctest-syntax plot_directive.
2 parents b85e958 + f900c34 commit 8ac6bcb

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
``plot_directive`` internals deprecations
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
The following helpers in `matplotlib.sphinxext.plot_directive` are deprecated:
4+
``unescape_doctest`` (use `doctest.script_from_examples` instead),
5+
``split_code_at_show``, ``run_code``.

lib/matplotlib/sphinxext/plot_directive.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@
138138
"""
139139

140140
import contextlib
141+
import doctest
141142
from io import StringIO
142143
import itertools
143144
import 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")
304306
def 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")
324326
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):
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")
440447
def 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

lib/matplotlib/tests/tinypages/some_plots.rst

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,16 @@ Plot 2 doesn't use context either; has length 6:
1515

1616
plt.plot(range(6))
1717

18-
Plot 3 has length 4:
18+
Plot 3 has length 4, and uses doctest syntax:
1919

2020
.. plot::
21+
:format: doctest
2122

22-
plt.plot(range(4))
23+
This is a doctest...
24+
25+
>>> plt.plot(range(4))
26+
27+
... isn't it?
2328

2429
Plot 4 shows that a new block with context does not see the variable defined
2530
in the no-context block:

0 commit comments

Comments
 (0)