[Bug]: `collections._count_elements` looks up `mapping.get` before calling `iter()` on the iterable
Maintainer antworten meist innerhalb von 1 Tag
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Anfängerfreundlichkeit
- 62/100
Rechercherichtung
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.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
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.
- Vorherrschende Sprache
- Python
- Sterne
- 1.7k
- Forks
- 167
- Ø Merge
- 3 T. 11 Std.
- Gemergte PRs (30 T.)
- 68
Entwicklungsumgebung
Startet den Dev-Container des Projekts im Browser, mit Ihrem eigenen GitHub-Konto.
- Kein Dockerfile und keine Docker-Compose-Datei
- Keine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus oracle/graalpython
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 74/100
oracle/graalpython#1105 ·
Maintainer antworten meist innerhalb von 1 Tag
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
oracle/graalpython#1102 ·
Maintainer antworten meist innerhalb von 1 Tag
-
mmap.mmap.move() is missingEvtl. vergeben @deronnax hat das vor 3 Tagen übernommen. Offen
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 62/100
oracle/graalpython#1368 ·
Maintainer antworten meist innerhalb von 1 Tag
-
mmap extended slicing ignores the step; negative steps raise java.lang.IndexOutOfBoundsExceptionOffen
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 64/100
oracle/graalpython#1367 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 22/100
oracle/graalpython#1366 ·
Maintainer antworten meist innerhalb von 1 Tag
Alle Issues in oracle/graalpython
Ähnliche Issues
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
Maintainer antworten meist innerhalb von 1 Tag
-
bug improve content
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 92/100
opensafely/documentation#2102 ·
Maintainer antworten meist innerhalb von 1 Tag
-
difficulty:1 documentation
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
MakazhanAlpamys/Soup#3533 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 74/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
onyx-dot-app/onyx#18730 ·
Maintainer antworten meist innerhalb von 1 Tag