@@ -17,6 +17,11 @@ pub fn init(context: &PyContext) {
17
17
18
18
context. set_attr ( & complex_type, "__abs__" , context. new_rustfunc ( complex_abs) ) ;
19
19
context. set_attr ( & complex_type, "__add__" , context. new_rustfunc ( complex_add) ) ;
20
+ context. set_attr (
21
+ & complex_type,
22
+ "__radd__" ,
23
+ context. new_rustfunc ( complex_radd) ,
24
+ ) ;
20
25
context. set_attr ( & complex_type, "__eq__" , context. new_rustfunc ( complex_eq) ) ;
21
26
context. set_attr ( & complex_type, "__neg__" , context. new_rustfunc ( complex_neg) ) ;
22
27
context. set_attr ( & complex_type, "__new__" , context. new_rustfunc ( complex_new) ) ;
@@ -107,7 +112,29 @@ fn complex_add(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
107
112
if objtype:: isinstance ( i2, & vm. ctx . complex_type ( ) ) {
108
113
Ok ( vm. ctx . new_complex ( v1 + get_value ( i2) ) )
109
114
} else if objtype:: isinstance ( i2, & vm. ctx . int_type ( ) ) {
110
- Ok ( vm. ctx . new_complex ( Complex64 :: new ( v1. re + objint:: get_value ( i2) . to_f64 ( ) . unwrap ( ) , v1. im ) ) )
115
+ Ok ( vm. ctx . new_complex ( Complex64 :: new (
116
+ v1. re + objint:: get_value ( i2) . to_f64 ( ) . unwrap ( ) ,
117
+ v1. im ,
118
+ ) ) )
119
+ } else {
120
+ Err ( vm. new_type_error ( format ! ( "Cannot add {} and {}" , i. borrow( ) , i2. borrow( ) ) ) )
121
+ }
122
+ }
123
+
124
+ fn complex_radd ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
125
+ arg_check ! (
126
+ vm,
127
+ args,
128
+ required = [ ( i, Some ( vm. ctx. complex_type( ) ) ) , ( i2, None ) ]
129
+ ) ;
130
+
131
+ let v1 = get_value ( i) ;
132
+
133
+ if objtype:: isinstance ( i2, & vm. ctx . int_type ( ) ) {
134
+ Ok ( vm. ctx . new_complex ( Complex64 :: new (
135
+ v1. re + objint:: get_value ( i2) . to_f64 ( ) . unwrap ( ) ,
136
+ v1. im ,
137
+ ) ) )
111
138
} else {
112
139
Err ( vm. new_type_error ( format ! ( "Cannot add {} and {}" , i. borrow( ) , i2. borrow( ) ) ) )
113
140
}
0 commit comments