Skip to content

Commit

Permalink
Python: Runtime warning when compiled in debug mode (#859)
Browse files Browse the repository at this point in the history
Closes #851
  • Loading branch information
kylebarron authored Nov 13, 2024
1 parent 01648f7 commit fc2a563
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
21 changes: 20 additions & 1 deletion python/geoarrow-compute/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use pyo3::exceptions::PyRuntimeWarning;
use pyo3::intern;
use pyo3::prelude::*;
use pyo3::types::PyTuple;
mod algorithm;
pub mod broadcasting;
pub mod ffi;
Expand All @@ -11,9 +14,25 @@ fn ___version() -> &'static str {
VERSION
}

/// Raise RuntimeWarning for debug builds
#[pyfunction]
fn check_debug_build(py: Python) -> PyResult<()> {
#[cfg(debug_assertions)]
{
let warnings_mod = py.import_bound(intern!(py, "warnings"))?;
let warning = PyRuntimeWarning::new_err(
"geoarrow-rust-compute has not been compiled in release mode. Performance will be degraded.",
);
let args = PyTuple::new_bound(py, vec![warning.into_py(py)]);
warnings_mod.call_method1(intern!(py, "warn"), args)?;
}
Ok(())
}

/// A Python module implemented in Rust.
#[pymodule]
fn _compute(_py: Python, m: &Bound<PyModule>) -> PyResult<()> {
fn _compute(py: Python, m: &Bound<PyModule>) -> PyResult<()> {
check_debug_build(py)?;
m.add_wrapped(wrap_pyfunction!(___version))?;

// Top-level array/chunked array functions
Expand Down
21 changes: 20 additions & 1 deletion python/geoarrow-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use pyo3::exceptions::PyRuntimeWarning;
use pyo3::intern;
use pyo3::prelude::*;
use pyo3::types::PyTuple;
mod constructors;
pub(crate) mod crs;
pub mod ffi;
Expand All @@ -12,9 +15,25 @@ fn ___version() -> &'static str {
VERSION
}

/// Raise RuntimeWarning for debug builds
#[pyfunction]
fn check_debug_build(py: Python) -> PyResult<()> {
#[cfg(debug_assertions)]
{
let warnings_mod = py.import_bound(intern!(py, "warnings"))?;
let warning = PyRuntimeWarning::new_err(
"geoarrow-rust-core has not been compiled in release mode. Performance will be degraded.",
);
let args = PyTuple::new_bound(py, vec![warning.into_py(py)]);
warnings_mod.call_method1(intern!(py, "warn"), args)?;
}
Ok(())
}

/// A Python module implemented in Rust.
#[pymodule]
fn _rust(_py: Python, m: &Bound<PyModule>) -> PyResult<()> {
fn _rust(py: Python, m: &Bound<PyModule>) -> PyResult<()> {
check_debug_build(py)?;
m.add_wrapped(wrap_pyfunction!(___version))?;

m.add_class::<pyo3_geoarrow::PyGeometry>()?;
Expand Down
21 changes: 20 additions & 1 deletion python/geoarrow-io/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use pyo3::exceptions::PyRuntimeWarning;
use pyo3::intern;
use pyo3::prelude::*;
use pyo3::types::PyTuple;
pub(crate) mod crs;
pub mod error;
// pub mod ffi;
Expand All @@ -12,9 +15,25 @@ fn ___version() -> &'static str {
VERSION
}

/// Raise RuntimeWarning for debug builds
#[pyfunction]
fn check_debug_build(py: Python) -> PyResult<()> {
#[cfg(debug_assertions)]
{
let warnings_mod = py.import_bound(intern!(py, "warnings"))?;
let warning = PyRuntimeWarning::new_err(
"geoarrow-rust-io has not been compiled in release mode. Performance will be degraded.",
);
let args = PyTuple::new_bound(py, vec![warning.into_py(py)]);
warnings_mod.call_method1(intern!(py, "warn"), args)?;
}
Ok(())
}

/// A Python module implemented in Rust.
#[pymodule]
fn _io(_py: Python, m: &Bound<PyModule>) -> PyResult<()> {
fn _io(py: Python, m: &Bound<PyModule>) -> PyResult<()> {
check_debug_build(py)?;
m.add_wrapped(wrap_pyfunction!(___version))?;

// Async IO
Expand Down

0 comments on commit fc2a563

Please sign in to comment.