File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ use std::io::BufReader;
44use :: e57:: { CartesianCoordinate , E57Reader } ;
55use numpy:: ndarray:: Array2 ;
66use numpy:: { IntoPyArray , PyArray2 } ;
7+ use pyo3:: exceptions:: PyRuntimeError ;
78use pyo3:: prelude:: * ;
89
910// Keep these docs in sync with e57.pyi.
@@ -76,15 +77,15 @@ unsafe fn read_points(py: Python<'_>, filepath: &str) -> PyResult<E57> {
7677 for pointcloud in pointclouds {
7778 let mut iter = file
7879 . pointcloud_simple ( & pointcloud)
79- . expect ( "this file should contain a pointcloud" ) ;
80+ . map_err ( |e| PyRuntimeError :: new_err ( e . to_string ( ) ) ) ? ;
8081 iter. spherical_to_cartesian ( true ) ;
8182 iter. cartesian_to_spherical ( false ) ;
8283 iter. intensity_to_color ( true ) ;
8384 iter. normalize_intensity ( false ) ;
8485 iter. apply_pose ( true ) ;
8586
8687 for p in iter {
87- let p = p. expect ( "Unable to read next point" ) ;
88+ let p = p. map_err ( |e| PyRuntimeError :: new_err ( e . to_string ( ) ) ) ? ;
8889 // Invalid points are skipped entirely, so colour and intensity
8990 // stay row-aligned with the points that are kept.
9091 let CartesianCoordinate :: Valid { x, y, z } = p. cartesian else {
Original file line number Diff line number Diff line change 1+ import pathlib
2+
13import numpy as np
24import pytest
35
@@ -202,3 +204,15 @@ def test_invalid_points_rows_aligned():
202204 np .testing .assert_allclose (pointcloud .points , xyz , atol = 1e-9 )
203205 np .testing .assert_allclose (pointcloud .color , rgb , atol = 1e-6 )
204206 np .testing .assert_allclose (pointcloud .intensity [:, 0 ], intensity , atol = 1e-6 )
207+
208+
209+ def test_corrupt_point_data (tmp_path ):
210+ """Valid header and XML but garbage point data raises RuntimeError, not a panic."""
211+ good = pathlib .Path ("testdata/bunnyFloat.e57" ).read_bytes ()
212+ xml_offset = int .from_bytes (good [24 :32 ], "little" ) # header: XML physical offset
213+ bad = bytearray (good )
214+ bad [2000 : xml_offset - 2000 ] = b"\xff " * (xml_offset - 4000 )
215+ path = tmp_path / "corrupt.e57"
216+ path .write_bytes (bad )
217+ with pytest .raises (RuntimeError , match = "Failed to read data packet" ):
218+ e57 .read_points (str (path ))
You can’t perform that action at this time.
0 commit comments