py/with macro doesn't bind the return value of __enter__ method
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 58/100
Hướng nghiên cứu
Bắt đầu với src/libpython_clj2/python/with.clj#L45-L63 và test hiện có trong test/libpython_clj2/python_test.clj#L167. Bổ sung coverage bằng cách sử dụng một context manager có enter trả về một object khác, sau đó xác minh rằng giá trị được bind là giá trị trả về đó và behavior hiện có của with vẫn pass.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
The py/with macro implementation doesn't correctly follow Python's with statement semantics. According to the Python documentation, the value bound in a with statement should be the return value of calling __enter__() on the context manager:
manager = (EXPRESSION)
enter = type(manager).__enter__
exit = type(manager).__exit__
value = enter(manager) # This value should be bound to TARGET
hit_except = False
try:
TARGET = value # TARGET gets the return value of enter(manager)
SUITE
...
Current Implementation
The current macro (with.clj#L45-L63) calls __enter__ but discards its return value:
(py-fn/call-attr ~varname "__enter__" nil) ; Return value is ignored
(try
(let [retval# (do ~@body)]
...
Expected Behavior
The bound variable should receive the return value of __enter__(), not the context manager itself:
(let [enter-result# (py-fn/call-attr ~varname "__enter__" nil)]
(try
(let [~varname enter-result# ; Rebind to __enter__'s return value
retval# (do ~@body)]
...
Why Current Tests Pass
The existing test in python_test.clj#L167 uses a WithObjClass where __enter__() returns None (implicitly), and the test methods are called on the original object. This masks the bug.
Real-World Example
A common Python pattern that fails with the current implementation:
with open('file.txt', 'r') as f:
content = f.read() # f should be the file object returned by __enter__
With the current macro, f would be bound to the unopened file path/manager rather than the file object returned by __enter__().
Next Steps
- Add test cases that verify this behavior (e.g., update
WithObjClass.__enter__to return a different object, or test with Python's built-in file context manager) - Update the macro to correctly bind the return value of
__enter__()
- Ngôn ngữ chính
- Clojure
- Star
- 1.2k
- Fork
- 74
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
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.
Issue khác của clj-python/libpython-clj
-
upgrade to dtype-next 11.xxx Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 48/100
clj-python/libpython-clj#280 ·
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 38/100
clj-python/libpython-clj#275 · 5 bình luận ·
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 25/100
clj-python/libpython-clj#271 · 1 bình luận ·
-
"auto flush" tty optionally Đang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
clj-python/libpython-clj#270 · 1 bình luận ·
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 42/100
clj-python/libpython-clj#269 · 3 bình luận ·
Tất cả issue của clj-python/libpython-clj
Issue tương tự
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
-
.Team/Metabot Priority:P3
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
-
needs triage
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100