Skip to content

[WIP]Convert method __new__ to staticmethod #1241

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

Closed
wants to merge 1 commit into from
Closed
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
16 changes: 8 additions & 8 deletions vm/src/obj/objstaticmethod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use super::objtype::PyClassRef;
use crate::pyobject::{PyContext, PyObjectRef, PyRef, PyResult, PyValue};
use crate::pyobject::{PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue};
use crate::vm::VirtualMachine;

#[pyclass]
#[derive(Clone, Debug)]
pub struct PyStaticMethod {
pub callable: PyObjectRef,
Expand All @@ -14,7 +15,9 @@ impl PyValue for PyStaticMethod {
}
}

impl PyStaticMethodRef {
#[pyimpl]
impl PyStaticMethod {
#[pymethod(name = "__new__")]
fn new(
cls: PyClassRef,
callable: PyObjectRef,
Expand All @@ -26,15 +29,12 @@ impl PyStaticMethodRef {
.into_ref_with_type(vm, cls)
}

fn get(self, _inst: PyObjectRef, _owner: PyObjectRef, _vm: &VirtualMachine) -> PyResult {
#[pymethod(name = "__get__")]
fn get(&self, _inst: PyObjectRef, _owner: PyObjectRef, _vm: &VirtualMachine) -> PyResult {
Ok(self.callable.clone())
}
}

pub fn init(context: &PyContext) {
let staticmethod_type = &context.staticmethod_type;
extend_class!(context, staticmethod_type, {
"__get__" => context.new_rustfunc(PyStaticMethodRef::get),
"__new__" => context.new_rustfunc(PyStaticMethodRef::new),
});
PyStaticMethod::extend_class(context, &context.staticmethod_type);
}
15 changes: 14 additions & 1 deletion vm/src/pyobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use crate::obj::objproperty::PropertyBuilder;
use crate::obj::objrange;
use crate::obj::objset::{self, PySet};
use crate::obj::objslice;
use crate::obj::objstaticmethod;
use crate::obj::objstaticmethod::{self, PyStaticMethod};
use crate::obj::objstr;
use crate::obj::objsuper;
use crate::obj::objtuple::{self, PyTuple, PyTupleRef};
Expand Down Expand Up @@ -716,6 +716,19 @@ impl PyContext {
)
}

pub fn new_staticmethod<F, T, R>(&self, f: F) -> PyObjectRef
where
F: IntoPyNativeFunc<T, R>,
{
PyObject::new(
PyStaticMethod {
callable: self.new_rustfunc(f),
},
self.staticmethod_type(),
None,
)
}

pub fn new_classmethod<F, T, R>(&self, f: F) -> PyObjectRef
where
F: IntoPyNativeFunc<T, R>,
Expand Down