Issue #3002: `shutil.copyfile()` and `shutil.copytree()` now raise an
error when a named pipe is encountered, rather than blocking infinitely.
diff --git a/Lib/shutil.py b/Lib/shutil.py
index af96546..c46ec47 100644
--- a/Lib/shutil.py
+++ b/Lib/shutil.py
@@ -11,11 +11,15 @@
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 @@
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 @@
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), why:
- errors.append((srcname, dstname, str(why)))
# catch the Error from the recursive copytree so that we can
# continue with other files
except Error, err:
errors.extend(err.args[0])
+ except EnvironmentError, why:
+ errors.append((srcname, dstname, str(why)))
try:
copystat(src, dst)
except OSError, why: