mirror of
https://github.com/python/cpython.git
synced 2025-08-01 07:33:08 +00:00
Issue #3002: shutil.copyfile()
and shutil.copytree()
now raise an
error when a named pipe is encountered, rather than blocking infinitely.
This commit is contained in:
parent
1d499265e0
commit
1fc0231a22
3 changed files with 53 additions and 4 deletions
|
@ -11,11 +11,15 @@ from os.path import abspath
|
||||||
import fnmatch
|
import fnmatch
|
||||||
|
|
||||||
__all__ = ["copyfileobj","copyfile","copymode","copystat","copy","copy2",
|
__all__ = ["copyfileobj","copyfile","copymode","copystat","copy","copy2",
|
||||||
"copytree","move","rmtree","Error"]
|
"copytree","move","rmtree","Error", "SpecialFileError"]
|
||||||
|
|
||||||
class Error(EnvironmentError):
|
class Error(EnvironmentError):
|
||||||
pass
|
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:
|
try:
|
||||||
WindowsError
|
WindowsError
|
||||||
except NameError:
|
except NameError:
|
||||||
|
@ -48,6 +52,15 @@ def copyfile(src, dst):
|
||||||
|
|
||||||
fsrc = None
|
fsrc = None
|
||||||
fdst = 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:
|
try:
|
||||||
fsrc = open(src, 'rb')
|
fsrc = open(src, 'rb')
|
||||||
fdst = open(dst, 'wb')
|
fdst = open(dst, 'wb')
|
||||||
|
@ -157,14 +170,14 @@ def copytree(src, dst, symlinks=False, ignore=None):
|
||||||
elif os.path.isdir(srcname):
|
elif os.path.isdir(srcname):
|
||||||
copytree(srcname, dstname, symlinks, ignore)
|
copytree(srcname, dstname, symlinks, ignore)
|
||||||
else:
|
else:
|
||||||
|
# Will raise a SpecialFileError for unsupported file types
|
||||||
copy2(srcname, dstname)
|
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
|
# catch the Error from the recursive copytree so that we can
|
||||||
# continue with other files
|
# continue with other files
|
||||||
except Error, err:
|
except Error, err:
|
||||||
errors.extend(err.args[0])
|
errors.extend(err.args[0])
|
||||||
|
except EnvironmentError, why:
|
||||||
|
errors.append((srcname, dstname, str(why)))
|
||||||
try:
|
try:
|
||||||
copystat(src, dst)
|
copystat(src, dst)
|
||||||
except OSError, why:
|
except OSError, why:
|
||||||
|
|
|
@ -9,6 +9,7 @@ import os
|
||||||
import os.path
|
import os.path
|
||||||
from test import test_support
|
from test import test_support
|
||||||
from test.test_support import TESTFN
|
from test.test_support import TESTFN
|
||||||
|
TESTFN2 = TESTFN + "2"
|
||||||
|
|
||||||
class TestShutil(unittest.TestCase):
|
class TestShutil(unittest.TestCase):
|
||||||
def test_rmtree_errors(self):
|
def test_rmtree_errors(self):
|
||||||
|
@ -240,6 +241,38 @@ class TestShutil(unittest.TestCase):
|
||||||
finally:
|
finally:
|
||||||
shutil.rmtree(TESTFN, ignore_errors=True)
|
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):
|
class TestMove(unittest.TestCase):
|
||||||
|
|
||||||
|
|
|
@ -261,6 +261,9 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #3002: ``shutil.copyfile()`` and ``shutil.copytree()`` now raise an
|
||||||
|
error when a named pipe is encountered, rather than blocking infinitely.
|
||||||
|
|
||||||
- Issue #3959: The ipaddr module has been added to the standard library.
|
- Issue #3959: The ipaddr module has been added to the standard library.
|
||||||
Contributed by Google.
|
Contributed by Google.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue