diff --git a/qtconsole/rich_jupyter_widget.py b/qtconsole/rich_jupyter_widget.py index 885033d8..f6ad52f9 100644 --- a/qtconsole/rich_jupyter_widget.py +++ b/qtconsole/rich_jupyter_widget.py @@ -8,7 +8,6 @@ from qtpy import QtCore, QtGui, QtWidgets -from ipython_genutils.path import ensure_dir_exists from traitlets import Bool from pygments.util import ClassNotFound @@ -22,6 +21,23 @@ latex_to_png = None +def _ensure_dir_exists(path, mode=0o755): + """ensure that a directory exists + + If it doesn't exist, try to create it and protect against a race condition + if another process is doing the same. + + The default permissions are 755, which differ from os.makedirs default of 777. + """ + if not os.path.exists(path): + try: + os.makedirs(path, mode=mode) + except OSError as e: + if e.errno != errno.EEXIST: + raise + elif not os.path.isdir(path): + raise IOError("%r exists but is not a directory" % path) + class LatexError(Exception): """Exception for Latex errors""" @@ -310,7 +326,7 @@ def _get_image_tag(self, match, path = None, format = "png"): return "Couldn't find image %s" % match.group("name") if path is not None: - ensure_dir_exists(path) + _ensure_dir_exists(path) relpath = os.path.basename(path) if image.save("%s/qt_img%s.%s" % (path, match.group("name"), format), "PNG"):