python / python/cpython

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

Aperta
#156,035 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

stdlib topic-pathlib type-bug
Lingua principale
Python
Stelle
77.2k
Fork
36k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia eseguendo il reproducer fornito, quindi individua pathlib.Path.rename() e Path.replace() e traccia quando viene validata la loro destinazione. Il lavoro è completato quando le destinazioni bytes vengono rifiutate prima che una delle due operazioni modifichi lo stato della sorgente o della destinazione, con copertura per entrambi i metodi.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
operating-systems
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.