StopIteration.value is stored in the instance dict instead of as a member · Issue #8290 · RustPython/RustPython · GitHub
Skip to content

StopIteration.value is stored in the instance dict instead of as a member #8290

Description

@kangdora

Feature

StopIteration.value is stored in the instance __dict__ instead of as a member (struct field) like CPython. This makes StopIteration().__dict__ non-empty and causes value to return the wrong result when the dict is cleared.

Same class of issue as SystemExit.code (#8230, fixed in #8282). Other built-in exceptions share it (AttributeError, ImportError, SyntaxError, UnicodeDecodeError); this issue targets StopIteration first.

Wrong value:

e = StopIteration(5)
e.__dict__.clear()
print(e.value)
# CPython 3.14: 5      (value is a member, unaffected by dict)
# RustPython:   None   (value lives in the dict, so it's gone)

Also observable:

type(StopIteration.value)   # CPython: member_descriptor   RustPython: NoneType
vars(StopIteration(5))      # CPython: {}                  RustPython: {'value': 5}

Cause: value is a class-level None set via set_attr into the instance dict in slot_init, instead of a struct field. The hot-path helper VirtualMachine::new_stop_iteration (crates/vm/src/vm/vm_new.rs) also allocates a fresh dict per call.

Proposed fix: Move value to a struct field on PyStopIteration (the #[repr(C)] + base-field approach used by PyOSError and now SystemExit in #8282), exposed via #[pygetset]. Since StopIteration is raised on a very hot path, this also removes the per-instance dict allocation.

I'll take this and open a PR, following the same approach as #8282.

Python Documentation or reference to CPython source code

https://docs.python.org/3/library/exceptions.html#StopIteration

value is defined as a PyMemberDef (a struct member, not an instance-dict entry) in CPython's Objects/exceptions.c.


Reproduction verified against CPython 3.14.6 and RustPython locally. Issue drafted with AI assistance (Claude), reviewed and edited by me.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

C-compatA discrepancy between RustPython and CPythonz-ca-2026Tag to track Contribution Academy 2026

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions