python / python/cpython

Linux: shutil.which fails to find executable in current working directory/cwd when PATH unset (subprocess works)

Aperta
#141,041 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Bug report

Bug description:

On Linux, executables from the cwd are used when PATH is unset:

$ PATH= python -V
Python 3.13.7

subprocess.run also works in this case:

$ PATH= python -q
>>> from subprocess import run
... run(['python', '-V'])
... 
Python 3.13.7
CompletedProcess(args=['python', '-V'], returncode=0)
>>> 

But shutil.which returns None:

>>> from shutil import which
>>> which('python')
>>> 

This is because when PATH is empty(""), the executable ("python") is passed to the kernel's execve directly, without resolving. execve then resolves the file relative to the cwd, I believe it's the case in both Linux and FreeBSD.
POSIX 2024 says this about the PATH environment variable:

If PATH is unset or is set to null, or if a path prefix in PATH contains a character ('%'), the path search is implementation-defined.

EDIT: PATH is empty, not unset

CPython versions tested on:

3.13

Operating systems tested on:

Linux

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

Parti dall’entry point shutil.which e riproduci il caso Linux con PATH=, confrontandolo con subprocess.run(['python', '-V']). Determina il comportamento previsto per un PATH vuoto, quindi verifica che shutil.which lo segua e copri il caso con un test di regressione.

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
42/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.