diff options
author | 2013-06-14 18:33:00 -0400 | |
---|---|---|
committer | 2013-06-14 18:33:00 -0400 | |
commit | 33915eba7c8293eab4962345fbbb1e5d193295ed (patch) | |
tree | 320c366c8c8f651c6c0d515b662f8cecf2e41cac /Lib/py_compile.py | |
parent | Issue #18193: Add importlib.reload(), documenting (but not (diff) | |
download | cpython-33915eba7c8293eab4962345fbbb1e5d193295ed.tar.gz cpython-33915eba7c8293eab4962345fbbb1e5d193295ed.tar.bz2 cpython-33915eba7c8293eab4962345fbbb1e5d193295ed.zip |
Issue #17222: Raise FileExistsError when py_compile.compile would
overwrite a symlink or non-regular file with a regular file.
Diffstat (limited to 'Lib/py_compile.py')
-rw-r--r-- | Lib/py_compile.py | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/Lib/py_compile.py b/Lib/py_compile.py index 701e8acb9a6..cee35a5b6b1 100644 --- a/Lib/py_compile.py +++ b/Lib/py_compile.py @@ -7,6 +7,7 @@ import imp import importlib._bootstrap import importlib.machinery import os +import os.path import sys import traceback @@ -96,12 +97,25 @@ def compile(file, cfile=None, dfile=None, doraise=False, optimize=-1): See compileall.py for a script/module that uses this module to byte-compile all installed files (or all files in selected directories). + + Do note that FileExistsError is raised if cfile ends up pointing at a + non-regular file or symlink. Because the compilation uses a file renaming, + the resulting file would be regular and thus not the same type of file as + it was previously. """ if cfile is None: if optimize >= 0: cfile = imp.cache_from_source(file, debug_override=not optimize) else: cfile = imp.cache_from_source(file) + if os.path.islink(cfile): + msg = ('{} is a symlink and will be changed into a regular file if ' + 'import writes a byte-compiled file to it') + raise FileExistsError(msg.format(file, cfile)) + elif os.path.exists(cfile) and not os.path.isfile(cfile): + msg = ('{} is a non-regular file and will be changed into a regular ' + 'one if import writes a byte-compiled file to it') + raise FileExistsError(msg.format(file, cfile)) loader = importlib.machinery.SourceFileLoader('<py_compile>', file) source_bytes = loader.get_data(file) try: |