Skip to content

Commit 2428996

Browse files
authored
Merge pull request RustPython#1649 from youknowone/func-vm
Allow to skip VM param for IntoPyNativeFunc functions
2 parents 9fdf54f + 92ba35d commit 2428996

File tree

5 files changed

+38
-18
lines changed

5 files changed

+38
-18
lines changed

vm/src/exceptions.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl PyBaseException {
8484
}
8585

8686
#[pyproperty(name = "__traceback__", setter)]
87-
fn setter_traceback(&self, traceback: Option<PyTracebackRef>, _vm: &VirtualMachine) {
87+
pub fn set_traceback(&self, traceback: Option<PyTracebackRef>) {
8888
self.traceback.replace(traceback);
8989
}
9090

@@ -155,9 +155,6 @@ impl PyBaseException {
155155
pub fn traceback(&self) -> Option<PyTracebackRef> {
156156
self.traceback.borrow().clone()
157157
}
158-
pub fn set_traceback(&self, tb: Option<PyTracebackRef>) {
159-
self.traceback.replace(tb);
160-
}
161158

162159
pub fn cause(&self) -> Option<PyBaseExceptionRef> {
163160
self.cause.borrow().clone()

vm/src/function.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -498,11 +498,11 @@ pub type PyNativeFunc = Box<dyn Fn(&VirtualMachine, PyFuncArgs) -> PyResult + 's
498498
///
499499
/// A bare `PyNativeFunc` also implements this trait, allowing the above to be
500500
/// done manually, for rare situations that don't fit into this model.
501-
pub trait IntoPyNativeFunc<T, R> {
501+
pub trait IntoPyNativeFunc<T, R, VM> {
502502
fn into_func(self) -> PyNativeFunc;
503503
}
504504

505-
impl<F> IntoPyNativeFunc<PyFuncArgs, PyResult> for F
505+
impl<F> IntoPyNativeFunc<PyFuncArgs, PyResult, VirtualMachine> for F
506506
where
507507
F: Fn(&VirtualMachine, PyFuncArgs) -> PyResult + 'static,
508508
{
@@ -520,7 +520,7 @@ pub struct RefParam<T>(std::marker::PhantomData<T>);
520520
// Note that this could be done without a macro - it is simply to avoid repetition.
521521
macro_rules! into_py_native_func_tuple {
522522
($(($n:tt, $T:ident)),*) => {
523-
impl<F, $($T,)* R> IntoPyNativeFunc<($(OwnedParam<$T>,)*), R> for F
523+
impl<F, $($T,)* R> IntoPyNativeFunc<($(OwnedParam<$T>,)*), R, VirtualMachine> for F
524524
where
525525
F: Fn($($T,)* &VirtualMachine) -> R + 'static,
526526
$($T: FromArgs,)*
@@ -535,7 +535,7 @@ macro_rules! into_py_native_func_tuple {
535535
}
536536
}
537537

538-
impl<F, S, $($T,)* R> IntoPyNativeFunc<(RefParam<S>, $(OwnedParam<$T>,)*), R> for F
538+
impl<F, S, $($T,)* R> IntoPyNativeFunc<(RefParam<S>, $(OwnedParam<$T>,)*), R, VirtualMachine> for F
539539
where
540540
F: Fn(&S, $($T,)* &VirtualMachine) -> R + 'static,
541541
S: PyValue,
@@ -550,6 +550,29 @@ macro_rules! into_py_native_func_tuple {
550550
})
551551
}
552552
}
553+
554+
impl<F, $($T,)* R> IntoPyNativeFunc<($(OwnedParam<$T>,)*), R, ()> for F
555+
where
556+
F: Fn($($T,)*) -> R + 'static,
557+
$($T: FromArgs,)*
558+
R: IntoPyObject,
559+
{
560+
fn into_func(self) -> PyNativeFunc {
561+
IntoPyNativeFunc::into_func(move |$($n,)* _vm: &VirtualMachine| (self)($($n,)*))
562+
}
563+
}
564+
565+
impl<F, S, $($T,)* R> IntoPyNativeFunc<(RefParam<S>, $(OwnedParam<$T>,)*), R, ()> for F
566+
where
567+
F: Fn(&S, $($T,)*) -> R + 'static,
568+
S: PyValue,
569+
$($T: FromArgs,)*
570+
R: IntoPyObject,
571+
{
572+
fn into_func(self) -> PyNativeFunc {
573+
IntoPyNativeFunc::into_func(move |zelf: &S, $($n,)* _vm: &VirtualMachine| (self)(zelf, $($n,)*))
574+
}
575+
}
553576
};
554577
}
555578

vm/src/obj/objproperty.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ impl<'a> PropertyBuilder<'a> {
265265
}
266266
}
267267

268-
pub fn add_getter<I, V, F: IntoPyNativeFunc<I, V>>(self, func: F) -> Self {
268+
pub fn add_getter<I, V, VM, F: IntoPyNativeFunc<I, V, VM>>(self, func: F) -> Self {
269269
let func = self.ctx.new_rustfunc(func);
270270
Self {
271271
ctx: self.ctx,
@@ -274,7 +274,7 @@ impl<'a> PropertyBuilder<'a> {
274274
}
275275
}
276276

277-
pub fn add_setter<I, V, F: IntoPyNativeFunc<(I, V), impl PropertySetterResult>>(
277+
pub fn add_setter<I, V, VM, F: IntoPyNativeFunc<(I, V), impl PropertySetterResult, VM>>(
278278
self,
279279
func: F,
280280
) -> Self {

vm/src/pyobject.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -461,9 +461,9 @@ impl PyContext {
461461
PyObject::new(PyNamespace, self.namespace_type(), Some(self.new_dict()))
462462
}
463463

464-
pub fn new_rustfunc<F, T, R>(&self, f: F) -> PyObjectRef
464+
pub fn new_rustfunc<F, T, R, VM>(&self, f: F) -> PyObjectRef
465465
where
466-
F: IntoPyNativeFunc<T, R>,
466+
F: IntoPyNativeFunc<T, R, VM>,
467467
{
468468
PyObject::new(
469469
PyBuiltinFunction::new(f.into_func()),
@@ -472,9 +472,9 @@ impl PyContext {
472472
)
473473
}
474474

475-
pub fn new_classmethod<F, T, R>(&self, f: F) -> PyObjectRef
475+
pub fn new_classmethod<F, T, R, VM>(&self, f: F) -> PyObjectRef
476476
where
477-
F: IntoPyNativeFunc<T, R>,
477+
F: IntoPyNativeFunc<T, R, VM>,
478478
{
479479
PyObject::new(
480480
PyClassMethod {
@@ -485,9 +485,9 @@ impl PyContext {
485485
)
486486
}
487487

488-
pub fn new_property<F, I, V>(&self, f: F) -> PyObjectRef
488+
pub fn new_property<F, I, V, VM>(&self, f: F) -> PyObjectRef
489489
where
490-
F: IntoPyNativeFunc<I, V>,
490+
F: IntoPyNativeFunc<I, V, VM>,
491491
{
492492
PropertyBuilder::new(self).add_getter(f).create()
493493
}

vm/src/stdlib/os.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,7 +1257,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
12571257
follow_symlinks: Option<bool>,
12581258
};
12591259
impl<'a> SupportFunc<'a> {
1260-
fn new<F, T, R>(
1260+
fn new<F, T, R, VM>(
12611261
vm: &VirtualMachine,
12621262
name: &'a str,
12631263
func: F,
@@ -1266,7 +1266,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
12661266
follow_symlinks: Option<bool>,
12671267
) -> Self
12681268
where
1269-
F: IntoPyNativeFunc<T, R>,
1269+
F: IntoPyNativeFunc<T, R, VM>,
12701270
{
12711271
let func_obj = vm.ctx.new_rustfunc(func);
12721272
Self {

0 commit comments

Comments
 (0)