Skip to content

Commit dc9d5e8

Browse files
authored
Add clarifying comments for code review feedback
- Document that const cast to (char*) is safe because PyBUF_READ makes view read-only - Add detailed comment explaining PyErr_Occurred() check is necessary and correct - NpyIter can return 0 for TWO reasons: end of iteration OR error - Checking PyErr_Occurred() after loop is the standard NumPy iterator pattern
1 parent b181e3a commit dc9d5e8

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

src/methods.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,7 +1269,9 @@ write_array_to_file(PyObject *Py_UNUSED(m), PyObject *args, PyObject *kwargs)
12691269
}
12701270
}
12711271
else {
1272-
/* Fall back to Python write() method */
1272+
/* Fall back to Python write() method.
1273+
* Note: PyMemoryView_FromMemory requires non-const char*, but we pass
1274+
* PyBUF_READ flag which makes the view read-only, so the cast is safe. */
12731275
PyObject *buffer = PyMemoryView_FromMemory((char *)data_to_write, chunk_size, PyBUF_READ);
12741276
if (buffer == NULL) {
12751277
goto fail;
@@ -1300,7 +1302,10 @@ write_array_to_file(PyObject *Py_UNUSED(m), PyObject *args, PyObject *kwargs)
13001302
}
13011303
} while(iternext(iter));
13021304

1303-
/* Check if iteration stopped due to an error */
1305+
/* Check if iteration stopped due to an error.
1306+
* NpyIter can return 0 for TWO reasons: end of iteration OR error.
1307+
* We MUST check PyErr_Occurred() to distinguish between the two.
1308+
* This is the standard pattern for NpyIter error handling. */
13041309
if (PyErr_Occurred()) {
13051310
goto fail;
13061311
}

0 commit comments

Comments
 (0)