MCPcopy Create free account
hub / github.com/ashvardanian/StringZilla / sz_py_is_mutable

Function sz_py_is_mutable

python/stringzilla.c:420–438  ·  view source on GitHub ↗

* @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. */

Source from the content-addressed store, hash-verified

418 * Sets a Python exception if immutable.
419 */
420SZ_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`.

Callers 2

Str_like_fill_randomFunction · 0.85
Str_like_translateFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…