Skip to content
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
3 changes: 3 additions & 0 deletions tests/snippets/class.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,18 @@ def __init__(self, x):
self.x = x

def get_x(self):
assert __class__ is Bar
return self.x

@classmethod
def fubar(cls, x):
assert __class__ is cls
assert cls is Bar
assert x == 2

@staticmethod
def kungfu(x):
assert __class__ is Bar
assert x == 3


Expand Down
9 changes: 6 additions & 3 deletions vm/src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::obj::objtype;
use crate::frame::Scope;
use crate::function::{Args, OptionalArg, PyFuncArgs};
use crate::pyobject::{
AttributeProtocol, IdProtocol, PyContext, PyObjectRef, PyResult, TypeProtocol,
AttributeProtocol, DictProtocol, IdProtocol, PyContext, PyObjectRef, PyResult, TypeProtocol,
};
use crate::vm::VirtualMachine;

Expand Down Expand Up @@ -842,7 +842,10 @@ pub fn builtin_build_class_(vm: &mut VirtualMachine, mut args: PyFuncArgs) -> Py
let prepare = vm.get_attribute(metaclass.clone(), prepare_name)?;
let namespace = vm.invoke(prepare, vec![name_arg.clone(), bases.clone()])?;

vm.invoke_with_locals(function, namespace.clone())?;
let cells = vm.new_dict();

vm.call_method(&metaclass, "__call__", vec![name_arg, bases, namespace])
vm.invoke_with_locals(function, cells.clone(), namespace.clone())?;
let class = vm.call_method(&metaclass, "__call__", vec![name_arg, bases, namespace])?;
cells.set_item(&vm.ctx, "__class__", class.clone());
Ok(class)
}
11 changes: 9 additions & 2 deletions vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,14 +359,21 @@ impl VirtualMachine {
}
}

pub fn invoke_with_locals(&mut self, function: PyObjectRef, locals: PyObjectRef) -> PyResult {
pub fn invoke_with_locals(
&mut self,
function: PyObjectRef,
cells: PyObjectRef,
locals: PyObjectRef,
) -> PyResult {
if let Some(PyFunction {
code,
scope,
defaults: _,
}) = &function.payload()
{
let scope = scope.child_scope_with_locals(locals);
let scope = scope
.child_scope_with_locals(cells)
.child_scope_with_locals(locals);
let frame = self.ctx.new_frame(code.clone(), scope);
return self.run_frame_full(frame);
}
Expand Down