Duck-typing in `inspect.getfile`
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ả
Running inspect.getsource(numpy.random.random_sample) yields
File ~\.conda\envs\sage-dev\Lib\inspect.py:943, in getfile(object)
941 if iscode(object):
942 return object.co_filename
--> 943 raise TypeError('module, class, method, function, traceback, frame, or '
944 'code object was expected, got {}'.format(
945 type(object).__name__))
since the cython function is not correct recognized as a "true" function (i.e. it is not derived from FunctionType). But since a Cython function correct provides __code__, all the information is actually there to properly get the source file.
While it is quite possible to emulate the interface of functions in Cython's own function type, and recent Cython releases have seen several improvements here, the "inspect" module does not consider a Cython implemented function a "function", because it tests the object type explicitly instead of comparing an abstract interface or an abstract base class. This has a negative impact on code that uses inspect to inspect function objects, but would require a change to Python itself.
and
At the very least, the inspect module should use more duck-typing internally. For example, consider this code from "getfile":
if ismethod(object): object = object.__func__ if isfunction(object): object = object.__code__ if istraceback(object): object = object.tb_frame if isframe(object): object = object.f_code if iscode(object): return object.co_filename
Originally posted by @jdemeyer in #74257
Linked PRs
- gh-131632
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 inspect.getfile và các kiểm tra isfunction/iscode được nêu trong issue. So sánh cách các đối tượng cung cấp code được xử lý, sau đó xem xét PR được liên kết gh-131632 trước khi thực hiện bất kỳ công việc nào. Được xem là hoàn tất khi hàm Cython được báo cáo có thể xác định tệp mã nguồn của nó mà không làm hỏng hành vi hiện có của inspect.getfile.
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
- devtools
- Loại issue
- Tính năng
- Độ 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