From 46e91d1c84c6b8923af6b814d01c578d30d28433 Mon Sep 17 00:00:00 2001 From: Stijn de Gooijer Date: Mon, 8 Jul 2024 00:26:49 +0200 Subject: [PATCH] Use ComputeError instead --- py-polars/src/dataframe/serde.rs | 19 +++++++------------ py-polars/src/expr/serde.rs | 11 +++++------ py-polars/src/lazyframe/serde.rs | 10 +++++----- py-polars/tests/unit/dataframe/test_serde.py | 2 +- py-polars/tests/unit/lazyframe/test_serde.py | 2 +- 5 files changed, 19 insertions(+), 25 deletions(-) diff --git a/py-polars/src/dataframe/serde.rs b/py-polars/src/dataframe/serde.rs index a6b1f29e23cf..524d894786cd 100644 --- a/py-polars/src/dataframe/serde.rs +++ b/py-polars/src/dataframe/serde.rs @@ -2,12 +2,12 @@ use std::io::{BufReader, BufWriter, Cursor}; use std::ops::Deref; use polars_io::mmap::ReaderBytes; -use pyo3::exceptions::PyValueError; use pyo3::prelude::*; use pyo3::types::PyBytes; use super::PyDataFrame; use crate::error::PyPolarsErr; +use crate::exceptions::ComputeError; use crate::file::{get_file_like, get_mmap_bytes_reader}; use crate::prelude::*; @@ -48,7 +48,7 @@ impl PyDataFrame { let file = get_file_like(py_f, true)?; let writer = BufWriter::new(file); ciborium::into_writer(&self.df, writer) - .map_err(|err| PyValueError::new_err(format!("{err:?}"))) + .map_err(|err| ComputeError::new_err(err.to_string())) } /// Serialize into a JSON string. @@ -57,7 +57,7 @@ impl PyDataFrame { let file = get_file_like(py_f, true)?; let writer = BufWriter::new(file); serde_json::to_writer(writer, &self.df) - .map_err(|err| PyValueError::new_err(format!("{err:?}"))) + .map_err(|err| ComputeError::new_err(err.to_string())) } /// Deserialize a file-like object containing binary data into a DataFrame. @@ -66,7 +66,7 @@ impl PyDataFrame { let file = get_file_like(py_f, false)?; let reader = BufReader::new(file); let df = ciborium::from_reader::(reader) - .map_err(|err| PyValueError::new_err(format!("{err:?}")))?; + .map_err(|err| ComputeError::new_err(err.to_string()))?; Ok(df.into()) } @@ -81,14 +81,9 @@ impl PyDataFrame { py.allow_threads(move || { let mmap_read: ReaderBytes = (&mut mmap_bytes_r).into(); let bytes = mmap_read.deref(); - match serde_json::from_slice::(bytes) { - Ok(df) => Ok(df.into()), - Err(e) => { - let msg = format!("{e}"); - let e = PyPolarsErr::from(PolarsError::ComputeError(msg.into())); - Err(PyErr::from(e)) - }, - } + let df = serde_json::from_slice::(bytes) + .map_err(|err| ComputeError::new_err(err.to_string()))?; + Ok(df.into()) }) } } diff --git a/py-polars/src/expr/serde.rs b/py-polars/src/expr/serde.rs index a2107ee67665..8045a1076d39 100644 --- a/py-polars/src/expr/serde.rs +++ b/py-polars/src/expr/serde.rs @@ -1,14 +1,13 @@ use std::io::{BufReader, BufWriter, Cursor}; use polars::lazy::prelude::Expr; -use pyo3::exceptions::PyValueError; use pyo3::prelude::*; use pyo3::pybacked::PyBackedBytes; use pyo3::types::PyBytes; use crate::error::PyPolarsErr; +use crate::exceptions::ComputeError; use crate::file::get_file_like; -use crate::prelude::polars_err; use crate::PyExpr; #[pymethods] @@ -40,7 +39,7 @@ impl PyExpr { let file = get_file_like(py_f, true)?; let writer = BufWriter::new(file); ciborium::into_writer(&self.inner, writer) - .map_err(|err| PyValueError::new_err(format!("{err:?}"))) + .map_err(|err| ComputeError::new_err(err.to_string())) } /// Serialize into a JSON string. @@ -49,7 +48,7 @@ impl PyExpr { let file = get_file_like(py_f, true)?; let writer = BufWriter::new(file); serde_json::to_writer(writer, &self.inner) - .map_err(|err| PyValueError::new_err(format!("{err:?}"))) + .map_err(|err| ComputeError::new_err(err.to_string())) } /// Deserialize a file-like object containing binary data into an Expr. @@ -58,7 +57,7 @@ impl PyExpr { let file = get_file_like(py_f, false)?; let reader = BufReader::new(file); let expr = ciborium::from_reader::(reader) - .map_err(|err| PyValueError::new_err(format!("{err:?}")))?; + .map_err(|err| ComputeError::new_err(err.to_string()))?; Ok(expr.into()) } @@ -83,7 +82,7 @@ impl PyExpr { let inner: Expr = serde_json::from_str(json).map_err(|_| { let msg = "could not deserialize input into an expression"; - PyPolarsErr::from(polars_err!(ComputeError: msg)) + ComputeError::new_err(msg) })?; Ok(inner.into()) } diff --git a/py-polars/src/lazyframe/serde.rs b/py-polars/src/lazyframe/serde.rs index af49c0f2ebd7..fa51fa9efb37 100644 --- a/py-polars/src/lazyframe/serde.rs +++ b/py-polars/src/lazyframe/serde.rs @@ -1,12 +1,12 @@ use std::io::{BufReader, BufWriter}; -use pyo3::exceptions::PyValueError; use pyo3::prelude::*; use pyo3::pybacked::PyBackedBytes; use pyo3::types::PyBytes; use super::PyLazyFrame; use crate::error::PyPolarsErr; +use crate::exceptions::ComputeError; use crate::file::get_file_like; use crate::prelude::*; @@ -40,7 +40,7 @@ impl PyLazyFrame { let file = get_file_like(py_f, true)?; let writer = BufWriter::new(file); ciborium::into_writer(&self.ldf.logical_plan, writer) - .map_err(|err| PyValueError::new_err(format!("{err:?}"))) + .map_err(|err| ComputeError::new_err(err.to_string())) } /// Serialize into a JSON string. @@ -49,7 +49,7 @@ impl PyLazyFrame { let file = get_file_like(py_f, true)?; let writer = BufWriter::new(file); serde_json::to_writer(writer, &self.ldf.logical_plan) - .map_err(|err| PyValueError::new_err(format!("{err:?}"))) + .map_err(|err| ComputeError::new_err(err.to_string())) } /// Deserialize a file-like object containing binary data into a LazyFrame. @@ -58,7 +58,7 @@ impl PyLazyFrame { let file = get_file_like(py_f, false)?; let reader = BufReader::new(file); let lp = ciborium::from_reader::(reader) - .map_err(|err| PyValueError::new_err(format!("{err:?}")))?; + .map_err(|err| ComputeError::new_err(err.to_string()))?; Ok(LazyFrame::from(lp).into()) } @@ -82,7 +82,7 @@ impl PyLazyFrame { let json = unsafe { std::mem::transmute::<&'_ str, &'static str>(json.as_str()) }; let lp = serde_json::from_str::(json) - .map_err(|err| PyValueError::new_err(format!("{err:?}")))?; + .map_err(|err| ComputeError::new_err(err.to_string()))?; Ok(LazyFrame::from(lp).into()) } } diff --git a/py-polars/tests/unit/dataframe/test_serde.py b/py-polars/tests/unit/dataframe/test_serde.py index 864eb8005d91..04c453f08080 100644 --- a/py-polars/tests/unit/dataframe/test_serde.py +++ b/py-polars/tests/unit/dataframe/test_serde.py @@ -226,6 +226,6 @@ def test_df_deserialize_validation() -> None: def test_df_serialize_invalid_type() -> None: df = pl.DataFrame({"a": [object()]}) with pytest.raises( - ValueError, match="serializing data of type Object is not supported" + ComputeError, match="serializing data of type Object is not supported" ): df.serialize() diff --git a/py-polars/tests/unit/lazyframe/test_serde.py b/py-polars/tests/unit/lazyframe/test_serde.py index 941cf6996cd3..5ec0e6fd14c1 100644 --- a/py-polars/tests/unit/lazyframe/test_serde.py +++ b/py-polars/tests/unit/lazyframe/test_serde.py @@ -91,7 +91,7 @@ def test_lf_serde_to_from_file(lf: pl.LazyFrame, tmp_path: Path) -> None: def test_lf_deserialize_validation() -> None: f = io.BytesIO(b"hello world!") with pytest.raises(ComputeError, match="expected value at line 1 column 1"): - pl.DataFrame.deserialize(f, format="json") + pl.LazyFrame.deserialize(f, format="json") @pytest.mark.write_disk()