Skip to content

Commit b92cda5

Browse files
committed
Also use setattr_cm for sys.path.
1 parent 1f09d18 commit b92cda5

File tree

1 file changed

+2
-5
lines changed

1 file changed

+2
-5
lines changed

lib/matplotlib/sphinxext/plot_directive.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,6 @@ def run_code(code, code_path, ns=None, function_name=None):
468468
# it can get at its data files, if any. Add its path to sys.path
469469
# so it can import any helper modules sitting beside it.
470470
pwd = os.getcwd()
471-
old_sys_path = sys.path.copy()
472471
if setup.config.plot_working_directory is not None:
473472
try:
474473
os.chdir(setup.config.plot_working_directory)
@@ -480,13 +479,12 @@ def run_code(code, code_path, ns=None, function_name=None):
480479
raise TypeError(str(err) + '\n`plot_working_directory` option in '
481480
'Sphinx configuration file must be a string or '
482481
'None')
483-
sys.path.insert(0, setup.config.plot_working_directory)
484482
elif code_path is not None:
485483
dirname = os.path.abspath(os.path.dirname(code_path))
486484
os.chdir(dirname)
487-
sys.path.insert(0, dirname)
488485

489-
with cbook._setattr_cm(sys, argv=[code_path]), \
486+
with cbook._setattr_cm(
487+
sys, argv=[code_path], path=[os.getcwd(), *sys.path]), \
490488
contextlib.redirect_stdout(StringIO()):
491489
try:
492490
code = unescape_doctest(code)
@@ -507,7 +505,6 @@ def run_code(code, code_path, ns=None, function_name=None):
507505
raise PlotError(traceback.format_exc())
508506
finally:
509507
os.chdir(pwd)
510-
sys.path[:] = old_sys_path
511508
return ns
512509

513510

0 commit comments

Comments
 (0)