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

Function parse_and_intersect_capabilities

python/stringzilla.c:7666–7704  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7664#pragma endregion
7665
7666static int parse_and_intersect_capabilities(PyObject *caps_obj, sz_capability_t *result) {
7667 if (!caps_obj) {
7668 PyErr_SetString(PyExc_TypeError, "capabilities must be a tuple or list of strings");
7669 return -1;
7670 }
7671 PyObject *seq = PySequence_Fast(caps_obj, "capabilities must be a tuple or list of strings");
7672 if (!seq) return -1;
7673
7674 sz_capability_t requested_caps = 0;
7675 Py_ssize_t n = PySequence_Fast_GET_SIZE(seq);
7676 PyObject **items = PySequence_Fast_ITEMS(seq);
7677
7678 for (Py_ssize_t i = 0; i < n; i++) {
7679 PyObject *item = items[i];
7680 if (!PyUnicode_Check(item)) {
7681 PyErr_SetString(PyExc_TypeError, "capabilities must be strings");
7682 Py_DECREF(seq);
7683 return -1;
7684 }
7685 char const *cap_str = PyUnicode_AsUTF8(item);
7686 if (!cap_str) {
7687 Py_DECREF(seq);
7688 return -1;
7689 }
7690 sz_capability_t flag = sz_capability_from_string_implementation_(cap_str);
7691 if (flag == sz_caps_none_k) {
7692 PyErr_Format(PyExc_ValueError, "Unknown capability: %s", cap_str);
7693 Py_DECREF(seq);
7694 return -1;
7695 }
7696 requested_caps |= flag;
7697 }
7698 Py_DECREF(seq);
7699
7700 // Intersect with hardware capabilities for safety
7701 *result = requested_caps & sz_capabilities();
7702 if (*result == 0) { *result = sz_cap_serial_k; }
7703 return 0;
7704}
7705
7706static char const doc_reset_capabilities[] = //
7707 "reset_capabilities(names) -> None\n\n"

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…