diff options
author | Jason R. Coombs <jaraco@jaraco.com> | 2017-07-13 14:03:23 -0400 |
---|---|---|
committer | Jason R. Coombs <jaraco@jaraco.com> | 2017-07-13 14:03:23 -0400 |
commit | 925dd35ec77d4cfd79c899f34255e3a753f33ca5 (patch) | |
tree | 676b1e44a2e187b6e2e7db3b7d44c700e80b6202 /pkg_resources | |
parent | 995d309317c6895a123c03df28bc8f51f6ead5f5 (diff) | |
download | external_python_setuptools-925dd35ec77d4cfd79c899f34255e3a753f33ca5.tar.gz external_python_setuptools-925dd35ec77d4cfd79c899f34255e3a753f33ca5.tar.bz2 external_python_setuptools-925dd35ec77d4cfd79c899f34255e3a753f33ca5.zip |
Avoid race condition in ensure_directory. Ref #1083.
Diffstat (limited to 'pkg_resources')
-rw-r--r-- | pkg_resources/__init__.py | 5 | ||||
-rw-r--r-- | pkg_resources/py31compat.py | 17 |
2 files changed, 20 insertions, 2 deletions
diff --git a/pkg_resources/__init__.py b/pkg_resources/__init__.py index 2ed07c3d..51b47492 100644 --- a/pkg_resources/__init__.py +++ b/pkg_resources/__init__.py @@ -67,6 +67,7 @@ try: except ImportError: importlib_machinery = None +from . import py31compat from pkg_resources.extern import appdirs from pkg_resources.extern import packaging __import__('pkg_resources.extern.packaging.version') @@ -74,6 +75,7 @@ __import__('pkg_resources.extern.packaging.specifiers') __import__('pkg_resources.extern.packaging.requirements') __import__('pkg_resources.extern.packaging.markers') + if (3, 0) < sys.version_info < (3, 3): raise RuntimeError("Python 3.3 or later is required") @@ -2958,8 +2960,7 @@ def _find_adapter(registry, ob): def ensure_directory(path): """Ensure that the parent directory of `path` exists""" dirname = os.path.dirname(path) - if not os.path.isdir(dirname): - os.makedirs(dirname) + py31compat.makedirs(dirname, exist_ok=True) def _bypass_ensure_directory(path): diff --git a/pkg_resources/py31compat.py b/pkg_resources/py31compat.py new file mode 100644 index 00000000..28120cac --- /dev/null +++ b/pkg_resources/py31compat.py @@ -0,0 +1,17 @@ +import os +import errno +import sys + + +PY32 = sys.version_info >= (3, 2) + + +def _makedirs_31(path, exist_ok=False): + try: + os.makedirs(path) + except OSError as exc: + if exc.errno != errno.EEXIST: + raise + + +makedirs = os.makedirs if PY32 else _makedirs_31 |