pathlib: Path('.').exists() returns True when current working directory (cwd) was deleted
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 77.2k
- Fork
- 36k
- Chỉ số merge pull request
- Chỉ số pull request đang chờ
Mô tả
Bug report
Bug description:
Path.exists() and Path.resolve() do not work consistently with each other if the current working directory is deleted after Python is launched.
For example, if I open a Python 3.13.0 interpreter (although I have verified this occurs in older versions as well) in a directory, and then delete that directory I experience the following behavior:
>>> from pathlib import Path
>>> Path('.').exists()
True
>>> Path('.').resolve()
Traceback (most recent call last):
File "<python-input-2>", line 1, in <module>
Path('.').resolve()
~~~~~~~~~~~~~~~~~^^
File "/opt/homebrew/Caskroom/mambaforge/base/envs/py313minimal/lib/python3.13/pathlib/_local.py", line 670, in resolve
return self.with_segments(os.path.realpath(self, strict=strict))
~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
File "<frozen posixpath>", line 413, in realpath
FileNotFoundError: [Errno 2] No such file or directory
Note how Path('.').exists() is True, even though the cwd does not exist, and then when I try to resolve the path, I get a FileNotFoundError.
You also get a FileNotFoundError when the cwd doesn't exist for all relative paths, even though in the non-dot cases Path.exists() works as one would expect.
>>> Path('asdf').exists()
False
>>> Path('asdf').resolve().exists()
Traceback (most recent call last):
File "<python-input-4>", line 1, in <module>
Path('asdf').resolve().exists()
~~~~~~~~~~~~~~~~~~~~^^
File "/opt/homebrew/Caskroom/mambaforge/base/envs/py313minimal/lib/python3.13/pathlib/_local.py", line 670, in resolve
return self.with_segments(os.path.realpath(self, strict=strict))
~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
File "<frozen posixpath>", line 413, in realpath
FileNotFoundError: [Errno 2] No such file or directory
If the cwd does exist, you can see that Path.resolve() does not raise a FileNotFoundError when the path does not exist.
>>> from pathlib import Path
>>> Path('asdf').exists()
False
>>> Path('asdf').resolve()
PosixPath('/Users/danielblanchard/asdf')
I believe resolve is doing the right thing here, but two things are not working as expected when the cwd is deleted:
Path('.').exists()andPath('').exists()should not tell you a path exists that doesn't.Path('foo').resolve()should not raise aFileNotFoundErrorif the cwd doesn't exist, unlessstrict=True. Instead it should just return the initial path.
CPython versions tested on:
3.9, 3.12, 3.13
Operating systems tested on:
Linux, macOS
Linked PRs
- gh-135531
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu trong pathlib/_local.py tại Path.exists() và Path.resolve(), sau đó theo dõi việc chúng sử dụng os.path.realpath cho các đường dẫn tương đối khi thư mục làm việc hiện tại đã bị xóa. So sánh hành vi được báo cáo đối với '.', '' và 'foo'; được xem là hoàn tất khi exists() không còn báo cáo thư mục làm việc hiện tại đã bị xóa và resolve() chỉ phát sinh FileNotFoundError khi strict=True, với gh-135531 cho biết công việc liên quan đã được tiến hành.
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
- 25/100