|
| 1 | +// Small shim to provide simdjson_error_handler symbol when the vendored |
| 2 | +// simdjson/util.cpp is not compiled into this extension. This avoids |
| 3 | +// import-time undefined-symbol errors on macOS. The implementation is a |
| 4 | +// minimal translator mapping simdjson exceptions to Python exceptions. |
| 5 | + |
| 6 | +// Minimal shim that converts any active C++ exception into a Python exception. |
| 7 | +// Avoid including the full simdjson headers here to prevent symbol duplication |
| 8 | +// and large in-object code generation; catch std::exception to retrieve |
| 9 | +// the message when available. |
| 10 | + |
| 11 | +#define PY_SSIZE_T_CLEAN |
| 12 | +#include <Python.h> |
| 13 | +#include <exception> |
| 14 | + |
| 15 | +void simdjson_error_handler() { |
| 16 | + try { |
| 17 | + if (PyErr_Occurred()) { |
| 18 | + return; // preserve existing Python exception |
| 19 | + } else { |
| 20 | + throw; // rethrow the active C++ exception |
| 21 | + } |
| 22 | + } catch (const std::exception &e) { |
| 23 | + const char *msg = e.what(); |
| 24 | + if (!msg) msg = "simdjson: unknown error"; |
| 25 | + |
| 26 | + // Map known simdjson error codes (present in exception message) |
| 27 | + // to the appropriate Python exception types. This mirrors the |
| 28 | + // behavior in the original vendored `util.cpp` without |
| 29 | + // including simdjson headers here (avoids symbol duplication). |
| 30 | + if (strstr(msg, "NO_SUCH_FIELD") != NULL) { |
| 31 | + PyErr_SetString(PyExc_KeyError, msg); |
| 32 | + return; |
| 33 | + } |
| 34 | + if (strstr(msg, "INDEX_OUT_OF_BOUNDS") != NULL) { |
| 35 | + PyErr_SetString(PyExc_IndexError, msg); |
| 36 | + return; |
| 37 | + } |
| 38 | + if (strstr(msg, "INCORRECT_TYPE") != NULL) { |
| 39 | + PyErr_SetString(PyExc_TypeError, msg); |
| 40 | + return; |
| 41 | + } |
| 42 | + if (strstr(msg, "MEMALLOC") != NULL) { |
| 43 | + PyErr_SetNone(PyExc_MemoryError); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + // ValueError group |
| 48 | + if (strstr(msg, "EMPTY") != NULL || strstr(msg, "STRING_ERROR") != NULL || |
| 49 | + strstr(msg, "T_ATOM_ERROR") != NULL || strstr(msg, "F_ATOM_ERROR") != NULL || |
| 50 | + strstr(msg, "N_ATOM_ERROR") != NULL || strstr(msg, "NUMBER_ERROR") != NULL || |
| 51 | + strstr(msg, "UNESCAPED_CHARS") != NULL || strstr(msg, "UNCLOSED_STRING") != NULL || |
| 52 | + strstr(msg, "NUMBER_OUT_OF_RANGE") != NULL || strstr(msg, "INVALID_JSON_POINTER") != NULL || |
| 53 | + strstr(msg, "INVALID_URI_FRAGMENT") != NULL || strstr(msg, "CAPACITY") != NULL || |
| 54 | + strstr(msg, "TAPE_ERROR") != NULL) { |
| 55 | + PyErr_SetString(PyExc_ValueError, msg); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + if (strstr(msg, "IO_ERROR") != NULL) { |
| 60 | + PyErr_SetString(PyExc_IOError, msg); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + if (strstr(msg, "UTF8_ERROR") != NULL) { |
| 65 | + PyObject *unicode_error = PyObject_CallFunction( |
| 66 | + PyExc_UnicodeDecodeError, |
| 67 | + "sy#nns", |
| 68 | + "utf-8", |
| 69 | + "", |
| 70 | + 0, |
| 71 | + 0, |
| 72 | + 1, |
| 73 | + msg |
| 74 | + ); |
| 75 | + if (unicode_error) { |
| 76 | + PyErr_SetObject(PyExc_UnicodeDecodeError, unicode_error); |
| 77 | + Py_XDECREF(unicode_error); |
| 78 | + } else { |
| 79 | + PyErr_SetString(PyExc_UnicodeDecodeError, msg); |
| 80 | + } |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + PyErr_SetString(PyExc_RuntimeError, msg); |
| 85 | + return; |
| 86 | + } catch (...) { |
| 87 | + PyErr_SetString(PyExc_RuntimeError, "simdjson: unknown error"); |
| 88 | + return; |
| 89 | + } |
| 90 | +} |
0 commit comments