summaryrefslogtreecommitdiffstats
path: root/Lib/importlib/_bootstrap.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/importlib/_bootstrap.py')
-rw-r--r--Lib/importlib/_bootstrap.py44
1 files changed, 26 insertions, 18 deletions
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
index 04d9bff..35b42ca 100644
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -482,27 +482,35 @@ class _SourceFileLoader(_FileLoader, SourceLoader):
def set_data(self, path, data):
"""Write bytes data to a file."""
+ parent, _, filename = path.rpartition(path_sep)
+ path_parts = []
+ # Figure out what directories are missing.
+ while parent and not _path_isdir(parent):
+ parent, _, part = parent.rpartition(path_sep)
+ path_parts.append(part)
+ # Create needed directories.
+ for part in reversed(path_parts):
+ parent = _path_join(parent, part)
+ try:
+ _os.mkdir(parent)
+ except IOError as exc:
+ # Probably another Python process already created the dir.
+ if exc.errno == errno.EEXIST:
+ continue
+ # If can't get proper access, then just forget about writing
+ # the data.
+ elif errno == errno.EACCES:
+ return
+ else:
+ raise
try:
- with _closing(_io.FileIO(path, 'wb')) as file:
+ with _io.FileIO(path, 'wb') as file:
file.write(data)
except IOError as exc:
- if exc.errno == errno.ENOENT:
- directory, _, filename = path.rpartition(path_sep)
- sub_directories = []
- while not _path_isdir(directory):
- directory, _, sub_dir = directory.rpartition(path_sep)
- sub_directories.append(sub_dir)
- for part in reversed(sub_directories):
- directory = _path_join(directory, part)
- try:
- _os.mkdir(directory)
- except IOError as exc:
- if exc.errno != errno.EACCES:
- raise
- else:
- return
- return self.set_data(path, data)
- elif exc.errno != errno.EACCES:
+ # Don't worry if you can't write bytecode.
+ if exc.errno == errno.EACCES:
+ return
+ else:
raise