python / python/cpython

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

Đang mở
#141,041 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

stdlib type-bug
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu từ entry point shutil.which và tái hiện trường hợp Linux với PATH=, so sánh với subprocess.run(['python', '-V']). Xác định hành vi dự kiến đối với PATH rỗng, sau đó xác minh rằng shutil.which tuân theo hành vi đó và bao phủ trường hợp này bằng một regression test.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
operating-systems
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
42/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.