Skip to content

Commit 060f094

Browse files
committed
Fix optional feature compilation
1 parent 86185a2 commit 060f094

File tree

2 files changed

+42
-35
lines changed

2 files changed

+42
-35
lines changed

src/types/complex.rs

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl<'py> Neg for &'py PyComplex {
131131
#[cfg(feature = "num-complex")]
132132
mod complex_conversion {
133133
use super::*;
134-
use crate::{FromPyObject, PyErr, PyObject, PyResult, ToPyObject};
134+
use crate::{FromPyObject, Py, PyErr, PyObject, PyResult, ToPyObject};
135135
use num_complex::Complex;
136136

137137
impl PyComplex {
@@ -151,18 +151,18 @@ mod complex_conversion {
151151
impl ToPyObject for Complex<$float> {
152152
#[inline]
153153
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 {
159154
unsafe {
160155
let raw_obj =
161156
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)
163158
}
164159
}
165160
}
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+
}
166166
#[cfg(not(Py_LIMITED_API))]
167167
#[allow(clippy::float_cmp)] // The comparison is for an error value
168168
impl<'source> FromPyObject<'source> for Complex<$float> {
@@ -197,30 +197,36 @@ mod complex_conversion {
197197
complex_conversion!(f32);
198198
complex_conversion!(f64);
199199

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+
}
224230
}
225231
}
226232

src/types/num.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,13 +270,13 @@ mod bigint_conversion {
270270
1,
271271
$is_signed,
272272
);
273-
PyObject::from_owned_ptr_or_panic(py, obj)
273+
py.from_owned_ptr(obj)
274274
}
275275
}
276276
}
277277
impl IntoPy<Py<PyObject>> for $rust_ty {
278-
fn into_py(self, py: Python) -> PyObject {
279-
self.to_object(py)
278+
fn into_py(self, py: Python) -> Py<PyObject> {
279+
self.to_object(py).into()
280280
}
281281
}
282282
impl<'source> FromPyObject<'source> for $rust_ty {
@@ -317,6 +317,7 @@ mod bigint_conversion {
317317
mod test {
318318
use super::*;
319319
use crate::types::{PyDict, PyModule};
320+
use crate::ObjectProtocol;
320321
use indoc::indoc;
321322
use num_traits::{One, Zero};
322323

@@ -415,7 +416,7 @@ mod bigint_conversion {
415416
let value = $value;
416417
println!("{}: {}", stringify!($T), value);
417418
let python_value = value.clone().to_object(py);
418-
let roundtrip_value = python_value.extract::<$T>(py).unwrap();
419+
let roundtrip_value = python_value.extract::<$T>().unwrap();
419420
assert_eq!(value, roundtrip_value);
420421
};
421422
}

0 commit comments

Comments
 (0)