Skip to content

Commit e8e8544

Browse files
committed
Avoid use of PyObject in objcomplex.
1 parent 871ac15 commit e8e8544

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

vm/src/obj/objcomplex.rs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use super::objfloat;
22
use super::objint;
3-
use super::objtype;
3+
use super::objtype::{self, PyClassRef};
44
use crate::pyobject::{
5-
PyContext, PyFuncArgs, PyObject, PyObjectRef, PyResult, PyValue, TypeProtocol,
5+
OptionalArg, PyContext, PyFuncArgs, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol,
66
};
77
use crate::vm::VirtualMachine;
88
use num_complex::Complex64;
@@ -12,6 +12,7 @@ use num_traits::ToPrimitive;
1212
pub struct PyComplex {
1313
value: Complex64,
1414
}
15+
type PyComplexRef = PyRef<PyComplex>;
1516

1617
impl PyValue for PyComplex {
1718
fn required_type(ctx: &PyContext) -> PyObjectRef {
@@ -65,31 +66,24 @@ pub fn get_value(obj: &PyObjectRef) -> Complex64 {
6566
obj.payload::<PyComplex>().unwrap().value
6667
}
6768

68-
fn complex_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
69-
arg_check!(
70-
vm,
71-
args,
72-
required = [(cls, None)],
73-
optional = [(real, None), (imag, None)]
74-
);
75-
76-
if !objtype::issubclass(cls, &vm.ctx.complex_type()) {
77-
return Err(vm.new_type_error(format!("{:?} is not a subtype of complex", cls)));
78-
}
79-
69+
fn complex_new(
70+
cls: PyClassRef,
71+
real: OptionalArg<PyObjectRef>,
72+
imag: OptionalArg<PyObjectRef>,
73+
vm: &mut VirtualMachine,
74+
) -> PyResult<PyComplexRef> {
8075
let real = match real {
81-
None => 0.0,
82-
Some(value) => objfloat::make_float(vm, value)?,
76+
OptionalArg::Missing => 0.0,
77+
OptionalArg::Present(ref value) => objfloat::make_float(vm, value)?,
8378
};
8479

8580
let imag = match imag {
86-
None => 0.0,
87-
Some(value) => objfloat::make_float(vm, value)?,
81+
OptionalArg::Missing => 0.0,
82+
OptionalArg::Present(ref value) => objfloat::make_float(vm, value)?,
8883
};
8984

9085
let value = Complex64::new(real, imag);
91-
92-
Ok(PyObject::new(PyComplex { value }, cls.clone()))
86+
PyComplex { value }.into_ref_with_type(vm, cls)
9387
}
9488

9589
fn complex_real(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {

0 commit comments

Comments
 (0)