From: Antoine Pitrou Date: Fri, 1 May 2009 21:09:44 +0000 (+0000) Subject: Merged revisions 72178 via svnmerge from X-Git-Tag: v3.1b1~95 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7fff09629811ff43d77aca27c6b999634f775fee;p=python 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. ........ --- diff --git a/Lib/shutil.py b/Lib/shutil.py index 7a84646fc8..9ef1cf2ea6 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: diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py index 86c7b649e7..cdb039d4d0 100644 --- a/Lib/test/test_shutil.py +++ b/Lib/test/test_shutil.py @@ -9,6 +9,7 @@ import os import os.path from test import support from test.support import TESTFN +TESTFN2 = TESTFN + "2" class TestShutil(unittest.TestCase): def test_rmtree_errors(self): @@ -226,6 +227,38 @@ class TestShutil(unittest.TestCase): finally: shutil.rmtree(TESTFN, ignore_errors=True) + if hasattr(os, "mkfifo"): + # Issue #3002: copyfile and copytree block indefinitely on named pipes + def test_copyfile_named_pipe(self): + os.mkfifo(TESTFN) + try: + self.assertRaises(shutil.SpecialFileError, + shutil.copyfile, TESTFN, TESTFN2) + self.assertRaises(shutil.SpecialFileError, + shutil.copyfile, __file__, TESTFN) + finally: + os.remove(TESTFN) + + def test_copytree_named_pipe(self): + os.mkdir(TESTFN) + try: + subdir = os.path.join(TESTFN, "subdir") + os.mkdir(subdir) + pipe = os.path.join(subdir, "mypipe") + os.mkfifo(pipe) + try: + shutil.copytree(TESTFN, TESTFN2) + except shutil.Error as e: + errors = e.args[0] + self.assertEqual(len(errors), 1) + src, dst, error_msg = errors[0] + self.assertEqual("`%s` is a named pipe" % pipe, error_msg) + else: + self.fail("shutil.Error should have been raised") + finally: + shutil.rmtree(TESTFN, ignore_errors=True) + shutil.rmtree(TESTFN2, ignore_errors=True) + class TestMove(unittest.TestCase): diff --git a/Misc/NEWS b/Misc/NEWS index 4319a4a9ed..2a5e2e67da 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -107,6 +107,9 @@ Installation Library ------- +- Issue #3002: ``shutil.copyfile()`` and ``shutil.copytree()`` now raise an + error when a named pipe is encountered, rather than blocking infinitely. + - Issue #5857: tokenize.tokenize() now returns named tuples. - Issue #4305: ctypes should now build again on mipsel-linux-gnu