Skip to content

object.__init__ as slot caller #4884

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
52 changes: 50 additions & 2 deletions vm/src/builtins/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,11 +400,59 @@ impl PyBaseObject {
}

#[pyslot]
#[pymethod(magic)]
fn init(_zelf: PyObjectRef, _args: FuncArgs, _vm: &VirtualMachine) -> PyResult<()> {
fn slot_init(zelf: PyObjectRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult<()> {
let typ = zelf.class();
eprintln!("called init of {}", zelf.class().name());
if typ.slots.init.load().map_or(0, |f| f as usize)
== vm
.ctx
.types
.object_type
.slots
.init
.load()
.map_or(0, |f| f as usize)
{
return Ok(());
}
if !args.is_empty() {
// if typ.slots.init.load().map_or(0, |f| f as usize) != vm.ctx.types.object_type.slots.init.load().map_or(0, |f| f as usize) {
// return Err(vm.new_type_error(format!(
// "object.__init__() takes exactly one argument (the instance to initialize)",
// )));
// }
if typ.slots.new.load().map_or(0, |f| f as usize)
== vm
.ctx
.types
.object_type
.slots
.new
.load()
.map_or(0, |f| f as usize)
{
return Err(vm.new_type_error(format!(
"{:.200}.__init__() takes exactly one argument (the instance to initialize)",
typ.name()
)));
}
}
Ok(())
}

#[pymethod(magic)]
fn init(zelf: PyObjectRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult<()> {
if zelf.class().is(vm.ctx.types.object_type) {
return Ok(());
}
eprintln!("{:?}", zelf.class().name());
let init = zelf
.class()
.mro_find_map(|cls| cls.slots.init.load())
.unwrap();
(init)(zelf, args, vm)
}

#[pygetset(name = "__class__")]
fn get_class(obj: PyObjectRef) -> PyTypeRef {
obj.class().to_owned()
Expand Down
21 changes: 13 additions & 8 deletions vm/src/builtins/super.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@
(typ, obj)
};

let mut inner = PySuperInner::new(typ, obj, vm)?;
std::mem::swap(&mut inner, &mut zelf.inner.write());
let inner = PySuperInner::new(typ, obj, vm)?;
let _ = std::mem::replace::<PySuperInner>(&mut zelf.inner.write(), inner);

Ok(())
}
Expand Down Expand Up @@ -173,14 +173,19 @@
.skip(1) // skip su->type (if any)
.collect();
for cls in mro.iter() {
println!("traversing mro: {}", cls.name());
if let Some(descr) = cls.get_direct_attr(name) {
// Only pass 'obj' param if this is instance-mode super (See https://bugs.python.org/issue743267)
let obj = if obj.is(&start_type) {
None
} else {
println!("getattr {}.{} as {}", obj.class().name(), name, cls.name());

Some(obj)
};
let cls = cls.as_object().to_owned();
return vm
.call_get_descriptor_specific(
&descr,
// Only pass 'obj' param if this is instance-mode super (See https://bugs.python.org/issue743267)
if obj.is(&start_type) { None } else { Some(obj) },
Some(start_type.as_object().to_owned()),
)
.call_get_descriptor_specific(&descr, obj, Some(cls))
.unwrap_or(Ok(descr));
}
}
Expand Down Expand Up @@ -270,8 +275,8 @@
This works for class methods too:\n\
class C(B):\n \
@classmethod\n \
def cmeth(cls, arg):\n \

Check warning on line 278 in vm/src/builtins/super.rs

View workflow job for this annotation

GitHub Actions / Check Rust code with rustfmt and clippy

Unknown word (cmeth)
super().cmeth(arg)\n";

Check warning on line 279 in vm/src/builtins/super.rs

View workflow job for this annotation

GitHub Actions / Check Rust code with rustfmt and clippy

Unknown word (cmeth)

extend_class!(context, super_type, {
"__doc__" => context.new_str(super_doc),
Expand Down
3 changes: 3 additions & 0 deletions vm/src/builtins/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,9 @@ impl PyType {
let mut slot_name_set = std::collections::HashSet::new();

for cls in self.mro.read().iter() {
if !cls.slots.flags.contains(PyTypeFlags::HEAPTYPE) {
continue;
}
for &name in cls.attributes.read().keys() {
if name == identifier!(ctx, __new__) {
continue;
Expand Down
1 change: 0 additions & 1 deletion vm/src/stdlib/ast/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ pub(crate) mod _ast {
#[pyclass(flags(BASETYPE, HAS_DICT))]
impl NodeAst {
#[pyslot]
#[pymethod(magic)]
fn init(zelf: PyObjectRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult<()> {
let fields = zelf.get_attr("_fields", vm)?;
let fields: Vec<PyStrRef> = fields.try_to_value(vm)?;
Expand Down
7 changes: 1 addition & 6 deletions vm/src/stdlib/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1402,17 +1402,12 @@ mod _io {
#[pyslot]
fn slot_init(zelf: PyObjectRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult<()> {
let zelf: PyRef<Self> = zelf.try_into_value(vm)?;
zelf.__init__(args, vm)
}

#[pymethod]
fn __init__(&self, args: FuncArgs, vm: &VirtualMachine) -> PyResult<()> {
let (raw, BufferSize { buffer_size }): (PyObjectRef, _) =
args.bind(vm).map_err(|e| {
let msg = format!("{}() {}", Self::CLASS_NAME, *e.str(vm));
vm.new_exception_msg(e.class().to_owned(), msg)
})?;
self.init(raw, BufferSize { buffer_size }, vm)
zelf.init(raw, BufferSize { buffer_size }, vm)
}

fn init(
Expand Down
12 changes: 5 additions & 7 deletions vm/src/types/slot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,10 @@ fn descr_set_wrapper(
fn init_wrapper(obj: PyObjectRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult<()> {
let res = vm.call_special_method(&obj, identifier!(vm, __init__), args)?;
if !vm.is_none(&res) {
return Err(vm.new_type_error("__init__ must return None".to_owned()));
return Err(vm.new_type_error(format!(
"__init__ should return None, not '{:.200}'",
res.class().name()
)));
}
Ok(())
}
Expand Down Expand Up @@ -511,6 +514,7 @@ impl PyType {
update_slot!(descr_set, descr_set_wrapper);
}
_ if name == identifier!(ctx, __init__) => {
eprintln!("init of {:?}", self.name());
toggle_slot!(init, init_wrapper);
}
_ if name == identifier!(ctx, __new__) => {
Expand Down Expand Up @@ -829,12 +833,6 @@ pub trait Initializer: PyPayload {
Self::init(zelf, args, vm)
}

#[pymethod]
#[inline]
fn __init__(zelf: PyRef<Self>, args: Self::Args, vm: &VirtualMachine) -> PyResult<()> {
Self::init(zelf, args, vm)
}

fn init(zelf: PyRef<Self>, args: Self::Args, vm: &VirtualMachine) -> PyResult<()>;
}

Expand Down
Loading