Skip to content

Allow update __class__ for mutable types or module subclasses #5225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions Lib/test/test_opcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,6 @@ def f():
for _ in range(1025):
self.assertTrue(f())

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_metaclass_swap(self):
class OldMetaclass(type):
@property
Expand Down Expand Up @@ -411,8 +409,6 @@ def f():
for _ in range(1025):
self.assertTrue(f())

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_metaclass_swap(self):
class OldMetaclass(type):
@property
Expand Down
3 changes: 3 additions & 0 deletions derive-impl/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1497,6 +1497,9 @@ fn extract_impl_attrs(attr: AttributeArgs, item: &Ident) -> Result<ExtractedImpl
.union(::rustpython_vm::types::PyTypeFlags::#ident)
});
}
flags.push(quote! {
.union(::rustpython_vm::types::PyTypeFlags::IMMUTABLETYPE)
});
} else {
bail_span!(path, "Unknown pyimpl attribute")
}
Expand Down
17 changes: 15 additions & 2 deletions vm/src/builtins/object.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::{PyDictRef, PyList, PyStr, PyStrRef, PyType, PyTypeRef};
use crate::common::hash::PyHash;
use crate::types::PyTypeFlags;
use crate::{
class::PyClassImpl,
function::{Either, FuncArgs, PyArithmeticValue, PyComparisonValue, PySetterValue},
Expand Down Expand Up @@ -281,7 +282,18 @@ impl PyBaseObject {

#[pygetset(name = "__class__", setter)]
fn set_class(instance: PyObjectRef, value: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
if instance.payload_is::<PyBaseObject>() {
let both_module = instance.class().fast_issubclass(vm.ctx.types.module_type)
&& value.class().fast_issubclass(vm.ctx.types.module_type);
let both_mutable = !instance
.class()
.slots
.flags
.has_feature(PyTypeFlags::IMMUTABLETYPE)
&& !value
.downcast_ref::<PyType>()
.map(|t| t.slots.flags.has_feature(PyTypeFlags::IMMUTABLETYPE))
.unwrap_or(false);
if both_mutable || both_module {
match value.downcast::<PyType>() {
Ok(cls) => {
// FIXME(#1979) cls instances might have a payload
Expand All @@ -298,7 +310,8 @@ impl PyBaseObject {
}
} else {
Err(vm.new_type_error(
"__class__ assignment only supported for types without a payload".to_owned(),
"__class__ assignment only supported for mutable types or ModuleType subclasses"
.to_owned(),
))
}
}
Expand Down