Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize the codebase #204

Merged
merged 10 commits into from
Feb 4, 2020
14 changes: 14 additions & 0 deletions src/objects/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,24 @@ impl PyModule {
/// Gets the module filename.
///
/// May fail if the module does not have a `__file__` attribute.
#[allow(deprecated)]
gracinet marked this conversation as resolved.
Show resolved Hide resolved
pub fn filename<'a>(&'a self, py: Python) -> PyResult<&'a str> {
unsafe { self.str_from_ptr(py, ffi::PyModule_GetFilename(self.0.as_ptr())) }
}

/// Gets the module filename object.
///
/// May fail if the module does not have a `__file__` attribute.
#[cfg(feature = "python3-sys")]
pub fn filename_object<'a>(&'a self, py: Python) -> PyResult<PyObject> {
let ptr = unsafe { ffi::PyModule_GetFilenameObject(self.0.as_ptr()) };
if ptr.is_null() {
Err(PyErr::fetch(py))
} else {
Ok(unsafe { PyObject::from_borrowed_ptr(py, ptr) })
}
}

/// Gets a member from the module.
/// This is equivalent to the Python expression: `getattr(module, name)`
pub fn get(&self, py: Python, name: &str) -> PyResult<PyObject> {
Expand Down
88 changes: 50 additions & 38 deletions src/objects/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,20 +125,23 @@ macro_rules! int_fits_c_long(
}
}

/// Converts Python integers to Rust integers.
///
/// Returns OverflowError if the input integer does not fit the Rust type;
/// or TypeError if the input is not an integer.
extract!(obj to $rust_type; py => {
let val = unsafe { ffi::PyLong_AsLong(obj.as_ptr()) };
if val == -1 && PyErr::occurred(py) {
return Err(PyErr::fetch(py));
}
match cast::<c_long, $rust_type>(val) {
Some(v) => Ok(v),
None => Err(overflow_error(py))
extract!(
obj to $rust_type;
/// Converts Python integers to Rust integers.
///
/// Returns OverflowError if the input integer does not fit the Rust type;
/// or TypeError if the input is not an integer.
py => {
let val = unsafe { ffi::PyLong_AsLong(obj.as_ptr()) };
if val == -1 && PyErr::occurred(py) {
return Err(PyErr::fetch(py));
}
match cast::<c_long, $rust_type>(val) {
Some(v) => Ok(v),
None => Err(overflow_error(py))
}
}
});
);
)
);

Expand All @@ -155,17 +158,20 @@ macro_rules! int_fits_larger_int(
}
}

/// Converts Python integers to Rust integers.
///
/// Returns OverflowError if the input integer does not fit the Rust type;
/// or TypeError if the input is not an integer.
extract!(obj to $rust_type; py => {
let val = obj.extract::<$larger_type>(py)?;
match cast::<$larger_type, $rust_type>(val) {
Some(v) => Ok(v),
None => Err(overflow_error(py))
extract!(
obj to $rust_type;
/// Converts Python integers to Rust integers.
///
/// Returns OverflowError if the input integer does not fit the Rust type;
/// or TypeError if the input is not an integer.
py => {
let val = obj.extract::<$larger_type>(py)?;
match cast::<$larger_type, $rust_type>(val) {
Some(v) => Ok(v),
None => Err(overflow_error(py))
}
}
});
);
)
);

Expand Down Expand Up @@ -293,15 +299,18 @@ impl ToPyObject for f64 {
}
}

/// Converts Python `float` to Rust `f64`.
extract!(obj to f64; py => {
let v = unsafe { ffi::PyFloat_AsDouble(obj.as_ptr()) };
if v == -1.0 && PyErr::occurred(py) {
Err(PyErr::fetch(py))
} else {
Ok(v)
extract!(
obj to f64;
/// Converts Python `float` to Rust `f64`.
py => {
let v = unsafe { ffi::PyFloat_AsDouble(obj.as_ptr()) };
if v == -1.0 && PyErr::occurred(py) {
Err(PyErr::fetch(py))
} else {
Ok(v)
}
}
});
);

fn overflow_error(py: Python) -> PyErr {
PyErr::new_lazy_init(py.get_type::<exc::OverflowError>(), None)
Expand All @@ -316,13 +325,16 @@ impl ToPyObject for f32 {
}
}

/// Converts Python `float` to Rust `f32`.
///
/// This conversion loses precision as the 64-bit float from Python gets
/// converted to a 32-bit float. Out-of-range numbers may also overflow to infinity.
extract!(obj to f32; py => {
Ok(obj.extract::<f64>(py)? as f32)
});
extract!(
obj to f32;
/// Converts Python `float` to Rust `f32`.
///
/// This conversion loses precision as the 64-bit float from Python gets
/// converted to a 32-bit float. Out-of-range numbers may also overflow to infinity.
py => {
Ok(obj.extract::<f64>(py)? as f32)
}
);

#[cfg(test)]
mod test {
Expand Down