Skip to content

Commit eca0db4

Browse files
committed
Update tests
1 parent 0e02b8d commit eca0db4

File tree

2 files changed

+20
-34
lines changed

2 files changed

+20
-34
lines changed

pandas/io/sas/sas.pyx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ cdef struct Buffer:
2323

2424

2525
cdef inline uint8_t buf_get(Buffer buf, size_t offset) except? 0:
26-
assert offset < buf.length
26+
assert offset < buf.length, f"Out of bounds read"
2727
return buf.data[offset]
2828

2929

3030
cdef inline void buf_set(Buffer buf, size_t offset, uint8_t value) except *:
31-
assert offset < buf.length
31+
assert offset < buf.length, "Out of bounds write"
3232
buf.data[offset] = value
3333

3434

3535
cdef inline bytes buf_as_bytes(Buffer buf, size_t offset, size_t length):
36-
assert offset + length <= buf.length
36+
assert offset + length <= buf.length, "Out of bounds read"
3737
return buf.data[offset:offset+length]
3838

3939

@@ -428,7 +428,7 @@ cdef class Parser:
428428
object[:, :] string_chunk
429429
bint compressed
430430

431-
assert offset + length <= self.cached_page_len
431+
assert offset + length <= self.cached_page_len, "Out of bounds read"
432432
source = Buffer(&self.cached_page[offset], length)
433433

434434
compressed = self.decompress != NULL and length < self.row_length

pandas/tests/io/sas/test_sas7bdat.py

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -350,34 +350,20 @@ def test_meta2_page(datapath):
350350
assert len(df) == 1000
351351

352352

353-
@pytest.mark.parametrize("test_file", ["test2.sas7bdat", "test3.sas7bdat"])
354-
def test_exception_propagation_rdc_rle_decompress(datapath, monkeypatch, test_file):
355-
"""Errors in RLE/RDC decompression should propagate the same error."""
356-
orig_np_zeros = np.zeros
357-
358-
def _patched_zeros(size, dtype):
359-
if isinstance(size, int):
360-
# np.zeros() call in {rdc,rle}_decompress
361-
raise Exception("Test exception")
362-
else:
363-
# Other calls to np.zeros
364-
return orig_np_zeros(size, dtype)
365-
366-
monkeypatch.setattr(np, "zeros", _patched_zeros)
367-
368-
with pytest.raises(Exception, match="^Test exception$"):
369-
pd.read_sas(datapath("io", "sas", "data", test_file))
370-
371-
372-
def test_exception_propagation_rle_decompress(tmp_path, datapath):
373-
"""Illegal control byte in RLE decompressor should raise the correct ValueError."""
374-
with open(datapath("io", "sas", "data", "test2.sas7bdat"), "rb") as f:
353+
@pytest.mark.parametrize(
354+
"test_file, override_offset, override_value, expected_msg",
355+
[
356+
("test2.sas7bdat", 0x10000 + 55229, 0x80 | 0x0F, "Out of bounds"),
357+
("test2.sas7bdat", 0x10000 + 55229, 0x10, "unknown control byte"),
358+
("test3.sas7bdat", 118170, 184, "Out of bounds"),
359+
],
360+
)
361+
def test_rle_rdc_exceptions(
362+
datapath, test_file, override_offset, override_value, expected_msg
363+
):
364+
"""Errors in RLE/RDC decompression should propagate."""
365+
with open(datapath("io", "sas", "data", test_file), "rb") as f:
375366
data = bytearray(f.read())
376-
invalid_control_byte = 0x10
377-
page_offset = 0x10000
378-
control_byte_pos = 55229
379-
data[page_offset + control_byte_pos] = invalid_control_byte
380-
tmp_file = tmp_path / "test2.sas7bdat"
381-
tmp_file.write_bytes(data)
382-
with pytest.raises(ValueError, match="unknown control byte"):
383-
pd.read_sas(tmp_file)
367+
data[override_offset] = override_value
368+
with pytest.raises(Exception, match=expected_msg):
369+
pd.read_sas(io.BytesIO(data), format="sas7bdat")

0 commit comments

Comments
 (0)