Skip to content

Commit 4f14ae5

Browse files
Copilotgerlero
andcommitted
Add safety documentation for unsafe bytearray access
Co-authored-by: gerlero <[email protected]>
1 parent 7818f50 commit 4f14ae5

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

rust_src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ fn skip(contents: &Bound<'_, pyo3::types::PyAny>, mut pos: usize, newline_ok: bo
4242
let contents = if let Ok(bytes) = contents.downcast::<pyo3::types::PyBytes>() {
4343
bytes.as_bytes()
4444
} else if let Ok(bytearray) = contents.downcast::<pyo3::types::PyByteArray>() {
45+
// SAFETY: This is safe because:
46+
// 1. We only read from the bytearray (immutable access)
47+
// 2. The reference doesn't escape this function
48+
// 3. The GIL prevents concurrent modification by Python code
4549
unsafe { bytearray.as_bytes() }
4650
} else {
4751
return Err(PyErr::new::<pyo3::exceptions::PyTypeError, _>(

0 commit comments

Comments
 (0)