@@ -67,7 +67,9 @@ impl PyComplexRef {
67
67
}
68
68
69
69
fn to_complex ( value : PyObjectRef , vm : & VirtualMachine ) -> PyResult < Option < Complex64 > > {
70
- if objtype:: isinstance ( & value, & vm. ctx . int_type ( ) ) {
70
+ if objtype:: isinstance ( & value, & vm. ctx . complex_type ( ) ) {
71
+ Ok ( Some ( get_value ( & value) ) )
72
+ } else if objtype:: isinstance ( & value, & vm. ctx . int_type ( ) ) {
71
73
match objint:: get_value ( & value) . to_f64 ( ) {
72
74
Some ( v) => Ok ( Some ( Complex64 :: new ( v, 0.0 ) ) ) ,
73
75
None => Err ( vm. new_overflow_error ( "int too large to convert to float" . to_string ( ) ) ) ,
@@ -161,6 +163,28 @@ impl PyComplex {
161
163
vm. ctx . new_bool ( result)
162
164
}
163
165
166
+ #[ pymethod( name = "__float__" ) ]
167
+ fn float ( & self , vm : & VirtualMachine ) -> PyResult {
168
+ return Err ( vm. new_type_error ( String :: from ( "Can't convert complex to float" ) ) ) ;
169
+ }
170
+
171
+ #[ pymethod( name = "__int__" ) ]
172
+ fn int ( & self , vm : & VirtualMachine ) -> PyResult {
173
+ return Err ( vm. new_type_error ( String :: from ( "Can't convert complex to int" ) ) ) ;
174
+ }
175
+
176
+ #[ pymethod( name = "__mul__" ) ]
177
+ fn mul ( & self , other : PyObjectRef , vm : & VirtualMachine ) -> PyResult {
178
+ match to_complex ( other, vm) {
179
+ Ok ( Some ( other) ) => Ok ( vm. ctx . new_complex ( Complex64 :: new (
180
+ self . value . re * other. re - self . value . im * other. im ,
181
+ self . value . re * other. im + self . value . im * other. re ,
182
+ ) ) ) ,
183
+ Ok ( None ) => Ok ( vm. ctx . not_implemented ( ) ) ,
184
+ Err ( err) => Err ( err) ,
185
+ }
186
+ }
187
+
164
188
#[ pymethod( name = "__neg__" ) ]
165
189
fn neg ( & self , _vm : & VirtualMachine ) -> PyComplex {
166
190
PyComplex :: from ( -self . value )
0 commit comments