RustPython / RustPython

A Python Interpreter written in Rust
https://rustpython.github.io
MIT License
19.13k stars 1.24k forks source link

Inconsist with cpython in deleting __dict__ #5355

Open Mskxn opened 3 months ago

Mskxn commented 3 months ago

In CPython, the following code can execute normally.

class Foo: pass
f = Foo()
print(f.__dict__)
del f.__dict__

But in rustpython, it triggers TypeError:

Traceback (most recent call last):
File "temp.py", line 4, in <module>
                  del f.__dict__
TypeError: can't delete attribute

I am not sure if it is a design or a bug. P.S., Are there any docs to explain the consistency between CPython and rustpython, or they should be exactly the same?

youknowone commented 3 months ago

I expect that eventually be the same.

I never expect this is possible in CPython. So this is the way to "clear" __dict__.

Mskxn commented 3 months ago

It seems that del __dict__ is always possible in CPython (I have tried on CPython 3.10), but some relative tests have been introduced recently (3.13): https://github.com/python/cpython/blob/81fd625b5c30cc6f417c93bad404923676ad8ca3/Lib/test/test_class.py#L817

youknowone commented 3 months ago

For contributors: current object_set_dict function signature doesn't support delete. Changing it to pub fn object_set_dict(obj: PyObjectRef, dict: PySetterValue<PyDictRef>, vm: &VirtualMachine) -> PyResult<()> is the starting point. The remaining steps will be straight-forward by understanding how PySetterValue looks like.