Skip to content

Commit eb85f0f

Browse files
committed
deprecate vm.invoke
1 parent 5eb7754 commit eb85f0f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+186
-195
lines changed

examples/call_between_rust_and_python.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,12 @@ pub fn main() {
1919

2020
let module = vm.import("call_between_rust_and_python", None, 0).unwrap();
2121
let init_fn = module.get_attr("python_callback", vm).unwrap();
22-
vm.invoke(&init_fn, ()).unwrap();
22+
init_fn.call((), vm).unwrap();
2323

2424
let take_string_fn = module.get_attr("take_string", vm).unwrap();
25-
vm.invoke(
26-
&take_string_fn,
27-
(String::from("Rust string sent to python"),),
28-
)
29-
.unwrap();
25+
take_string_fn
26+
.call((String::from("Rust string sent to python"),), vm)
27+
.unwrap();
3028
})
3129
}
3230

examples/package_embed.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn py_main(interp: &Interpreter) -> vm::PyResult<PyStrRef> {
88
.expect("add path");
99
let module = vm.import("package_embed", None, 0)?;
1010
let name_func = module.get_attr("context", vm)?;
11-
let result = vm.invoke(&name_func, ())?;
11+
let result = name_func.call((), vm)?;
1212
let result: PyStrRef = result.get_attr("name", vm)?.try_into_value(vm)?;
1313
vm::PyResult::Ok(result)
1414
})

stdlib/src/array.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@ pub(crate) fn make_module(vm: &VirtualMachine) -> PyObjectRef {
1717
.get_attr("MutableSequence", vm)
1818
.expect("Expect collections.abc has MutableSequence type.");
1919

20-
vm.invoke(
21-
&mutable_sequence
22-
.get_attr("register", vm)
23-
.expect("Expect collections.abc.MutableSequence has register method."),
24-
(array,),
25-
)
26-
.expect("Expect collections.abc.MutableSequence.register(array.array) not fail.");
20+
let register = &mutable_sequence
21+
.get_attr("register", vm)
22+
.expect("Expect collections.abc.MutableSequence has register method.");
23+
register
24+
.call((array,), vm)
25+
.expect("Expect collections.abc.MutableSequence.register(array.array) not fail.");
2726

2827
module
2928
}

stdlib/src/bisect.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ mod _bisect {
7070
let mid = (lo + hi) / 2;
7171
let a_mid = a.get_item(&mid, vm)?;
7272
let comp = if let Some(ref key) = key {
73-
vm.invoke(key, (a_mid,))?
73+
key.call((a_mid,), vm)?
7474
} else {
7575
a_mid
7676
};
@@ -96,7 +96,7 @@ mod _bisect {
9696
let mid = (lo + hi) / 2;
9797
let a_mid = a.get_item(&mid, vm)?;
9898
let comp = if let Some(ref key) = key {
99-
vm.invoke(key, (a_mid,))?
99+
key.call((a_mid,), vm)?
100100
} else {
101101
a_mid
102102
};
@@ -112,7 +112,7 @@ mod _bisect {
112112
#[pyfunction]
113113
fn insort_left(BisectArgs { a, x, lo, hi, key }: BisectArgs, vm: &VirtualMachine) -> PyResult {
114114
let x = if let Some(ref key) = key {
115-
vm.invoke(key, (x,))?
115+
key.call((x,), vm)?
116116
} else {
117117
x
118118
};
@@ -132,7 +132,7 @@ mod _bisect {
132132
#[pyfunction]
133133
fn insort_right(BisectArgs { a, x, lo, hi, key }: BisectArgs, vm: &VirtualMachine) -> PyResult {
134134
let x = if let Some(ref key) = key {
135-
vm.invoke(key, (x,))?
135+
key.call((x,), vm)?
136136
} else {
137137
x
138138
};

stdlib/src/csv.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ mod _csv {
309309
let s = std::str::from_utf8(&buffer[..buffer_offset])
310310
.map_err(|_| vm.new_unicode_decode_error("csv not utf8".to_owned()))?;
311311

312-
vm.invoke(&self.write, (s.to_owned(),))
312+
self.write.call((s,), vm)
313313
}
314314

315315
#[pymethod]

stdlib/src/json.rs

+16-21
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ mod machinery;
55
mod _json {
66
use super::machinery;
77
use crate::vm::{
8-
builtins::{PyBaseExceptionRef, PyStrRef, PyTypeRef},
8+
builtins::{PyBaseExceptionRef, PyStrRef, PyType, PyTypeRef},
99
convert::{ToPyObject, ToPyResult},
10-
function::OptionalArg,
10+
function::{IntoFuncArgs, OptionalArg},
1111
protocol::PyIterReturn,
1212
types::{Callable, Constructor},
1313
AsObject, Py, PyObjectRef, PyPayload, PyResult, VirtualMachine,
@@ -91,25 +91,23 @@ mod _json {
9191
'{' => {
9292
// TODO: parse the object in rust
9393
let parse_obj = self.ctx.get_attr("parse_object", vm)?;
94-
return PyIterReturn::from_pyresult(
95-
vm.invoke(
96-
&parse_obj,
97-
(
98-
(pystr, next_idx),
99-
self.strict,
100-
scan_once,
101-
self.object_hook.clone(),
102-
self.object_pairs_hook.clone(),
103-
),
94+
let result = parse_obj.call(
95+
(
96+
(pystr, next_idx),
97+
self.strict,
98+
scan_once,
99+
self.object_hook.clone(),
100+
self.object_pairs_hook.clone(),
104101
),
105102
vm,
106103
);
104+
return PyIterReturn::from_pyresult(result, vm);
107105
}
108106
'[' => {
109107
// TODO: parse the array in rust
110108
let parse_array = self.ctx.get_attr("parse_array", vm)?;
111109
return PyIterReturn::from_pyresult(
112-
vm.invoke(&parse_array, ((pystr, next_idx), scan_once)),
110+
parse_array.call(((pystr, next_idx), scan_once), vm),
113111
vm,
114112
);
115113
}
@@ -138,11 +136,8 @@ mod _json {
138136
($s:literal) => {
139137
if s.starts_with($s) {
140138
return Ok(PyIterReturn::Return(
141-
vm.new_tuple((
142-
vm.invoke(&self.parse_constant, ($s.to_owned(),))?,
143-
idx + $s.len(),
144-
))
145-
.into(),
139+
vm.new_tuple((self.parse_constant.call(($s,), vm)?, idx + $s.len()))
140+
.into(),
146141
));
147142
}
148143
};
@@ -181,12 +176,12 @@ mod _json {
181176
let ret = if has_decimal || has_exponent {
182177
// float
183178
if let Some(ref parse_float) = self.parse_float {
184-
vm.invoke(parse_float, (buf.to_owned(),))
179+
parse_float.call((buf,), vm)
185180
} else {
186181
Ok(vm.ctx.new_float(f64::from_str(buf).unwrap()).into())
187182
}
188183
} else if let Some(ref parse_int) = self.parse_int {
189-
vm.invoke(parse_int, (buf.to_owned(),))
184+
parse_int.call((buf,), vm)
190185
} else {
191186
Ok(vm.new_pyobj(BigInt::from_str(buf).unwrap()))
192187
};
@@ -243,7 +238,7 @@ mod _json {
243238
) -> PyBaseExceptionRef {
244239
let get_error = || -> PyResult<_> {
245240
let cls = vm.try_class("json", "JSONDecodeError")?;
246-
let exc = vm.invoke(&cls, (e.msg, s, e.pos))?;
241+
let exc = PyType::call(&cls, (e.msg, s, e.pos).into_args(vm), vm)?;
247242
exc.try_into_value(vm)
248243
};
249244
match get_error() {

stdlib/src/math.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ mod math {
512512
func_name.as_str(),
513513
)
514514
})?;
515-
vm.invoke(&method, ())
515+
method.call((), vm)
516516
}
517517

518518
#[pyfunction]

stdlib/src/pyexpat.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ mod _pyexpat {
5959
where
6060
T: IntoFuncArgs,
6161
{
62-
vm.invoke(&handler.read().clone(), args).ok();
62+
handler.read().call(args, vm).ok();
6363
}
6464

6565
#[pyclass]

stdlib/src/select.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ impl TryFromObject for Selectable {
8080
vm.ctx.interned_str("fileno").unwrap(),
8181
|| "select arg must be an int or object with a fileno() method".to_owned(),
8282
)?;
83-
vm.invoke(&meth, ())?.try_into_value(vm)
83+
meth.call((), vm)?.try_into_value(vm)
8484
})?;
8585
Ok(Selectable { obj, fno })
8686
}

stdlib/src/sqlite.rs

+15-13
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ mod _sqlite {
390390
.map(|val| value_to_object(val, db, vm))
391391
.collect::<PyResult<Vec<PyObjectRef>>>()?;
392392

393-
let val = vm.invoke(func, args)?;
393+
let val = func.call(args, vm)?;
394394

395395
context.result_from_object(&val, vm)
396396
};
@@ -410,7 +410,7 @@ mod _sqlite {
410410
let args = std::slice::from_raw_parts(argv, argc as usize);
411411
let instance = context.aggregate_context::<*const PyObject>();
412412
if (*instance).is_null() {
413-
match vm.invoke(cls, ()) {
413+
match cls.call((), vm) {
414414
Ok(obj) => *instance = obj.into_raw(),
415415
Err(exc) => {
416416
return context.result_exception(
@@ -450,7 +450,7 @@ mod _sqlite {
450450
let text2 = ptr_to_string(b_ptr.cast(), b_len, null_mut(), vm)?;
451451
let text2 = vm.ctx.new_str(text2);
452452

453-
let val = vm.invoke(callable, (text1, text2))?;
453+
let val = callable.call((text1, text2), vm)?;
454454
let Some(val) = val.to_number().index(vm) else {
455455
return Ok(0);
456456
};
@@ -505,7 +505,7 @@ mod _sqlite {
505505
let db_name = ptr_to_str(db_name, vm)?;
506506
let access = ptr_to_str(access, vm)?;
507507

508-
let val = vm.invoke(callable, (action, arg1, arg2, db_name, access))?;
508+
let val = callable.call((action, arg1, arg2, db_name, access), vm)?;
509509
let Some(val) = val.payload::<PyInt>() else {
510510
return Ok(SQLITE_DENY);
511511
};
@@ -525,15 +525,16 @@ mod _sqlite {
525525
let expanded = sqlite3_expanded_sql(stmt.cast());
526526
let f = || -> PyResult<()> {
527527
let stmt = ptr_to_str(expanded, vm).or_else(|_| ptr_to_str(sql.cast(), vm))?;
528-
vm.invoke(callable, (stmt,)).map(drop)
528+
callable.call((stmt,), vm)?;
529+
Ok(())
529530
};
530531
let _ = f();
531532
0
532533
}
533534

534535
unsafe extern "C" fn progress_callback(data: *mut c_void) -> c_int {
535536
let (callable, vm) = (*data.cast::<Self>()).retrive();
536-
if let Ok(val) = vm.invoke(callable, ()) {
537+
if let Ok(val) = callable.call((), vm) {
537538
if let Ok(val) = val.is_true(vm) {
538539
return val as c_int;
539540
}
@@ -661,10 +662,10 @@ mod _sqlite {
661662
.new_tuple(vec![obj.class().to_owned().into(), proto.clone()]);
662663

663664
if let Some(adapter) = adapters().get_item_opt(key.as_object(), vm)? {
664-
return vm.invoke(&adapter, (obj,));
665+
return adapter.call((obj,), vm);
665666
}
666667
if let Ok(adapter) = proto.get_attr("__adapt__", vm) {
667-
match vm.invoke(&adapter, (obj,)) {
668+
match adapter.call((obj,), vm) {
668669
Ok(val) => return Ok(val),
669670
Err(exc) => {
670671
if !exc.fast_isinstance(vm.ctx.exceptions.type_error) {
@@ -674,7 +675,7 @@ mod _sqlite {
674675
}
675676
}
676677
if let Ok(adapter) = obj.get_attr("__conform__", vm) {
677-
match vm.invoke(&adapter, (proto,)) {
678+
match adapter.call((proto,), vm) {
678679
Ok(val) => return Ok(val),
679680
Err(exc) => {
680681
if !exc.fast_isinstance(vm.ctx.exceptions.type_error) {
@@ -1228,7 +1229,7 @@ mod _sqlite {
12281229
fn iterdump(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult {
12291230
let module = vm.import("sqlite3.dump", None, 0)?;
12301231
let func = module.get_attr("_iterdump", vm)?;
1231-
vm.invoke(&func, (zelf,))
1232+
func.call((zelf,), vm)
12321233
}
12331234

12341235
#[pymethod]
@@ -1699,7 +1700,7 @@ mod _sqlite {
16991700
std::slice::from_raw_parts(blob.cast::<u8>(), nbytes as usize)
17001701
};
17011702
let blob = vm.ctx.new_bytes(blob.to_vec());
1702-
vm.invoke(&converter, (blob,))?
1703+
converter.call((blob,), vm)?
17031704
}
17041705
} else {
17051706
let col_type = st.column_type(i);
@@ -1724,7 +1725,7 @@ mod _sqlite {
17241725
PyByteArray::from(text).into_ref(vm).into()
17251726
} else {
17261727
let bytes = vm.ctx.new_bytes(text);
1727-
vm.invoke(&text_factory, (bytes,))?
1728+
text_factory.call((bytes,), vm)?
17281729
}
17291730
}
17301731
SQLITE_BLOB => {
@@ -1765,7 +1766,8 @@ mod _sqlite {
17651766
let row = vm.ctx.new_tuple(row);
17661767

17671768
if let Some(row_factory) = zelf.row_factory.to_owned() {
1768-
vm.invoke(&row_factory, (zelf.to_owned(), row))
1769+
row_factory
1770+
.call((zelf.to_owned(), row), vm)
17691771
.map(PyIterReturn::Return)
17701772
} else {
17711773
Ok(PyIterReturn::Return(row.into()))

vm/src/builtins/bool.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl PyObjectRef {
3737
Some(method_or_err) => {
3838
// If descriptor returns Error, propagate it further
3939
let method = method_or_err?;
40-
let bool_obj = vm.invoke(&method, ())?;
40+
let bool_obj = method.call((), vm)?;
4141
if !bool_obj.fast_isinstance(vm.ctx.types.bool_type) {
4242
return Err(vm.new_type_error(format!(
4343
"__bool__ should return bool, returned type {}",
@@ -50,7 +50,7 @@ impl PyObjectRef {
5050
None => match vm.get_method(self, identifier!(vm, __len__)) {
5151
Some(method_or_err) => {
5252
let method = method_or_err?;
53-
let bool_obj = vm.invoke(&method, ())?;
53+
let bool_obj = method.call((), vm)?;
5454
let int_obj = bool_obj.payload::<PyInt>().ok_or_else(|| {
5555
vm.new_type_error(format!(
5656
"'{}' object cannot be interpreted as an integer",

vm/src/builtins/classmethod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl GetDescriptor for PyClassMethod {
5858
let call_descr_get: PyResult<PyObjectRef> = zelf.callable.lock().get_attr("__get__", vm);
5959
match call_descr_get {
6060
Err(_) => Ok(PyBoundMethod::new_ref(cls, zelf.callable.lock().clone(), &vm.ctx).into()),
61-
Ok(call_descr_get) => vm.invoke(&call_descr_get, (cls.clone(), cls)),
61+
Ok(call_descr_get) => call_descr_get.call((cls.clone(), cls), vm),
6262
}
6363
}
6464
}

vm/src/builtins/complex.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl PyObjectRef {
6060
return Ok(Some((complex.value, true)));
6161
}
6262
if let Some(method) = vm.get_method(self.clone(), identifier!(vm, __complex__)) {
63-
let result = vm.invoke(&method?, ())?;
63+
let result = method?.call((), vm)?;
6464
// TODO: returning strict subclasses of complex in __complex__ is deprecated
6565
return match result.payload::<PyComplex>() {
6666
Some(complex_obj) => Ok(Some((complex_obj.value, true))),

vm/src/builtins/dict.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl PyDict {
7272
};
7373
let dict = &self.entries;
7474
if let Some(keys) = vm.get_method(other.clone(), vm.ctx.intern_str("keys")) {
75-
let keys = vm.invoke(&keys?, ())?.get_iter(vm)?;
75+
let keys = keys?.call((), vm)?.get_iter(vm)?;
7676
while let PyIterReturn::Return(key) = keys.next(vm)? {
7777
let val = other.get_item(&*key, vm)?;
7878
dict.insert(vm, &*key, val)?;
@@ -511,7 +511,7 @@ impl Py<PyDict> {
511511
vm: &VirtualMachine,
512512
) -> PyResult<Option<PyObjectRef>> {
513513
vm.get_method(self.to_owned().into(), identifier!(vm, __missing__))
514-
.map(|methods| vm.invoke(&methods?, (key.to_pyobject(vm),)))
514+
.map(|methods| methods?.call((key.to_pyobject(vm),), vm))
515515
.transpose()
516516
}
517517

vm/src/builtins/filter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl IterNext for PyFilter {
5757
} else {
5858
// the predicate itself can raise StopIteration which does stop the filter
5959
// iteration
60-
match PyIterReturn::from_pyresult(vm.invoke(predicate, (next_obj.clone(),)), vm)? {
60+
match PyIterReturn::from_pyresult(predicate.call((next_obj.clone(),), vm), vm)? {
6161
PyIterReturn::Return(obj) => obj,
6262
PyIterReturn::StopIteration(v) => return Ok(PyIterReturn::StopIteration(v)),
6363
}

vm/src/builtins/function.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ impl Callable for PyBoundMethod {
469469
#[inline]
470470
fn call(zelf: &crate::Py<Self>, mut args: FuncArgs, vm: &VirtualMachine) -> PyResult {
471471
args.prepend_arg(zelf.object.clone());
472-
vm.invoke(&zelf.function, args)
472+
zelf.function.call(args, vm)
473473
}
474474
}
475475

vm/src/builtins/list.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ fn do_sort(
510510
if let Some(ref key_func) = key_func {
511511
let mut items = values
512512
.iter()
513-
.map(|x| Ok((x.clone(), vm.invoke(key_func, (x.clone(),))?)))
513+
.map(|x| Ok((x.clone(), key_func.call((x.clone(),), vm)?)))
514514
.collect::<Result<Vec<_>, _>>()?;
515515
timsort::try_sort_by_gt(&mut items, |a, b| cmp(&a.1, &b.1))?;
516516
*values = items.into_iter().map(|(val, _)| val).collect();

0 commit comments

Comments
 (0)