Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Merge branch 'master' into coolreader18/immutable-vm-ref
  • Loading branch information
coolreader18 authored Mar 22, 2019
commit b0d89357318d7ebdf585f4cd0cbb5f2efbcffcc3
36 changes: 2 additions & 34 deletions vm/src/obj/objclassmethod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ impl PyClassMethodRef {
fn new(
cls: PyClassRef,
callable: PyObjectRef,
vm: &mut VirtualMachine,
vm: &VirtualMachine,
) -> PyResult<PyClassMethodRef> {
PyClassMethod {
callable: callable.clone(),
}
.into_ref_with_type(vm, cls)
}

fn get(self, _inst: PyObjectRef, owner: PyObjectRef, vm: &mut VirtualMachine) -> PyResult {
fn get(self, _inst: PyObjectRef, owner: PyObjectRef, vm: &VirtualMachine) -> PyResult {
Ok(vm
.ctx
.new_bound_method(self.callable.clone(), owner.clone()))
Expand All @@ -40,35 +40,3 @@ pub fn init(context: &PyContext) {
"__new__" => context.new_rustfunc(PyClassMethodRef::new)
});
}

fn classmethod_get(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
trace!("classmethod.__get__ {:?}", args.args);
arg_check!(
vm,
args,
required = [
(cls, Some(vm.ctx.classmethod_type())),
(_inst, None),
(owner, None)
]
);
match cls.get_attr("function") {
Some(function) => {
let py_obj = owner.clone();
let py_method = vm.ctx.new_bound_method(function, py_obj);
Ok(py_method)
}
None => Err(vm.new_attribute_error(
"Attribute Error: classmethod must have 'function' attribute".to_string(),
)),
}
}

fn classmethod_new(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
trace!("classmethod.__new__ {:?}", args.args);
arg_check!(vm, args, required = [(cls, None), (callable, None)]);

let py_obj = vm.ctx.new_instance(cls.clone(), None);
vm.ctx.set_attr(&py_obj, "function", callable.clone());
Ok(py_obj)
}
35 changes: 3 additions & 32 deletions vm/src/obj/objstaticmethod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct PyStaticMethod {
pub type PyStaticMethodRef = PyRef<PyStaticMethod>;

impl PyValue for PyStaticMethod {
fn class(vm: &mut VirtualMachine) -> PyObjectRef {
fn class(vm: &VirtualMachine) -> PyObjectRef {
vm.ctx.staticmethod_type()
}
}
Expand All @@ -18,15 +18,15 @@ impl PyStaticMethodRef {
fn new(
cls: PyClassRef,
callable: PyObjectRef,
vm: &mut VirtualMachine,
vm: &VirtualMachine,
) -> PyResult<PyStaticMethodRef> {
PyStaticMethod {
callable: callable.clone(),
}
.into_ref_with_type(vm, cls)
}

fn get(self, _inst: PyObjectRef, _owner: PyObjectRef, _vm: &mut VirtualMachine) -> PyResult {
fn get(self, _inst: PyObjectRef, _owner: PyObjectRef, _vm: &VirtualMachine) -> PyResult {
Ok(self.callable.clone())
}
}
Expand All @@ -38,32 +38,3 @@ pub fn init(context: &PyContext) {
"__new__" => context.new_rustfunc(PyStaticMethodRef::new),
});
}

// `staticmethod` methods.
fn staticmethod_get(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
trace!("staticmethod.__get__ {:?}", args.args);
arg_check!(
vm,
args,
required = [
(cls, Some(vm.ctx.staticmethod_type())),
(_inst, None),
(_owner, None)
]
);
match cls.get_attr("function") {
Some(function) => Ok(function),
None => Err(vm.new_attribute_error(
"Attribute Error: staticmethod must have 'function' attribute".to_string(),
)),
}
}

fn staticmethod_new(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
trace!("staticmethod.__new__ {:?}", args.args);
arg_check!(vm, args, required = [(cls, None), (callable, None)]);

let py_obj = vm.ctx.new_instance(cls.clone(), None);
vm.ctx.set_attr(&py_obj, "function", callable.clone());
Ok(py_obj)
}
2 changes: 1 addition & 1 deletion vm/src/stdlib/re.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::vm::VirtualMachine;

impl PyValue for Regex {
fn class(vm: &VirtualMachine) -> PyObjectRef {
vm.import("re").unwrap().get_attr("Pattern").unwrap()
vm.class("re", "Pattern")
}
}

Expand Down
9 changes: 5 additions & 4 deletions vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,11 @@ impl VirtualMachine {
}

pub fn class(&self, module: &str, class: &str) -> PyObjectRef {
self.import(module)
.unwrap_or_else(|_| panic!("unable to import {}", module))
.get_attr(class)
.unwrap_or_else(|| panic!("module {} has no class {}", module, class))
let module = self
.import(module)
.unwrap_or_else(|_| panic!("unable to import {}", module));
self.get_attribute(module.clone(), class)
.unwrap_or_else(|_| panic!("module {} has no class {}", module, class))
}

/// Create a new python string object.
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.