[Bug]: `collections._count_elements` looks up `mapping.get` before calling `iter()` on the iterable

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

Maintainer thường phản hồi trong vòng 1 ngày

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

Đánh giá

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

Hướng nghiên cứu

Start by running the reproduction snippet against GraalPy to confirm the AttributeError. The fallback _count_elements is in graalpython/lib-python/3/collections/init.py around lines 537-546, and the _collections module is defined in CollectionsModuleBuiltins.java, which does not yet register it. Done means the snippet raises the same TypeError as CPython 3.13 ("iter() returned non-iterator") and _collections._count_elements is importable with the iterator obtained before mapping.get is looked up.

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

Mô tả

bug
Describe the bug

_collections has no _count_elements, so collections._count_elements is the pure-Python fallback in collections/__init__.py. That fallback reads mapping.get before it iterates, while CPython's C helper gets the iterator first. When both arguments are bad, GraalPy raises the mapping's AttributeError and CPython raises the iterable's error.

Operating system

macOS

CPU architecture

ARM64

GraalPy version

master a3009f9f52 (Python 3.13.14); also reproduced on 25.0.2 (Python 3.12.8)

JDK version

No response

Context configuration

No response

Steps to reproduce
import collections

class NoneIter:
    def __iter__(self):
        return None

collections._count_elements(None, NoneIter())

GraalPy master a3009f9f52:

AttributeError: 'NoneType' object has no attribute 'get'
Expected behavior

CPython 3.13.14 (same on 3.12.13):

TypeError: iter() returned non-iterator of type 'NoneType'
Stack trace

Additional context

CPython's own pure-Python fallback behaves the same way when _collections is blocked, and Counter.update() always passes a mapping that has get, so only direct callers of the private helper see the difference. GraalPy already patches torch._dynamo to substitute collections._count_elements because _collections._count_elements is missing (torch-2.10.0.patch#L540-L553).

Root Cause

collections/__init__.py#L537-L546 evaluates mapping.get and only then iterates, and from _collections import _count_elements fails because CollectionsModuleBuiltins (CollectionsModuleBuiltins.java#L51-L52) does not define it. CPython's _collections__count_elements_impl calls PyObject_GetIter(iterable) first (_collectionsmodule.c#L2514-L2516) and looks up get afterwards (#L2575).

Fix Suggestion

Provide _count_elements in _collections that gets the iterator before looking up mapping.get.

Ngôn ngữ chính
Python
Star
1.7k
Fork
167
Merge trung bình
3 ngày 11 giờ
Pull request đã merge (30 ngày)
68

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

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 oracle/graalpython

Tất cả issue của oracle/graalpython

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.