mirror of https://github.com/python/cpython
#1492704: Make shutil.copyfile() raise a distinct SameFileError
Patch by Atsuo Ishimoto.
This commit is contained in:
parent
7506dab2e8
commit
77d3283370
|
@ -52,7 +52,7 @@ Directory and files operations
|
||||||
Copy the contents (no metadata) of the file named *src* to a file named
|
Copy the contents (no metadata) of the file named *src* to a file named
|
||||||
*dst* and return *dst*. *dst* must be the complete target file name; look at
|
*dst* and return *dst*. *dst* must be the complete target file name; look at
|
||||||
:func:`shutil.copy` for a copy that accepts a target directory path. If
|
:func:`shutil.copy` for a copy that accepts a target directory path. If
|
||||||
*src* and *dst* are the same files, :exc:`Error` is raised.
|
*src* and *dst* are the same files, :exc:`SameFileError` is raised.
|
||||||
|
|
||||||
The destination location must be writable; otherwise, an :exc:`OSError` exception
|
The destination location must be writable; otherwise, an :exc:`OSError` exception
|
||||||
will be raised. If *dst* already exists, it will be replaced. Special files
|
will be raised. If *dst* already exists, it will be replaced. Special files
|
||||||
|
@ -67,6 +67,16 @@ Directory and files operations
|
||||||
:exc:`IOError` used to be raised instead of :exc:`OSError`.
|
:exc:`IOError` used to be raised instead of :exc:`OSError`.
|
||||||
Added *follow_symlinks* argument.
|
Added *follow_symlinks* argument.
|
||||||
Now returns *dst*.
|
Now returns *dst*.
|
||||||
|
Raise :exc:`SameFileError` instead of :exc:`Error`.
|
||||||
|
|
||||||
|
|
||||||
|
.. exception:: SameFileError
|
||||||
|
|
||||||
|
This exception is raised if source and destination in :func:`copyfile`
|
||||||
|
are the same file.
|
||||||
|
|
||||||
|
.. versionadded:: 3.3
|
||||||
|
|
||||||
|
|
||||||
.. function:: copymode(src, dst, *, follow_symlinks=True)
|
.. function:: copymode(src, dst, *, follow_symlinks=True)
|
||||||
|
|
||||||
|
|
|
@ -42,6 +42,9 @@ __all__ = ["copyfileobj", "copyfile", "copymode", "copystat", "copy", "copy2",
|
||||||
class Error(EnvironmentError):
|
class Error(EnvironmentError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
class SameFileError(Error):
|
||||||
|
"""Raised when source and destination are the same file."""
|
||||||
|
|
||||||
class SpecialFileError(EnvironmentError):
|
class SpecialFileError(EnvironmentError):
|
||||||
"""Raised when trying to do a kind of operation (e.g. copying) which is
|
"""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)"""
|
not supported on a special file (e.g. a named pipe)"""
|
||||||
|
@ -90,7 +93,7 @@ def copyfile(src, dst, *, follow_symlinks=True):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
if _samefile(src, dst):
|
if _samefile(src, dst):
|
||||||
raise Error("`%s` and `%s` are the same file" % (src, dst))
|
raise SameFileError("{!r} and {!r} are the same file".format(src, dst))
|
||||||
|
|
||||||
for fn in [src, dst]:
|
for fn in [src, dst]:
|
||||||
try:
|
try:
|
||||||
|
@ -215,6 +218,9 @@ def copy(src, dst, *, follow_symlinks=True):
|
||||||
If follow_symlinks is false, symlinks won't be followed. This
|
If follow_symlinks is false, symlinks won't be followed. This
|
||||||
resembles GNU's "cp -P src dst".
|
resembles GNU's "cp -P src dst".
|
||||||
|
|
||||||
|
If source and destination are the same file, a SameFileError will be
|
||||||
|
raised.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
if os.path.isdir(dst):
|
if os.path.isdir(dst):
|
||||||
dst = os.path.join(dst, os.path.basename(src))
|
dst = os.path.join(dst, os.path.basename(src))
|
||||||
|
|
|
@ -18,7 +18,8 @@ from shutil import (_make_tarball, _make_zipfile, make_archive,
|
||||||
register_archive_format, unregister_archive_format,
|
register_archive_format, unregister_archive_format,
|
||||||
get_archive_formats, Error, unpack_archive,
|
get_archive_formats, Error, unpack_archive,
|
||||||
register_unpack_format, RegistryError,
|
register_unpack_format, RegistryError,
|
||||||
unregister_unpack_format, get_unpack_formats)
|
unregister_unpack_format, get_unpack_formats,
|
||||||
|
SameFileError)
|
||||||
import tarfile
|
import tarfile
|
||||||
import warnings
|
import warnings
|
||||||
|
|
||||||
|
@ -688,7 +689,7 @@ class TestShutil(unittest.TestCase):
|
||||||
with open(src, 'w') as f:
|
with open(src, 'w') as f:
|
||||||
f.write('cheddar')
|
f.write('cheddar')
|
||||||
os.link(src, dst)
|
os.link(src, dst)
|
||||||
self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
|
self.assertRaises(shutil.SameFileError, shutil.copyfile, src, dst)
|
||||||
with open(src, 'r') as f:
|
with open(src, 'r') as f:
|
||||||
self.assertEqual(f.read(), 'cheddar')
|
self.assertEqual(f.read(), 'cheddar')
|
||||||
os.remove(dst)
|
os.remove(dst)
|
||||||
|
@ -708,7 +709,7 @@ class TestShutil(unittest.TestCase):
|
||||||
# to TESTFN/TESTFN/cheese, while it should point at
|
# to TESTFN/TESTFN/cheese, while it should point at
|
||||||
# TESTFN/cheese.
|
# TESTFN/cheese.
|
||||||
os.symlink('cheese', dst)
|
os.symlink('cheese', dst)
|
||||||
self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
|
self.assertRaises(shutil.SameFileError, shutil.copyfile, src, dst)
|
||||||
with open(src, 'r') as f:
|
with open(src, 'r') as f:
|
||||||
self.assertEqual(f.read(), 'cheddar')
|
self.assertEqual(f.read(), 'cheddar')
|
||||||
os.remove(dst)
|
os.remove(dst)
|
||||||
|
@ -1215,6 +1216,14 @@ class TestShutil(unittest.TestCase):
|
||||||
self.assertTrue(os.path.exists(rv))
|
self.assertTrue(os.path.exists(rv))
|
||||||
self.assertEqual(read_file(src_file), read_file(dst_file))
|
self.assertEqual(read_file(src_file), read_file(dst_file))
|
||||||
|
|
||||||
|
def test_copyfile_same_file(self):
|
||||||
|
# copyfile() should raise SameFileError if the source and destination
|
||||||
|
# are the same.
|
||||||
|
src_dir = self.mkdtemp()
|
||||||
|
src_file = os.path.join(src_dir, 'foo')
|
||||||
|
write_file(src_file, 'foo')
|
||||||
|
self.assertRaises(SameFileError, shutil.copyfile, src_file, src_file)
|
||||||
|
|
||||||
def test_copytree_return_value(self):
|
def test_copytree_return_value(self):
|
||||||
# copytree returns its destination path.
|
# copytree returns its destination path.
|
||||||
src_dir = self.mkdtemp()
|
src_dir = self.mkdtemp()
|
||||||
|
|
|
@ -44,6 +44,9 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #1492704: shutil.copyfile() raises a distinct SameFileError now if
|
||||||
|
source and destination are the same file. Patch by Atsuo Ishimoto.
|
||||||
|
|
||||||
- Issue #15397: inspect.getmodulename() is now based directly on importlib
|
- Issue #15397: inspect.getmodulename() is now based directly on importlib
|
||||||
via a new importlib.machinery.all_suffixes() API.
|
via a new importlib.machinery.all_suffixes() API.
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue