@@ -131,7 +131,7 @@ impl<'py> Neg for &'py PyComplex {
131
131
#[ cfg( feature = "num-complex" ) ]
132
132
mod complex_conversion {
133
133
use super :: * ;
134
- use crate :: { FromPyObject , PyErr , PyObject , PyResult , ToPyObject } ;
134
+ use crate :: { FromPyObject , Py , PyErr , PyObject , PyResult , ToPyObject } ;
135
135
use num_complex:: Complex ;
136
136
137
137
impl PyComplex {
@@ -151,18 +151,18 @@ mod complex_conversion {
151
151
impl ToPyObject for Complex <$float> {
152
152
#[ inline]
153
153
fn to_object<' p>( & self , py: Python <' p>) -> & ' p PyObject {
154
- crate :: IntoPy :: <PyObject >:: into_py( self . to_owned( ) , py)
155
- }
156
- }
157
- impl crate :: IntoPy <Py <PyObject >> for Complex <$float> {
158
- fn into_py( self , py: Python ) -> PyObject {
159
154
unsafe {
160
155
let raw_obj =
161
156
ffi:: PyComplex_FromDoubles ( self . re as c_double, self . im as c_double) ;
162
- PyObject :: from_owned_ptr_or_panic ( py , raw_obj)
157
+ py . from_owned_ptr ( raw_obj)
163
158
}
164
159
}
165
160
}
161
+ impl crate :: IntoPy <Py <PyObject >> for Complex <$float> {
162
+ fn into_py( self , py: Python ) -> Py <PyObject > {
163
+ self . to_object( py) . into( )
164
+ }
165
+ }
166
166
#[ cfg( not( Py_LIMITED_API ) ) ]
167
167
#[ allow( clippy:: float_cmp) ] // The comparison is for an error value
168
168
impl <' source> FromPyObject <' source> for Complex <$float> {
@@ -197,30 +197,36 @@ mod complex_conversion {
197
197
complex_conversion ! ( f32 ) ;
198
198
complex_conversion ! ( f64 ) ;
199
199
200
- #[ allow( clippy:: float_cmp) ] // The test wants to ensure that no precision was lost on the Python round-trip
201
- #[ test]
202
- fn from_complex ( ) {
203
- let gil = Python :: acquire_gil ( ) ;
204
- let py = gil. python ( ) ;
205
- let complex = Complex :: new ( 3.0 , 1.2 ) ;
206
- let py_c = PyComplex :: from_complex ( py, complex) ;
207
- assert_eq ! ( py_c. real( ) , 3.0 ) ;
208
- assert_eq ! ( py_c. imag( ) , 1.2 ) ;
209
- }
210
- #[ test]
211
- fn to_from_complex ( ) {
212
- let gil = Python :: acquire_gil ( ) ;
213
- let py = gil. python ( ) ;
214
- let val = Complex :: new ( 3.0 , 1.2 ) ;
215
- let obj = val. to_object ( py) ;
216
- assert_eq ! ( obj. extract:: <Complex <f64 >>( py) . unwrap( ) , val) ;
217
- }
218
- #[ test]
219
- fn from_complex_err ( ) {
220
- let gil = Python :: acquire_gil ( ) ;
221
- let py = gil. python ( ) ;
222
- let obj = vec ! [ 1 ] . to_object ( py) ;
223
- assert ! ( obj. extract:: <Complex <f64 >>( py) . is_err( ) ) ;
200
+ #[ cfg( test) ]
201
+ mod test {
202
+ use super :: * ;
203
+ use crate :: ObjectProtocol ;
204
+
205
+ #[ allow( clippy:: float_cmp) ] // The test wants to ensure that no precision was lost on the Python round-trip
206
+ #[ test]
207
+ fn from_complex ( ) {
208
+ let gil = Python :: acquire_gil ( ) ;
209
+ let py = gil. python ( ) ;
210
+ let complex = Complex :: new ( 3.0 , 1.2 ) ;
211
+ let py_c = PyComplex :: from_complex ( py, complex) ;
212
+ assert_eq ! ( py_c. real( ) , 3.0 ) ;
213
+ assert_eq ! ( py_c. imag( ) , 1.2 ) ;
214
+ }
215
+ #[ test]
216
+ fn to_from_complex ( ) {
217
+ let gil = Python :: acquire_gil ( ) ;
218
+ let py = gil. python ( ) ;
219
+ let val = Complex :: new ( 3.0 , 1.2 ) ;
220
+ let obj = val. to_object ( py) ;
221
+ assert_eq ! ( obj. extract:: <Complex <f64 >>( ) . unwrap( ) , val) ;
222
+ }
223
+ #[ test]
224
+ fn from_complex_err ( ) {
225
+ let gil = Python :: acquire_gil ( ) ;
226
+ let py = gil. python ( ) ;
227
+ let obj = vec ! [ 1 ] . to_object ( py) ;
228
+ assert ! ( obj. extract:: <Complex <f64 >>( ) . is_err( ) ) ;
229
+ }
224
230
}
225
231
}
226
232
0 commit comments