From 4c912f36b8bc33983a5cb8219b0e5b4a9c4f35f0 2017-01-27 22:48:17 From: Matthias Bussonnier Date: 2017-01-27 22:48:17 Subject: [PATCH] Merge pull request #10208 from ipython/auto-backport-of-pr-10186 Backport PR #10186 on branch 5.x --- diff --git a/IPython/sphinxext/ipython_directive.py b/IPython/sphinxext/ipython_directive.py index 75c9e70..63ad0c1 100644 --- a/IPython/sphinxext/ipython_directive.py +++ b/IPython/sphinxext/ipython_directive.py @@ -127,6 +127,7 @@ from __future__ import print_function # Stdlib import atexit +import errno import os import re import sys @@ -365,9 +366,9 @@ class EmbeddedSphinxShell(object): source_dir = self.source_dir saveargs = decorator.split(' ') filename = saveargs[1] - # insert relative path to image file in source - outfile = os.path.relpath(os.path.join(savefig_dir,filename), - source_dir) + # insert relative path to image file in source (as absolute path for Sphinx) + outfile = '/' + os.path.relpath(os.path.join(savefig_dir,filename), + source_dir) imagerows = ['.. image:: %s'%outfile] @@ -849,14 +850,9 @@ class IPythonDirective(Directive): config = self.state.document.settings.env.config # get config variables to set figure output directory - outdir = self.state.document.settings.env.app.outdir savefig_dir = config.ipython_savefig_dir - source_dir = os.path.dirname(self.state.document.current_source) - if savefig_dir is None: - savefig_dir = config.html_static_path or '_static' - if isinstance(savefig_dir, list): - savefig_dir = os.path.join(*savefig_dir) - savefig_dir = os.path.join(outdir, savefig_dir) + source_dir = self.state.document.settings.env.srcdir + savefig_dir = os.path.join(source_dir, savefig_dir) # get regex and prompt stuff rgxin = config.ipython_rgxin @@ -875,6 +871,12 @@ class IPythonDirective(Directive): (savefig_dir, source_dir, rgxin, rgxout, promptin, promptout, mplbackend, exec_lines, hold_count) = self.get_config_options() + try: + os.makedirs(savefig_dir) + except OSError as e: + if e.errno != errno.EEXIST: + raise + if self.shell is None: # We will be here many times. However, when the # EmbeddedSphinxShell is created, its interactive shell member @@ -983,7 +985,7 @@ def setup(app): setup.app = app app.add_directive('ipython', IPythonDirective) - app.add_config_value('ipython_savefig_dir', None, 'env') + app.add_config_value('ipython_savefig_dir', 'savefig', 'env') app.add_config_value('ipython_rgxin', re.compile('In \[(\d+)\]:\s?(.*)\s*'), 'env') app.add_config_value('ipython_rgxout',