Skip to content

Panic when setting __class__ with different layout #4634

@qingshi163

Description

@qingshi163

Summary

Setting object's class attribute to a type with different layout cause panic

thread 'main' panicked at 'index out of bounds: the len is 0 but the index is 0', vm/src/object/core.rs:813:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

which is an error in cpython

TypeError: __class__ assignment: 'TypeB' object layout differs from 'TypeA'

Detailed Explanation

class TypeA:
    def __init__(self) -> None:
        self.a = 1

class TypeB:
    __slots__ = "b"
    def __init__(self) -> None:
        self.b = 2

obj = TypeA()
assert obj.a == 1

obj.__class__ = TypeB
print(obj.b)

Metadata

Metadata

Labels

RFCRequest for comments

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions