Pathlib.iterdir semantics change dramatically under Python 3.13
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 77.2k
- Fork
- 35.9k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Bug report
Bug description:
This code behaves very differently on Python 3.13 than 3.12:
(p for p in pathlib.Path('does-not-exist').iterdir())
🐚 py -3.12 -c "import pathlib; (p for p in pathlib.Path('does-not-exist').iterdir())"
🐚 py -3.13 -c "import pathlib; (p for p in pathlib.Path('does-not-exist').iterdir())"
Traceback (most recent call last):
File "<string>", line 1, in <module>
import pathlib; (p for p in pathlib.Path('does-not-exist').iterdir())
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
File "/opt/homebrew/Cellar/python@3.13/3.13.1/Frameworks/Python.framework/Versions/3.13/lib/python3.13/pathlib/_local.py", line 575, in iterdir
with os.scandir(root_dir) as scandir_it:
~~~~~~~~~~^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: 'does-not-exist'
In Python 3.12, the generator expression was evaluated lazily, not invoking path.iterdir() until the generator was consumed. On 3.13, at least a portion of the generator is evaluated, triggering the exception when the dir does not exist.
I discovered this issue in https://github.com/jaraco/jaraco.develop/issues/26.
I looked at What's New for Python 3.13, and there's only one mention of generator expressions regarding mutation of locals in generator expressions, which doesn't seem to be relevant here.
That change mentions https://github.com/python/cpython/issues/74929, so maybe that change is also implicated in the change in execution order.
CPython versions tested on:
3.13
Operating systems tested on:
macOS
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Riproducete il comportamento delle espressioni generatrici su Python 3.12 e 3.13, quindi ispezionate pathlib/_local.py intorno alla riga 575 e la semantica delle espressioni generatrici discussa in issue 74929. Determinate se l’ordine di valutazione modificato è intenzionale o costituisce una regressione, e aggiungete un test di regressione mirato che mostri il momento previsto del FileNotFoundError.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100