Skip to content

Commit

Permalink
Fix optional feature compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Apr 17, 2020
1 parent 86185a2 commit 391f156
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 35 deletions.
68 changes: 37 additions & 31 deletions src/types/complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ impl<'py> Neg for &'py PyComplex {
#[cfg(feature = "num-complex")]
mod complex_conversion {
use super::*;
use crate::{FromPyObject, PyErr, PyObject, PyResult, ToPyObject};
use crate::{FromPyObject, PyErr, PyObject, PyResult, ToPyObject, Py};
use num_complex::Complex;

impl PyComplex {
Expand All @@ -151,18 +151,18 @@ mod complex_conversion {
impl ToPyObject for Complex<$float> {
#[inline]
fn to_object<'p>(&self, py: Python<'p>) -> &'p PyObject {
crate::IntoPy::<PyObject>::into_py(self.to_owned(), py)
}
}
impl crate::IntoPy<Py<PyObject>> for Complex<$float> {
fn into_py(self, py: Python) -> PyObject {
unsafe {
let raw_obj =
ffi::PyComplex_FromDoubles(self.re as c_double, self.im as c_double);
PyObject::from_owned_ptr_or_panic(py, raw_obj)
py.from_owned_ptr(raw_obj)
}
}
}
impl crate::IntoPy<Py<PyObject>> for Complex<$float> {
fn into_py(self, py: Python) -> Py<PyObject> {
self.to_object(py).into()
}
}
#[cfg(not(Py_LIMITED_API))]
#[allow(clippy::float_cmp)] // The comparison is for an error value
impl<'source> FromPyObject<'source> for Complex<$float> {
Expand Down Expand Up @@ -197,30 +197,36 @@ mod complex_conversion {
complex_conversion!(f32);
complex_conversion!(f64);

#[allow(clippy::float_cmp)] // The test wants to ensure that no precision was lost on the Python round-trip
#[test]
fn from_complex() {
let gil = Python::acquire_gil();
let py = gil.python();
let complex = Complex::new(3.0, 1.2);
let py_c = PyComplex::from_complex(py, complex);
assert_eq!(py_c.real(), 3.0);
assert_eq!(py_c.imag(), 1.2);
}
#[test]
fn to_from_complex() {
let gil = Python::acquire_gil();
let py = gil.python();
let val = Complex::new(3.0, 1.2);
let obj = val.to_object(py);
assert_eq!(obj.extract::<Complex<f64>>(py).unwrap(), val);
}
#[test]
fn from_complex_err() {
let gil = Python::acquire_gil();
let py = gil.python();
let obj = vec![1].to_object(py);
assert!(obj.extract::<Complex<f64>>(py).is_err());
#[cfg(test)]
mod test {
use super::*;
use crate::ObjectProtocol;

#[allow(clippy::float_cmp)] // The test wants to ensure that no precision was lost on the Python round-trip
#[test]
fn from_complex() {
let gil = Python::acquire_gil();
let py = gil.python();
let complex = Complex::new(3.0, 1.2);
let py_c = PyComplex::from_complex(py, complex);
assert_eq!(py_c.real(), 3.0);
assert_eq!(py_c.imag(), 1.2);
}
#[test]
fn to_from_complex() {
let gil = Python::acquire_gil();
let py = gil.python();
let val = Complex::new(3.0, 1.2);
let obj = val.to_object(py);
assert_eq!(obj.extract::<Complex<f64>>().unwrap(), val);
}
#[test]
fn from_complex_err() {
let gil = Python::acquire_gil();
let py = gil.python();
let obj = vec![1].to_object(py);
assert!(obj.extract::<Complex<f64>>().is_err());
}
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/types/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,13 +270,13 @@ mod bigint_conversion {
1,
$is_signed,
);
PyObject::from_owned_ptr_or_panic(py, obj)
py.from_owned_ptr(obj)
}
}
}
impl IntoPy<Py<PyObject>> for $rust_ty {
fn into_py(self, py: Python) -> PyObject {
self.to_object(py)
fn into_py(self, py: Python) -> Py<PyObject> {
self.to_object(py).into()
}
}
impl<'source> FromPyObject<'source> for $rust_ty {
Expand Down Expand Up @@ -317,6 +317,7 @@ mod bigint_conversion {
mod test {
use super::*;
use crate::types::{PyDict, PyModule};
use crate::ObjectProtocol;
use indoc::indoc;
use num_traits::{One, Zero};

Expand Down Expand Up @@ -415,7 +416,7 @@ mod bigint_conversion {
let value = $value;
println!("{}: {}", stringify!($T), value);
let python_value = value.clone().to_object(py);
let roundtrip_value = python_value.extract::<$T>(py).unwrap();
let roundtrip_value = python_value.extract::<$T>().unwrap();
assert_eq!(value, roundtrip_value);
};
}
Expand Down

0 comments on commit 391f156

Please sign in to comment.