Skip to content

Commit 2c53e8d

Browse files
committed
vm.ctx.new_complex -> PyComplex::new_ref
1 parent 616dfc1 commit 2c53e8d

File tree

4 files changed

+13
-17
lines changed

4 files changed

+13
-17
lines changed

vm/src/builtins/code.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ impl ConstantBag for PyObjBag<'_> {
8282
let obj = match constant {
8383
bytecode::ConstantData::Integer { value } => ctx.new_int(value).into(),
8484
bytecode::ConstantData::Float { value } => ctx.new_float(value).into(),
85-
bytecode::ConstantData::Complex { value } => ctx.new_complex(value),
85+
bytecode::ConstantData::Complex { value } => vm.new_pyobj(value),
8686
bytecode::ConstantData::Str { value } if value.len() <= 20 => {
8787
vm.intern_string(value).into()
8888
}
@@ -108,7 +108,7 @@ impl ConstantBag for PyObjBag<'_> {
108108
let obj = match constant {
109109
bytecode::BorrowedConstant::Integer { value } => ctx.new_bigint(value).into(),
110110
bytecode::BorrowedConstant::Float { value } => ctx.new_float(value).into(),
111-
bytecode::BorrowedConstant::Complex { value } => ctx.new_complex(value),
111+
bytecode::BorrowedConstant::Complex { value } => vm.new_pyobj(value),
112112
bytecode::BorrowedConstant::Str { value } if value.len() <= 20 => {
113113
vm.intern_string(value).into()
114114
}

vm/src/builtins/complex.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl PyValue for PyComplex {
2929

3030
impl IntoPyObject for Complex64 {
3131
fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef {
32-
vm.ctx.new_complex(self)
32+
PyComplex::new_ref(self, &vm.ctx).into()
3333
}
3434
}
3535

@@ -186,12 +186,18 @@ impl SlotConstructor for PyComplex {
186186
}
187187
}
188188

189-
#[pyimpl(flags(BASETYPE), with(Comparable, Hashable, SlotConstructor))]
190189
impl PyComplex {
190+
pub fn new_ref(value: Complex64, ctx: &PyContext) -> PyRef<Self> {
191+
PyRef::new_ref(Self::from(value), ctx.types.complex_type.clone(), None)
192+
}
193+
191194
pub fn to_complex(&self) -> Complex64 {
192195
self.value
193196
}
197+
}
194198

199+
#[pyimpl(flags(BASETYPE), with(Comparable, Hashable, SlotConstructor))]
200+
impl PyComplex {
195201
#[pymethod(magic)]
196202
fn complex(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyRef<PyComplex> {
197203
if zelf.is(&vm.ctx.types.complex_type) {

vm/src/pyobject.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ use crate::{
1313
namespace::PyNamespace,
1414
object, pystr,
1515
set::{self, PyFrozenSet},
16-
PyBaseExceptionRef, PyBoundMethod, PyComplex, PyDict, PyDictRef, PyEllipsis, PyFloat,
17-
PyInt, PyIntRef, PyList, PyNone, PyNotImplemented, PyStaticMethod, PyTuple, PyTupleRef,
18-
PyType, PyTypeRef,
16+
PyBaseExceptionRef, PyBoundMethod, PyDict, PyDictRef, PyEllipsis, PyFloat, PyInt, PyIntRef,
17+
PyList, PyNone, PyNotImplemented, PyStaticMethod, PyTuple, PyTupleRef, PyType, PyTypeRef,
1918
},
2019
dictdatatype::Dict,
2120
exceptions,
@@ -26,7 +25,6 @@ use crate::{
2625
VirtualMachine,
2726
};
2827
use num_bigint::BigInt;
29-
use num_complex::Complex64;
3028
use num_traits::ToPrimitive;
3129
use std::any::Any;
3230
use std::collections::HashMap;
@@ -186,14 +184,6 @@ impl PyContext {
186184
PyRef::new_ref(PyFloat::from(value), self.types.float_type.clone(), None)
187185
}
188186

189-
pub fn new_complex(&self, value: Complex64) -> PyObjectRef {
190-
PyObject::new(
191-
PyComplex::from(value),
192-
self.types.complex_type.clone(),
193-
None,
194-
)
195-
}
196-
197187
pub fn new_utf8_str<S>(&self, s: S) -> PyObjectRef
198188
where
199189
S: Into<pystr::PyStr>,

vm/src/stdlib/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ impl Node for ast::Constant {
197197
.new_tuple(t.into_iter().map(|c| c.ast_to_object(vm)).collect())
198198
.into(),
199199
ast::Constant::Float(f) => vm.ctx.new_float(f).into(),
200-
ast::Constant::Complex { real, imag } => vm.ctx.new_complex(Complex64::new(real, imag)),
200+
ast::Constant::Complex { real, imag } => vm.new_pyobj(Complex64::new(real, imag)),
201201
ast::Constant::Ellipsis => vm.ctx.ellipsis(),
202202
}
203203
}

0 commit comments

Comments
 (0)