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

Function Str_like_decode

python/stringzilla.c:2407–2449  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2405 " UnicodeDecodeError: If decoding fails.";
2406
2407static PyObject *Str_like_decode(PyObject *self, PyObject *const *args, Py_ssize_t positional_args_count,
2408 PyObject *args_names_tuple) {
2409 int is_member = self != NULL && PyObject_TypeCheck(self, &StrType);
2410 if (positional_args_count < !is_member || positional_args_count > !is_member + 2) {
2411 PyErr_Format(PyExc_TypeError, "Invalid number of arguments");
2412 return NULL;
2413 }
2414
2415 PyObject *text_obj = is_member ? self : args[0];
2416 PyObject *encoding_obj = positional_args_count > !is_member + 0 ? args[!is_member + 0] : NULL;
2417 PyObject *errors_obj = positional_args_count > !is_member + 1 ? args[!is_member + 1] : NULL;
2418
2419 if (args_names_tuple) {
2420 Py_ssize_t args_names_count = PyTuple_GET_SIZE(args_names_tuple);
2421 for (Py_ssize_t i = 0; i < args_names_count; ++i) {
2422 PyObject *key = PyTuple_GET_ITEM(args_names_tuple, i);
2423 PyObject *value = args[positional_args_count + i];
2424 if (PyUnicode_CompareWithASCIIString(key, "encoding") == 0 && !encoding_obj) { encoding_obj = value; }
2425 else if (PyUnicode_CompareWithASCIIString(key, "errors") == 0 && !errors_obj) { errors_obj = value; }
2426 else if (PyErr_Format(PyExc_TypeError, "Got an unexpected keyword argument '%U'", key))
2427 return NULL;
2428 }
2429 }
2430
2431 // Convert `encoding` and `errors` to `NULL` if they are `None`
2432 if (encoding_obj == Py_None) encoding_obj = NULL;
2433 if (errors_obj == Py_None) errors_obj = NULL;
2434
2435 sz_string_view_t text, encoding, errors;
2436 if ((!sz_py_export_string_like(text_obj, &text.start, &text.length)) ||
2437 (encoding_obj && !sz_py_export_string_like(encoding_obj, &encoding.start, &encoding.length)) ||
2438 (errors_obj && !sz_py_export_string_like(errors_obj, &errors.start, &errors.length))) {
2439 wrap_current_exception("text, encoding, and errors must be string-like");
2440 return NULL;
2441 }
2442
2443 if (encoding_obj == NULL) encoding = (sz_string_view_t) {"utf-8", 5};
2444 if (errors_obj == NULL) errors = (sz_string_view_t) {"strict", 6};
2445
2446 // Python docs: https://docs.python.org/3/library/stdtypes.html#bytes.decode
2447 // CPython docs: https://docs.python.org/3/c-api/unicode.html#c.PyUnicode_Decode
2448 return PyUnicode_Decode(text.start, text.length, encoding.start, errors.start);
2449}
2450
2451static char const doc_write_to[] = //
2452 "Write the string to a file.\n"

Callers

nothing calls this directly

Calls 2

sz_py_export_string_likeFunction · 0.85
wrap_current_exceptionFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…