diff --git a/src/lib.rs b/src/lib.rs index 10cb9b6..7159160 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,7 @@ use std::io::BufReader; use ::e57::{CartesianCoordinate, E57Reader}; use numpy::ndarray::Array2; use numpy::{IntoPyArray, PyArray2}; +use pyo3::exceptions::PyRuntimeError; use pyo3::prelude::*; // Keep these docs in sync with e57.pyi. @@ -76,7 +77,7 @@ unsafe fn read_points(py: Python<'_>, filepath: &str) -> PyResult { for pointcloud in pointclouds { let mut iter = file .pointcloud_simple(&pointcloud) - .expect("this file should contain a pointcloud"); + .map_err(|e| PyRuntimeError::new_err(e.to_string()))?; iter.spherical_to_cartesian(true); iter.cartesian_to_spherical(false); iter.intensity_to_color(true); @@ -84,7 +85,7 @@ unsafe fn read_points(py: Python<'_>, filepath: &str) -> PyResult { iter.apply_pose(true); for p in iter { - let p = p.expect("Unable to read next point"); + let p = p.map_err(|e| PyRuntimeError::new_err(e.to_string()))?; // Invalid points are skipped entirely, so colour and intensity // stay row-aligned with the points that are kept. let CartesianCoordinate::Valid { x, y, z } = p.cartesian else { diff --git a/tests/test_e57.py b/tests/test_e57.py index c654370..71a76aa 100644 --- a/tests/test_e57.py +++ b/tests/test_e57.py @@ -1,3 +1,5 @@ +import pathlib + import numpy as np import pytest @@ -202,3 +204,15 @@ def test_invalid_points_rows_aligned(): np.testing.assert_allclose(pointcloud.points, xyz, atol=1e-9) np.testing.assert_allclose(pointcloud.color, rgb, atol=1e-6) np.testing.assert_allclose(pointcloud.intensity[:, 0], intensity, atol=1e-6) + + +def test_corrupt_point_data(tmp_path): + """Valid header and XML but garbage point data raises RuntimeError, not a panic.""" + good = pathlib.Path("testdata/bunnyFloat.e57").read_bytes() + xml_offset = int.from_bytes(good[24:32], "little") # header: XML physical offset + bad = bytearray(good) + bad[2000 : xml_offset - 2000] = b"\xff" * (xml_offset - 4000) + path = tmp_path / "corrupt.e57" + path.write_bytes(bad) + with pytest.raises(RuntimeError, match="Failed to read data packet"): + e57.read_points(str(path))