python / python/cpython

`pathlib.Path.rename()` and `replace()` may move a file before rejecting a bytes target

Offen
#156,035 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

stdlib topic-pathlib type-bug
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
35.9k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Bug report

Bug description:

pathlib.Path.rename() and Path.replace() can successfully move a file and then raise TypeError when the target is a bytes path, or an os.PathLike object whose __fspath__() method returns bytes.

The important issue is not whether pathlib should support bytes paths. Pathlib deliberately requires string paths. The problem is that target validation takes place only after the filesystem has already been modified. A caller that sees the exception may reasonably assume that the rename or replacement failed,even though the source no longer exists and the destination now contains the file.

Reproducer
import os
import tempfile
from pathlib import Path


for method_name in ("rename", "replace"):
    with tempfile.TemporaryDirectory() as directory:
        source = Path(directory, "source")
        target = Path(directory, "target")
        source.write_text("payload")

        try:
            getattr(source, method_name)(os.fsencode(target))
        except Exception as error:
            print(method_name, type(error).__name__, str(error))

        print("source exists:", source.exists())
        print("target exists:", target.exists())
        print("target contents:", target.read_text())

result:

rename TypeError argument should be a str or an os.PathLike object where __fspath__ returns a str, not 'bytes'
source exists: False
target exists: True
target contents: payload
replace TypeError argument should be a str or an os.PathLike object where __fspath__ returns a str, not 'bytes'
source exists: False
target exists: True
target contents: payload
CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs
  • gh-156036

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Führen Sie zunächst den bereitgestellten Reproduzierer aus. Suchen Sie dann pathlib.Path.rename() und Path.replace() und verfolgen Sie, wann ihr Ziel validiert wird. Als abgeschlossen gilt die Aufgabe, wenn bytes-Ziele zurückgewiesen werden, bevor eine der beiden Operationen den Zustand der Quelle oder des Ziels verändert, mit Abdeckung für beide Methoden.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
operating-systems
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.