From 2c53e8d0254359cb62315d6df4b0cbfd1c4e8a46 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Mon, 11 Oct 2021 00:31:14 +0900 Subject: [PATCH] vm.ctx.new_complex -> PyComplex::new_ref --- vm/src/builtins/code.rs | 4 ++-- vm/src/builtins/complex.rs | 10 ++++++++-- vm/src/pyobject.rs | 14 ++------------ vm/src/stdlib/ast.rs | 2 +- 4 files changed, 13 insertions(+), 17 deletions(-) diff --git a/vm/src/builtins/code.rs b/vm/src/builtins/code.rs index 032d460735..cf8678c5a3 100644 --- a/vm/src/builtins/code.rs +++ b/vm/src/builtins/code.rs @@ -82,7 +82,7 @@ impl ConstantBag for PyObjBag<'_> { let obj = match constant { bytecode::ConstantData::Integer { value } => ctx.new_int(value).into(), bytecode::ConstantData::Float { value } => ctx.new_float(value).into(), - bytecode::ConstantData::Complex { value } => ctx.new_complex(value), + bytecode::ConstantData::Complex { value } => vm.new_pyobj(value), bytecode::ConstantData::Str { value } if value.len() <= 20 => { vm.intern_string(value).into() } @@ -108,7 +108,7 @@ impl ConstantBag for PyObjBag<'_> { let obj = match constant { bytecode::BorrowedConstant::Integer { value } => ctx.new_bigint(value).into(), bytecode::BorrowedConstant::Float { value } => ctx.new_float(value).into(), - bytecode::BorrowedConstant::Complex { value } => ctx.new_complex(value), + bytecode::BorrowedConstant::Complex { value } => vm.new_pyobj(value), bytecode::BorrowedConstant::Str { value } if value.len() <= 20 => { vm.intern_string(value).into() } diff --git a/vm/src/builtins/complex.rs b/vm/src/builtins/complex.rs index 15498caee9..d60277a2d8 100644 --- a/vm/src/builtins/complex.rs +++ b/vm/src/builtins/complex.rs @@ -29,7 +29,7 @@ impl PyValue for PyComplex { impl IntoPyObject for Complex64 { fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { - vm.ctx.new_complex(self) + PyComplex::new_ref(self, &vm.ctx).into() } } @@ -186,12 +186,18 @@ impl SlotConstructor for PyComplex { } } -#[pyimpl(flags(BASETYPE), with(Comparable, Hashable, SlotConstructor))] impl PyComplex { + pub fn new_ref(value: Complex64, ctx: &PyContext) -> PyRef { + PyRef::new_ref(Self::from(value), ctx.types.complex_type.clone(), None) + } + pub fn to_complex(&self) -> Complex64 { self.value } +} +#[pyimpl(flags(BASETYPE), with(Comparable, Hashable, SlotConstructor))] +impl PyComplex { #[pymethod(magic)] fn complex(zelf: PyRef, vm: &VirtualMachine) -> PyRef { if zelf.is(&vm.ctx.types.complex_type) { diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index f8fe3061a9..e50361cf48 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -13,9 +13,8 @@ use crate::{ namespace::PyNamespace, object, pystr, set::{self, PyFrozenSet}, - PyBaseExceptionRef, PyBoundMethod, PyComplex, PyDict, PyDictRef, PyEllipsis, PyFloat, - PyInt, PyIntRef, PyList, PyNone, PyNotImplemented, PyStaticMethod, PyTuple, PyTupleRef, - PyType, PyTypeRef, + PyBaseExceptionRef, PyBoundMethod, PyDict, PyDictRef, PyEllipsis, PyFloat, PyInt, PyIntRef, + PyList, PyNone, PyNotImplemented, PyStaticMethod, PyTuple, PyTupleRef, PyType, PyTypeRef, }, dictdatatype::Dict, exceptions, @@ -26,7 +25,6 @@ use crate::{ VirtualMachine, }; use num_bigint::BigInt; -use num_complex::Complex64; use num_traits::ToPrimitive; use std::any::Any; use std::collections::HashMap; @@ -186,14 +184,6 @@ impl PyContext { PyRef::new_ref(PyFloat::from(value), self.types.float_type.clone(), None) } - pub fn new_complex(&self, value: Complex64) -> PyObjectRef { - PyObject::new( - PyComplex::from(value), - self.types.complex_type.clone(), - None, - ) - } - pub fn new_utf8_str(&self, s: S) -> PyObjectRef where S: Into, diff --git a/vm/src/stdlib/ast.rs b/vm/src/stdlib/ast.rs index 26906f6623..6ba408a1e4 100644 --- a/vm/src/stdlib/ast.rs +++ b/vm/src/stdlib/ast.rs @@ -197,7 +197,7 @@ impl Node for ast::Constant { .new_tuple(t.into_iter().map(|c| c.ast_to_object(vm)).collect()) .into(), ast::Constant::Float(f) => vm.ctx.new_float(f).into(), - ast::Constant::Complex { real, imag } => vm.ctx.new_complex(Complex64::new(real, imag)), + ast::Constant::Complex { real, imag } => vm.new_pyobj(Complex64::new(real, imag)), ast::Constant::Ellipsis => vm.ctx.ellipsis(), } }