Skip to content

Commit

Permalink
Use Path objects within copyfile (#12708)
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner authored Jul 29, 2024
1 parent a4102a7 commit c1ed1e3
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions sphinx/util/osutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
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

Expand Down Expand Up @@ -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)
Expand All @@ -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

Expand Down

0 comments on commit c1ed1e3

Please sign in to comment.