`lazy import a.b as c` incorrectly binds to `a.b.b`

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

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

Đánh giá

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

Hướng nghiên cứu

Bắt đầu bằng cách kiểm tra PR được liên kết gh-157626, sau đó kiểm tra _PyEval_LazyImportFrom trong Python/ceval.c quanh các dòng 3305–3361, tập trung vào fast path của sys.modules và việc phân giải tên được lưu trong bộ nhớ đệm. Tái hiện hành vi bằng ví dụ importlib.metadata; được xem là hoàn tất khi lazy import a.b as c liên kết c với a.b thay vì a.b.b và hồi quy được bao phủ bởi các bài kiểm thử liên quan.

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

Mô tả

3.15 3.16 interpreter-core release-blocker topic-lazy-imports type-bug

Bug report

(Edited by @johnslavik)

To reproduce:

import importlib.metadata
import importlib.metadata as x
lazy import importlib.metadata as lazy_x

print("type(x):     ", type(x).__name__)  # reifies to importlib.metadata
print("type(lazy_x):", type(lazy_x).__name__)  # reifies to importlib.metadata.metadata

x.distributions()  # OK
lazy_x.distributions()  # AttributeError: 'function' object has no attribute 'distributions'

x is the importlib.metadata module, as expected. lazy_x is instead reified to importlib.metadata.metadata, the function.

The bug is in cached name resolution.

For lazy import a.b as c, the issue reproduces when:

  1. a.b is already in sys.modules.
  2. a.b has an attribute named b.
  3. c is reified.
Possible root cause

The problem may be in _PyEval_LazyImportFrom: [Python/ceval.c:3305-3361](https://github.com/python/cpython/blob/cf312cfa64dcaa1700531ec8bc76d56ddd65fb15/Python/ceval.c#L3305-L3361) (3.15 at cf312cfa64dcaa1700531ec8bc76d56ddd65fb15, v3.15.0rc2-22-gcf312cf).

The sys.modules fast path at lines 3313-3331 runs before the base-module computation at 3333-3360 and looks up name directly in d->lz_from.

In the IMPORT_FROM chain generated for import a.b.c as x, however, d->lz_from does not appear to be the module the attribute should be taken from: on the first step it is the full dotted name, and on later steps it is one component short of the base.

CPython versions tested on:

3.15

Operating systems tested on:

Linux

Linked PRs
  • gh-157626
Ngôn ngữ chính
Python
Star
77.2k
Fork
36k
Merge trung bình
1 ngày 9 giờ
Pull request đã merge (30 ngày)
558

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.

Issue khác của python/cpython

Tất cả issue của python/cpython

Issue tương tự

Thêm issue về Python

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.