Use `Path objects within copyfile` (#12708)

This commit is contained in:
Adam Turner
2024-07-29 18:37:14 +01:00
committed by GitHub
parent a4102a7507
commit c1ed1e3b70

View File

@@ -11,12 +11,12 @@ import sys
import unicodedata
from io import StringIO
from os import path
from pathlib import Path
from typing import TYPE_CHECKING
from sphinx.locale import __
if TYPE_CHECKING:
from pathlib import Path
from types import TracebackType
from typing import Any
@@ -107,12 +107,15 @@ def copyfile(
.. note:: :func:`copyfile` is a no-op if *source* and *dest* are identical.
"""
if not path.exists(source):
msg = f'{os.fsdecode(source)} does not exist'
# coerce to Path objects
source = Path(source)
dest = Path(dest)
if not source.exists():
msg = f'{source} does not exist'
raise FileNotFoundError(msg)
if (
not (dest_exists := path.exists(dest)) or
not (dest_exists := dest.exists()) or
# comparison must be done using shallow=False since
# two different files might have the same size
not filecmp.cmp(source, dest, shallow=False)
@@ -125,7 +128,7 @@ def copyfile(
msg = __('Aborted attempted copy from %s to %s '
'(the destination path has existing data).')
logger.warning(msg, os.fsdecode(source), os.fsdecode(dest),
logger.warning(msg, source, dest,
type='misc', subtype='copy_overwrite')
return