pkgutil.walk_packages(None) imports tests and then their SkipTest exceptions ends iteration
Chưa có ai nhận issue này.
- 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
I have to be clear that in some ways this isn't technically a bug per se, it's documented in the documentation, I just think the default behavior should be different because it's not ergonomic -- and that behavior change should be documented. It's a bug in the sense of I tried to do the simplest thing possible and Python was like "NOPE" and I was like "well that's gotta be a bug in Python," and I believe I am technically wrong but a lot of people in similar positions would assume the same.
Basic problem: Stdlib contains non-importable modules, if they fail with ImportError then pkgutil.walk_packages will ignore them, and those are the only packages it will ignore by default. But stdlib contains a bunch of non-importable test-case modules that instead fail with unittest.SkipTest. This means that any use of the default args on walk_packages() errors out, you have to manually tell it that SkipTest is a fine error to ignore.
Bug description:
Steps to Reproduce
Fire up python 3.13, then run the following two lines:
>>> import pkgutil
>>> [p.name for p in pkgutil.walk_packages()]
Traceback (most recent call last):
File "<python-input-7>", line 1, in <module>
[p.name for p in pkgutil.walk_packages()]
~~~~~~~~~~~~~~~~~~~~~^^
File "/opt/homebrew/Cellar/python@3.13/3.13.2/Frameworks/Python.framework/Versions/3.13/lib/python3.13/pkgutil.py", line 93, in walk_packages
yield from walk_packages(path, info.name+'.', onerror)
File "/opt/homebrew/Cellar/python@3.13/3.13.2/Frameworks/Python.framework/Versions/3.13/lib/python3.13/pkgutil.py", line 78, in walk_packages
__import__(info.name)
~~~~~~~~~~^^^^^^^^^^^
File "/opt/homebrew/Cellar/python@3.13/3.13.2/Frameworks/Python.framework/Versions/3.13/lib/python3.13/test/test_free_threading/__init__.py", line 8, in <module>
raise unittest.SkipTest("GIL enabled")
unittest.case.SkipTest: GIL enabled
OR, fire up homebrew's (or poetry's) python 3.12, then run the same:
Python 3.12.9 (main, Feb 4 2025, 14:38:38) [Clang 16.0.0 (clang-1600.0.26.6)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pkgutil
>>> [p.name for p in pkgutil.walk_packages()]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/homebrew/Cellar/python@3.12/3.12.9/Frameworks/Python.framework/Versions/3.12/lib/python3.12/pkgutil.py", line 93, in walk_packages
yield from walk_packages(path, info.name+'.', onerror)
File "/opt/homebrew/Cellar/python@3.12/3.12.9/Frameworks/Python.framework/Versions/3.12/lib/python3.12/pkgutil.py", line 78, in walk_packages
__import__(info.name)
File "/opt/homebrew/Cellar/python@3.12/3.12.9/Frameworks/Python.framework/Versions/3.12/lib/python3.12/test/test_gdb/__init__.py", line 22, in <module>
raise unittest.SkipTest("test_gdb only works on source builds at the moment.")
unittest.case.SkipTest: test_gdb only works on source builds at the moment.
Cause
Seems to be pretty obvious, these are test modules that exist all the time (whether GIL is enabled or not, whether debugger is enabled or not) but they raise an exception when they get imported.
Suggestions
pkgutil should be able to catch and discard unittest.SkipTest.
The easiest way to do this is to make unittest.SkipTest a subclass of ImportError instead of Exception, but that "smells risky" (in particular it presumably isn't only thrown when people import modules). The smarter way to do this is to just document that we by default don't error on SkipTest either and adjust the code at https://github.com/python/cpython/blob/v3.13.2/Lib/pkgutil.py#L82 accordingly.
CPython versions tested on:
3.11.11, 3.12.9, 3.13.2
Operating systems tested on:
macOS
Linked PRs
- gh-148926
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 với Lib/pkgutil.py quanh dòng 82 và tái hiện lỗi bằng cách sử dụng pkgutil.walk_packages() trên các package kiểm thử Python được mô tả trong issue. Xem xét PR được liên kết gh-148926 cùng với tài liệu và các bài kiểm thử liên quan; công việc được xem là hoàn tất khi hành vi SkipTest đã thống nhất được triển khai và ghi lại trong tài liệu mà không làm hỏng cách xử lý lỗi hiện có.
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
- backend
- Loại issue
- Lỗi
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 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