diff --git a/vm/src/exceptions.rs b/vm/src/exceptions.rs index 5552eb154..c284ae489 100644 --- a/vm/src/exceptions.rs +++ b/vm/src/exceptions.rs @@ -83,7 +83,7 @@ impl PyBaseException { } #[pyproperty(name = "__traceback__", setter)] - fn setter_traceback(&self, traceback: Option, _vm: &VirtualMachine) { + pub fn set_traceback(&self, traceback: Option) { self.traceback.replace(traceback); } @@ -154,9 +154,6 @@ impl PyBaseException { pub fn traceback(&self) -> Option { self.traceback.borrow().clone() } - pub fn set_traceback(&self, tb: Option) { - self.traceback.replace(tb); - } pub fn cause(&self) -> Option { self.cause.borrow().clone() diff --git a/vm/src/function.rs b/vm/src/function.rs index 41552de72..208215103 100644 --- a/vm/src/function.rs +++ b/vm/src/function.rs @@ -498,11 +498,11 @@ pub type PyNativeFunc = Box PyResult + 's /// /// A bare `PyNativeFunc` also implements this trait, allowing the above to be /// done manually, for rare situations that don't fit into this model. -pub trait IntoPyNativeFunc { +pub trait IntoPyNativeFunc { fn into_func(self) -> PyNativeFunc; } -impl IntoPyNativeFunc for F +impl IntoPyNativeFunc for F where F: Fn(&VirtualMachine, PyFuncArgs) -> PyResult + 'static, { @@ -520,7 +520,7 @@ pub struct RefParam(std::marker::PhantomData); // Note that this could be done without a macro - it is simply to avoid repetition. macro_rules! into_py_native_func_tuple { ($(($n:tt, $T:ident)),*) => { - impl IntoPyNativeFunc<($(OwnedParam<$T>,)*), R> for F + impl IntoPyNativeFunc<($(OwnedParam<$T>,)*), R, VirtualMachine> for F where F: Fn($($T,)* &VirtualMachine) -> R + 'static, $($T: FromArgs,)* @@ -535,7 +535,7 @@ macro_rules! into_py_native_func_tuple { } } - impl IntoPyNativeFunc<(RefParam, $(OwnedParam<$T>,)*), R> for F + impl IntoPyNativeFunc<(RefParam, $(OwnedParam<$T>,)*), R, VirtualMachine> for F where F: Fn(&S, $($T,)* &VirtualMachine) -> R + 'static, S: PyValue, @@ -550,6 +550,29 @@ macro_rules! into_py_native_func_tuple { }) } } + + impl IntoPyNativeFunc<($(OwnedParam<$T>,)*), R, ()> for F + where + F: Fn($($T,)*) -> R + 'static, + $($T: FromArgs,)* + R: IntoPyObject, + { + fn into_func(self) -> PyNativeFunc { + IntoPyNativeFunc::into_func(move |$($n,)* _vm: &VirtualMachine| (self)($($n,)*)) + } + } + + impl IntoPyNativeFunc<(RefParam, $(OwnedParam<$T>,)*), R, ()> for F + where + F: Fn(&S, $($T,)*) -> R + 'static, + S: PyValue, + $($T: FromArgs,)* + R: IntoPyObject, + { + fn into_func(self) -> PyNativeFunc { + IntoPyNativeFunc::into_func(move |zelf: &S, $($n,)* _vm: &VirtualMachine| (self)(zelf, $($n,)*)) + } + } }; } diff --git a/vm/src/obj/objproperty.rs b/vm/src/obj/objproperty.rs index f01482b7c..a5e65e12a 100644 --- a/vm/src/obj/objproperty.rs +++ b/vm/src/obj/objproperty.rs @@ -265,7 +265,7 @@ impl<'a> PropertyBuilder<'a> { } } - pub fn add_getter>(self, func: F) -> Self { + pub fn add_getter>(self, func: F) -> Self { let func = self.ctx.new_rustfunc(func); Self { ctx: self.ctx, @@ -274,7 +274,7 @@ impl<'a> PropertyBuilder<'a> { } } - pub fn add_setter>( + pub fn add_setter>( self, func: F, ) -> Self { diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index 0fe1cdd17..7c4260453 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -460,9 +460,9 @@ impl PyContext { PyObject::new(PyNamespace, self.namespace_type(), Some(self.new_dict())) } - pub fn new_rustfunc(&self, f: F) -> PyObjectRef + pub fn new_rustfunc(&self, f: F) -> PyObjectRef where - F: IntoPyNativeFunc, + F: IntoPyNativeFunc, { PyObject::new( PyBuiltinFunction::new(f.into_func()), @@ -471,9 +471,9 @@ impl PyContext { ) } - pub fn new_classmethod(&self, f: F) -> PyObjectRef + pub fn new_classmethod(&self, f: F) -> PyObjectRef where - F: IntoPyNativeFunc, + F: IntoPyNativeFunc, { PyObject::new( PyClassMethod { @@ -484,9 +484,9 @@ impl PyContext { ) } - pub fn new_property(&self, f: F) -> PyObjectRef + pub fn new_property(&self, f: F) -> PyObjectRef where - F: IntoPyNativeFunc, + F: IntoPyNativeFunc, { PropertyBuilder::new(self).add_getter(f).create() } diff --git a/vm/src/stdlib/os.rs b/vm/src/stdlib/os.rs index 2aef879f4..e0cef4034 100644 --- a/vm/src/stdlib/os.rs +++ b/vm/src/stdlib/os.rs @@ -1247,7 +1247,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { follow_symlinks: Option, }; impl<'a> SupportFunc<'a> { - fn new( + fn new( vm: &VirtualMachine, name: &'a str, func: F, @@ -1256,7 +1256,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { follow_symlinks: Option, ) -> Self where - F: IntoPyNativeFunc, + F: IntoPyNativeFunc, { let func_obj = vm.ctx.new_rustfunc(func); Self {