* @brief Helper function to check if a Python object represents a mutable buffer. * Returns sz_true_k if the object is mutable (can be written to), sz_false_k if immutable. * Sets a Python exception if immutable. */
| 418 | * Sets a Python exception if immutable. |
| 419 | */ |
| 420 | SZ_INTERNAL sz_bool_t sz_py_is_mutable(PyObject *object) { |
| 421 | if (PyUnicode_Check(object)) { |
| 422 | PyErr_SetString(PyExc_TypeError, "str objects are immutable (use bytearray instead)"); |
| 423 | return sz_false_k; |
| 424 | } |
| 425 | else if (PyBytes_Check(object)) { |
| 426 | PyErr_SetString(PyExc_TypeError, "bytes objects are immutable (use bytearray instead)"); |
| 427 | return sz_false_k; |
| 428 | } |
| 429 | else if (PyMemoryView_Check(object)) { |
| 430 | Py_buffer *view = PyMemoryView_GET_BUFFER(object); |
| 431 | if (view->readonly) { |
| 432 | PyErr_SetString(PyExc_TypeError, "memoryview is read-only"); |
| 433 | return sz_false_k; |
| 434 | } |
| 435 | } |
| 436 | // Everything else is optimistically considered mutable |
| 437 | return sz_true_k; |
| 438 | } |
| 439 | |
| 440 | /** |
| 441 | * @brief Helper function to export a Python string-like object into a `sz_string_view_t`. |
no outgoing calls
no test coverage detected
searching dependent graphs…