Skip to content

Commit 829f92e

Browse files
yongrenjieQuLogic
andauthored
apply suggestions from code review
Co-authored with @QuLogic Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
1 parent 31ffe4b commit 829f92e

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

lib/matplotlib/sphinxext/plot_directive.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -436,16 +436,19 @@ def out_of_date(original, derived, includes=None):
436436
*derived* and *original* are full paths, and *includes* is optionally a
437437
list of full paths which may have been included in the *original*.
438438
"""
439+
if not os.path.exists(derived):
440+
return True
441+
439442
if includes is None:
440443
includes = []
441444
files_to_check = [original, *includes]
442445

443-
def out_of_date_one(original, derived):
444-
return (not os.path.exists(derived) or
445-
(os.path.exists(original) and
446-
os.stat(derived).st_mtime < os.stat(original).st_mtime))
446+
def out_of_date_one(original, derived_mtime):
447+
return (os.path.exists(original) and
448+
derived_mtime < os.stat(original).st_mtime)
447449

448-
return any(out_of_date_one(f, derived) for f in files_to_check)
450+
derived_mtime = os.stat(derived).st_mtime
451+
return any(out_of_date_one(f, derived_mtime) for f in files_to_check)
449452

450453

451454
class PlotError(RuntimeError):
@@ -763,9 +766,9 @@ def run(arguments, content, options, state_machine, state, lineno):
763766
except AttributeError:
764767
# the document.include_log attribute only exists in docutils >=0.17,
765768
# before that we need to inspect the state machine
766-
possible_sources = [os.path.join(setup.confdir, t[0])
767-
for t in state_machine.input_lines.items]
768-
source_file_includes = [f for f in set(possible_sources)
769+
possible_sources = {os.path.join(setup.confdir, t[0])
770+
for t in state_machine.input_lines.items}
771+
source_file_includes = [f for f in possible_sources
769772
if os.path.isfile(f)]
770773
# remove the source file itself from the includes
771774
try:

lib/matplotlib/tests/test_sphinxext.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import filecmp
44
import os
55
from pathlib import Path
6+
import shutil
67
from subprocess import Popen, PIPE
78
import sys
8-
import shutil
99

1010
import pytest
1111

@@ -15,7 +15,7 @@
1515

1616
def test_tinypages(tmpdir):
1717
source_dir = Path(tmpdir) / 'src'
18-
shutil.copytree(str(Path(__file__).parent / 'tinypages'), str(source_dir))
18+
shutil.copytree(Path(__file__).parent / 'tinypages', source_dir)
1919
html_dir = source_dir / '_build' / 'html'
2020
doctree_dir = source_dir / 'doctrees'
2121

@@ -63,11 +63,9 @@ def plot_directive_file(num):
6363
assert filecmp.cmp(range_6, plot_file(17))
6464

6565
# Modify the included plot
66-
with open(str(source_dir / 'included_plot_21.rst'), 'r') as file:
67-
contents = file.read()
66+
contents = (source_dir / 'included_plot_21.rst').read_text()
6867
contents = contents.replace('plt.plot(range(6))', 'plt.plot(range(4))')
69-
with open(str(source_dir / 'included_plot_21.rst'), 'w') as file:
70-
file.write(contents)
68+
(source_dir / 'included_plot_21.rst').write_text(contents)
7169
# Build the pages again and check that the modified file was updated
7270
modification_times = [plot_directive_file(i).stat().st_mtime
7371
for i in (1, 2, 3, 5)]

0 commit comments

Comments
 (0)