diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2009-05-01 21:09:44 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2009-05-01 21:09:44 (GMT) |
commit | 7fff09629811ff43d77aca27c6b999634f775fee (patch) | |
tree | f6153f684e381d57ce75fed0a4946386b83d9c3c /Lib/shutil.py | |
parent | edc364717ecd31be14c86cc73af1920c9efedf5f (diff) | |
download | cpython-7fff09629811ff43d77aca27c6b999634f775fee.zip cpython-7fff09629811ff43d77aca27c6b999634f775fee.tar.gz cpython-7fff09629811ff43d77aca27c6b999634f775fee.tar.bz2 |
Merged revisions 72178 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r72178 | antoine.pitrou | 2009-05-01 22:55:35 +0200 (ven., 01 mai 2009) | 4 lines
Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
error when a named pipe is encountered, rather than blocking infinitely.
........
Diffstat (limited to 'Lib/shutil.py')
-rw-r--r-- | Lib/shutil.py | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/Lib/shutil.py b/Lib/shutil.py index 7a84646..9ef1cf2 100644 --- a/Lib/shutil.py +++ b/Lib/shutil.py @@ -11,11 +11,15 @@ from os.path import abspath import fnmatch __all__ = ["copyfileobj","copyfile","copymode","copystat","copy","copy2", - "copytree","move","rmtree","Error"] + "copytree","move","rmtree","Error", "SpecialFileError"] class Error(EnvironmentError): pass +class SpecialFileError(EnvironmentError): + """Raised when trying to do a kind of operation (e.g. copying) which is + not supported on a special file (e.g. a named pipe)""" + try: WindowsError except NameError: @@ -48,6 +52,15 @@ def copyfile(src, dst): fsrc = None fdst = None + for fn in [src, dst]: + try: + st = os.stat(fn) + except OSError: + # File most likely does not exist + pass + # XXX What about other special files? (sockets, devices...) + if stat.S_ISFIFO(st.st_mode): + raise SpecialFileError("`%s` is a named pipe" % fn) try: fsrc = open(src, 'rb') fdst = open(dst, 'wb') @@ -157,14 +170,14 @@ def copytree(src, dst, symlinks=False, ignore=None): elif os.path.isdir(srcname): copytree(srcname, dstname, symlinks, ignore) else: + # Will raise a SpecialFileError for unsupported file types copy2(srcname, dstname) - # XXX What about devices, sockets etc.? - except (IOError, os.error) as why: - errors.append((srcname, dstname, str(why))) # catch the Error from the recursive copytree so that we can # continue with other files except Error as err: errors.extend(err.args[0]) + except EnvironmentError as why: + errors.append((srcname, dstname, str(why))) try: copystat(src, dst) except OSError as why: |