Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb50f0be

Browse files
Merge pull requestRustPython#596 from RustPython/joey/complex-to-any
Convert complex to Any payload
2 parentsa7c3f85 +f820aeb commitb50f0be

File tree

2 files changed

+38
-20
lines changed

2 files changed

+38
-20
lines changed

‎vm/src/obj/objcomplex.rs‎

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,30 @@ use super::objfloat;
22
usesuper::objint;
33
usesuper::objtype;
44
usecrate::pyobject::{
5-
PyContext,PyFuncArgs,PyObject,PyObjectPayload,PyObjectRef,PyResult,TypeProtocol,
5+
PyContext,PyFuncArgs,PyObject,PyObjectPayload,PyObjectPayload2,PyObjectRef,PyResult,
6+
TypeProtocol,
67
};
78
usecrate::vm::VirtualMachine;
89
use num_complex::Complex64;
910
use num_traits::ToPrimitive;
1011

12+
#[derive(Debug,Copy,Clone,PartialEq)]
13+
pubstructPyComplex{
14+
value:Complex64,
15+
}
16+
17+
implPyObjectPayload2forPyComplex{
18+
fnrequired_type(ctx:&PyContext) ->PyObjectRef{
19+
ctx.complex_type()
20+
}
21+
}
22+
23+
implFrom<Complex64>forPyComplex{
24+
fnfrom(value:Complex64) ->Self{
25+
PyComplex{ value}
26+
}
27+
}
28+
1129
pubfninit(context:&PyContext){
1230
let complex_type =&context.complex_type;
1331

@@ -45,11 +63,7 @@ pub fn init(context: &PyContext) {
4563
}
4664

4765
pubfnget_value(obj:&PyObjectRef) ->Complex64{
48-
ifletPyObjectPayload::Complex{ value} =&obj.payload{
49-
*value
50-
}else{
51-
panic!("Inner error getting complex");
52-
}
66+
obj.payload::<PyComplex>().unwrap().value
5367
}
5468

5569
fncomplex_new(vm:&mutVirtualMachine,args:PyFuncArgs) ->PyResult{
@@ -77,7 +91,9 @@ fn complex_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
7791
let value =Complex64::new(real, imag);
7892

7993
Ok(PyObject::new(
80-
PyObjectPayload::Complex{ value},
94+
PyObjectPayload::AnyRustValue{
95+
value:Box::new(PyComplex{ value}),
96+
},
8197
cls.clone(),
8298
))
8399
}

‎vm/src/pyobject.rs‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,23 @@ use std::iter;
55
use std::ops::RangeInclusive;
66
use std::rc::{Rc,Weak};
77

8+
use num_bigint::BigInt;
9+
use num_bigint::ToBigInt;
10+
use num_complex::Complex64;
11+
use num_traits::{One,Zero};
12+
813
usecrate::bytecode;
914
usecrate::exceptions;
1015
usecrate::frame::{Frame,Scope,ScopeRef};
1116
usecrate::obj::objbool;
1217
usecrate::obj::objbytearray;
1318
usecrate::obj::objbytes;
1419
usecrate::obj::objcode;
15-
usecrate::obj::objcomplex;
20+
usecrate::obj::objcomplex::{self,PyComplex};
1621
usecrate::obj::objdict;
1722
usecrate::obj::objenumerate;
1823
usecrate::obj::objfilter;
19-
usecrate::obj::objfloat;
24+
usecrate::obj::objfloat::{self,PyFloat};
2025
usecrate::obj::objframe;
2126
usecrate::obj::objfunction;
2227
usecrate::obj::objgenerator;
@@ -37,10 +42,6 @@ use crate::obj::objtuple;
3742
usecrate::obj::objtype;
3843
usecrate::obj::objzip;
3944
usecrate::vm::VirtualMachine;
40-
use num_bigint::BigInt;
41-
use num_bigint::ToBigInt;
42-
use num_complex::Complex64;
43-
use num_traits::{One,Zero};
4445

4546
/* Python objects and references.
4647
@@ -463,14 +464,19 @@ impl PyContext {
463464
pubfnnew_float(&self,value:f64) ->PyObjectRef{
464465
PyObject::new(
465466
PyObjectPayload::AnyRustValue{
466-
value:Box::new(objfloat::PyFloat::from(value)),
467+
value:Box::new(PyFloat::from(value)),
467468
},
468469
self.float_type(),
469470
)
470471
}
471472

472-
pubfnnew_complex(&self,i:Complex64) ->PyObjectRef{
473-
PyObject::new(PyObjectPayload::Complex{value: i},self.complex_type())
473+
pubfnnew_complex(&self,value:Complex64) ->PyObjectRef{
474+
PyObject::new(
475+
PyObjectPayload::AnyRustValue{
476+
value:Box::new(PyComplex::from(value)),
477+
},
478+
self.complex_type(),
479+
)
474480
}
475481

476482
pubfnnew_str(&self,s:String) ->PyObjectRef{
@@ -1408,9 +1414,6 @@ pub enum PyObjectPayload {
14081414
Integer{
14091415
value:BigInt,
14101416
},
1411-
Complex{
1412-
value:Complex64,
1413-
},
14141417
Sequence{
14151418
elements:RefCell<Vec<PyObjectRef>>,
14161419
},
@@ -1494,7 +1497,6 @@ impl fmt::Debug for PyObjectPayload {
14941497
fnfmt(&self,f:&mut fmt::Formatter) -> fmt::Result{
14951498
matchself{
14961499
PyObjectPayload::Integer{ref value} =>write!(f,"int {}", value),
1497-
PyObjectPayload::Complex{ref value} =>write!(f,"complex {}", value),
14981500
PyObjectPayload::MemoryView{ref obj} =>write!(f,"bytes/bytearray {:?}", obj),
14991501
PyObjectPayload::Sequence{ ..} =>write!(f,"list or tuple"),
15001502
PyObjectPayload::Dict{ ..} =>write!(f,"dict"),

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp