Skip to content

Add __class__ cell to method scopes. #703

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 1 commit into from
Mar 20, 2019
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
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy