Skip to content

Commit 51aa6e4

Browse files
committed
FIX: do not try to split the code if a function name is passed
We do not know where it is safe to split the code if we are going to later call a function for the generated namespace.
1 parent 3c90d8f commit 51aa6e4

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

lib/matplotlib/sphinxext/plot_directive.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -342,21 +342,25 @@ def split_code_at_show(text):
342342
return _split_code_at_show(text)[1]
343343

344344

345-
def _split_code_at_show(text):
345+
def _split_code_at_show(text, function_name):
346346
"""Split code at plt.show()."""
347-
parts = []
347+
348348
is_doctest = contains_doctest(text)
349-
part = []
350-
for line in text.split("\n"):
351-
if (not is_doctest and line.startswith('plt.show')) or \
352-
(is_doctest and line.strip() == '>>> plt.show()'):
353-
part.append(line)
349+
if function_name is None:
350+
parts = []
351+
part = []
352+
for line in text.split("\n"):
353+
if (not is_doctest and line.startswith('plt.show')) or \
354+
(is_doctest and line.strip() == '>>> plt.show()'):
355+
part.append(line)
356+
parts.append("\n".join(part))
357+
part = []
358+
else:
359+
part.append(line)
360+
if "\n".join(part).strip():
354361
parts.append("\n".join(part))
355-
part = []
356-
else:
357-
part.append(line)
358-
if "\n".join(part).strip():
359-
parts.append("\n".join(part))
362+
else:
363+
parts = [text]
360364
return is_doctest, parts
361365

362366

@@ -572,7 +576,7 @@ def render_figures(code, code_path, output_dir, output_base, context,
572576

573577
# Try to determine if all images already exist
574578

575-
is_doctest, code_pieces = _split_code_at_show(code)
579+
is_doctest, code_pieces = _split_code_at_show(code, function_name)
576580

577581
# Look for single-figure output files first
578582
all_exists = True

0 commit comments

Comments
 (0)