Skip to content

Commit 61455ce

Browse files
committed
Separate IntoPyObject and IntoPyResult
1 parent 1249fab commit 61455ce

21 files changed

Lines changed: 133 additions & 125 deletions

derive/src/pyclass.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ pub fn impl_pystruct_sequence(attr: AttributeArgs, item: Item) -> Result<TokenSt
524524
vec![#(::rustpython_vm::pyobject::IntoPyObject::into_pyobject(
525525
::std::clone::Clone::clone(&self.#field_names),
526526
vm,
527-
)?),*],
527+
)),*],
528528
);
529529
::rustpython_vm::pyobject::PyValue::into_ref_with_type(tuple, vm, cls)
530530
}

vm/src/dictdatatype.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ impl<T: Clone> Dict<T> {
157157
hash_value,
158158
} => {
159159
// New key:
160-
self.unchecked_push(hash_index, hash_value, key.into_pyobject(vm)?, value);
160+
self.unchecked_push(hash_index, hash_value, key.into_pyobject(vm), value);
161161
break Ok(());
162162
}
163163
}

vm/src/format.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,7 @@ pub(crate) enum FormatParseError {
564564
}
565565

566566
impl FormatParseError {
567+
// TODO: IntoPyException
567568
pub fn into_pyobject(self, vm: &VirtualMachine) -> PyBaseExceptionRef {
568569
match self {
569570
FormatParseError::UnmatchedBracket => {
@@ -823,7 +824,7 @@ impl FormatString {
823824
}
824825
FieldNamePart::Index(index) => {
825826
// TODO Implement DictKey for usize so we can pass index directly
826-
argument = argument.get_item(&index.into_pyobject(vm)?, vm)?;
827+
argument = argument.get_item(&index.into_pyobject(vm), vm)?;
827828
}
828829
FieldNamePart::StringIndex(index) => {
829830
argument = argument.get_item(&index, vm)?;

vm/src/function.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::exceptions::PyBaseExceptionRef;
1010
use crate::obj::objtuple::PyTupleRef;
1111
use crate::obj::objtype::{isinstance, PyClassRef};
1212
use crate::pyobject::{
13-
IntoPyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol,
13+
IntoPyResult, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol,
1414
};
1515
use crate::vm::VirtualMachine;
1616

@@ -524,13 +524,13 @@ macro_rules! into_py_native_func_tuple {
524524
where
525525
F: Fn($($T,)* &VirtualMachine) -> R + 'static + Send + Sync,
526526
$($T: FromArgs,)*
527-
R: IntoPyObject,
527+
R: IntoPyResult,
528528
{
529529
fn into_func(self) -> PyNativeFunc {
530530
smallbox!(move |vm: &VirtualMachine, args: PyFuncArgs| {
531531
let ($($n,)*) = args.bind::<($($T,)*)>(vm)?;
532532

533-
(self)($($n,)* vm).into_pyobject(vm)
533+
(self)($($n,)* vm).into_pyresult(vm)
534534
})
535535
}
536536
}
@@ -540,13 +540,13 @@ macro_rules! into_py_native_func_tuple {
540540
F: Fn(&S, $($T,)* &VirtualMachine) -> R + 'static + Send + Sync,
541541
S: PyValue,
542542
$($T: FromArgs,)*
543-
R: IntoPyObject,
543+
R: IntoPyResult,
544544
{
545545
fn into_func(self) -> PyNativeFunc {
546546
smallbox!(move |vm: &VirtualMachine, args: PyFuncArgs| {
547547
let (zelf, $($n,)*) = args.bind::<(PyRef<S>, $($T,)*)>(vm)?;
548548

549-
(self)(&zelf, $($n,)* vm).into_pyobject(vm)
549+
(self)(&zelf, $($n,)* vm).into_pyresult(vm)
550550
})
551551
}
552552
}
@@ -555,7 +555,7 @@ macro_rules! into_py_native_func_tuple {
555555
where
556556
F: Fn($($T,)*) -> R + 'static + Send + Sync,
557557
$($T: FromArgs,)*
558-
R: IntoPyObject,
558+
R: IntoPyResult,
559559
{
560560
fn into_func(self) -> PyNativeFunc {
561561
IntoPyNativeFunc::into_func(move |$($n,)* _vm: &VirtualMachine| (self)($($n,)*))
@@ -567,7 +567,7 @@ macro_rules! into_py_native_func_tuple {
567567
F: Fn(&S, $($T,)*) -> R + 'static + Send + Sync,
568568
S: PyValue,
569569
$($T: FromArgs,)*
570-
R: IntoPyObject,
570+
R: IntoPyResult,
571571
{
572572
fn into_func(self) -> PyNativeFunc {
573573
IntoPyNativeFunc::into_func(move |zelf: &S, $($n,)* _vm: &VirtualMachine| (self)(zelf, $($n,)*))

vm/src/obj/objbool.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use super::objstr::PyStringRef;
1313
use super::objtype;
1414

1515
impl IntoPyObject for bool {
16-
fn into_pyobject(self, vm: &VirtualMachine) -> PyResult {
17-
Ok(vm.ctx.new_bool(self))
16+
fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef {
17+
vm.ctx.new_bool(self)
1818
}
1919
}
2020

@@ -112,7 +112,7 @@ impl PyBool {
112112

113113
#[pymethod(name = "__ror__")]
114114
#[pymethod(magic)]
115-
fn or(lhs: PyObjectRef, rhs: PyObjectRef, vm: &VirtualMachine) -> PyResult {
115+
fn or(lhs: PyObjectRef, rhs: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
116116
if objtype::isinstance(&lhs, &vm.ctx.types.bool_type)
117117
&& objtype::isinstance(&rhs, &vm.ctx.types.bool_type)
118118
{
@@ -126,7 +126,7 @@ impl PyBool {
126126

127127
#[pymethod(name = "__rand__")]
128128
#[pymethod(magic)]
129-
fn and(lhs: PyObjectRef, rhs: PyObjectRef, vm: &VirtualMachine) -> PyResult {
129+
fn and(lhs: PyObjectRef, rhs: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
130130
if objtype::isinstance(&lhs, &vm.ctx.types.bool_type)
131131
&& objtype::isinstance(&rhs, &vm.ctx.types.bool_type)
132132
{
@@ -140,7 +140,7 @@ impl PyBool {
140140

141141
#[pymethod(name = "__rxor__")]
142142
#[pymethod(magic)]
143-
fn xor(lhs: PyObjectRef, rhs: PyObjectRef, vm: &VirtualMachine) -> PyResult {
143+
fn xor(lhs: PyObjectRef, rhs: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
144144
if objtype::isinstance(&lhs, &vm.ctx.types.bool_type)
145145
&& objtype::isinstance(&rhs, &vm.ctx.types.bool_type)
146146
{

vm/src/obj/objbytes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ impl From<Vec<u8>> for PyBytes {
5959
}
6060

6161
impl IntoPyObject for Vec<u8> {
62-
fn into_pyobject(self, vm: &VirtualMachine) -> PyResult {
63-
Ok(vm.ctx.new_bytes(self))
62+
fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef {
63+
vm.ctx.new_bytes(self)
6464
}
6565
}
6666

vm/src/obj/objcomplex.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ impl PyValue for PyComplex {
3232
}
3333

3434
impl IntoPyObject for Complex64 {
35-
fn into_pyobject(self, vm: &VirtualMachine) -> PyResult {
36-
Ok(vm.ctx.new_complex(self))
35+
fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef {
36+
vm.ctx.new_complex(self)
3737
}
3838
}
3939

@@ -81,10 +81,10 @@ impl PyComplex {
8181
where
8282
F: Fn(Complex64, Complex64) -> Complex64,
8383
{
84-
try_complex(&other, vm)?.map_or_else(
85-
|| Ok(vm.ctx.not_implemented()),
84+
Ok(try_complex(&other, vm)?.map_or_else(
85+
|| vm.ctx.not_implemented(),
8686
|other| op(self.value, other).into_pyobject(vm),
87-
)
87+
))
8888
}
8989

9090
#[pymethod(name = "__add__")]

vm/src/obj/objdict.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ impl PyDictRef {
282282

283283
if let Some(method_or_err) = vm.get_method(self.clone().into_object(), "__missing__") {
284284
let method = method_or_err?;
285-
Ok(Some(vm.invoke(&method, vec![key.into_pyobject(vm)?])?))
285+
Ok(Some(vm.invoke(&method, vec![key.into_pyobject(vm)])?))
286286
} else {
287287
Ok(None)
288288
}
@@ -419,7 +419,7 @@ impl PyDictRef {
419419
}
420420

421421
pub fn contains_key<T: IntoPyObject>(&self, key: T, vm: &VirtualMachine) -> bool {
422-
let key = key.into_pyobject(vm).unwrap();
422+
let key = key.into_pyobject(vm);
423423
self.entries.contains(vm, &key).unwrap()
424424
}
425425

vm/src/obj/objfloat.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use super::objtype::PyClassRef;
1010
use crate::format::FormatSpec;
1111
use crate::function::{OptionalArg, OptionalOption};
1212
use crate::pyobject::{
13-
IntoPyObject, PyArithmaticValue::*, PyClassImpl, PyComparisonValue, PyContext, PyObjectRef,
14-
PyRef, PyResult, PyValue, TryFromObject, TypeProtocol,
13+
IntoPyObject, IntoPyResult, PyArithmaticValue::*, PyClassImpl, PyComparisonValue, PyContext,
14+
PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol,
1515
};
1616
use crate::vm::VirtualMachine;
1717
use rustpython_common::{float_ops, hash};
@@ -36,13 +36,13 @@ impl PyValue for PyFloat {
3636
}
3737

3838
impl IntoPyObject for f64 {
39-
fn into_pyobject(self, vm: &VirtualMachine) -> PyResult {
40-
Ok(vm.ctx.new_float(self))
39+
fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef {
40+
vm.ctx.new_float(self)
4141
}
4242
}
4343
impl IntoPyObject for f32 {
44-
fn into_pyobject(self, vm: &VirtualMachine) -> PyResult {
45-
Ok(vm.ctx.new_float(f64::from(self)))
44+
fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef {
45+
vm.ctx.new_float(f64::from(self))
4646
}
4747
}
4848

@@ -142,9 +142,9 @@ pub fn float_pow(v1: f64, v2: f64, vm: &VirtualMachine) -> PyResult {
142142
} else if v1.is_sign_negative() && (v2.floor() - v2).abs() > f64::EPSILON {
143143
let v1 = Complex64::new(v1, 0.);
144144
let v2 = Complex64::new(v2, 0.);
145-
v1.powc(v2).into_pyobject(vm)
145+
Ok(v1.powc(v2).into_pyobject(vm))
146146
} else {
147-
v1.powf(v2).into_pyobject(vm)
147+
Ok(v1.powf(v2).into_pyobject(vm))
148148
}
149149
}
150150

@@ -259,7 +259,7 @@ impl PyFloat {
259259
{
260260
try_float(&other, vm)?.map_or_else(
261261
|| Ok(vm.ctx.not_implemented()),
262-
|other| op(self.value, other).into_pyobject(vm),
262+
|other| op(self.value, other).into_pyresult(vm),
263263
)
264264
}
265265

@@ -270,7 +270,7 @@ impl PyFloat {
270270
{
271271
try_float(&other, vm)?.map_or_else(
272272
|| Ok(vm.ctx.not_implemented()),
273-
|other| op(self.value, other).into_pyobject(vm),
273+
|other| op(self.value, other).into_pyresult(vm),
274274
)
275275
}
276276

vm/src/obj/objgetset.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use super::objtype::PyClassRef;
55
use crate::function::{FunctionBox, OptionalArg, OwnedParam, RefParam};
66
use crate::pyobject::{
7-
IntoPyObject, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject,
7+
IntoPyResult, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject,
88
TypeProtocol,
99
};
1010
use crate::slots::SlotDescriptor;
@@ -22,12 +22,12 @@ impl<F, T, R> IntoPyGetterFunc<(OwnedParam<T>, R, VirtualMachine)> for F
2222
where
2323
F: Fn(T, &VirtualMachine) -> R + 'static + Send + Sync,
2424
T: TryFromObject,
25-
R: IntoPyObject,
25+
R: IntoPyResult,
2626
{
2727
fn into_getter(self) -> PyGetterFunc {
2828
smallbox::smallbox!(move |vm: &VirtualMachine, obj| {
2929
let obj = T::try_from_object(vm, obj)?;
30-
(self)(obj, vm).into_pyobject(vm)
30+
(self)(obj, vm).into_pyresult(vm)
3131
})
3232
}
3333
}
@@ -36,12 +36,12 @@ impl<F, S, R> IntoPyGetterFunc<(RefParam<S>, R, VirtualMachine)> for F
3636
where
3737
F: Fn(&S, &VirtualMachine) -> R + 'static + Send + Sync,
3838
S: PyValue,
39-
R: IntoPyObject,
39+
R: IntoPyResult,
4040
{
4141
fn into_getter(self) -> PyGetterFunc {
4242
smallbox::smallbox!(move |vm: &VirtualMachine, obj| {
4343
let zelf = PyRef::<S>::try_from_object(vm, obj)?;
44-
(self)(&zelf, vm).into_pyobject(vm)
44+
(self)(&zelf, vm).into_pyresult(vm)
4545
})
4646
}
4747
}
@@ -50,7 +50,7 @@ impl<F, T, R> IntoPyGetterFunc<(OwnedParam<T>, R)> for F
5050
where
5151
F: Fn(T) -> R + 'static + Send + Sync,
5252
T: TryFromObject,
53-
R: IntoPyObject,
53+
R: IntoPyResult,
5454
{
5555
fn into_getter(self) -> PyGetterFunc {
5656
IntoPyGetterFunc::into_getter(move |obj, _vm: &VirtualMachine| (self)(obj))
@@ -61,7 +61,7 @@ impl<F, S, R> IntoPyGetterFunc<(RefParam<S>, R)> for F
6161
where
6262
F: Fn(&S) -> R + 'static + Send + Sync,
6363
S: PyValue,
64-
R: IntoPyObject,
64+
R: IntoPyResult,
6565
{
6666
fn into_getter(self) -> PyGetterFunc {
6767
IntoPyGetterFunc::into_getter(move |zelf: &S, _vm: &VirtualMachine| (self)(zelf))

0 commit comments

Comments
 (0)