From 047c6f3e6e034f873777c9d03a494606403ce2d4 Mon Sep 17 00:00:00 2001 From: JustAnotherArchivist Date: Sat, 5 Feb 2022 02:26:17 +0000 Subject: [PATCH] Add JSONDecodeError Fixes #497 --- python/JSONtoObj.c | 3 ++- python/ujson.c | 13 +++++++++++++ python/ujson.h | 1 + 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 python/ujson.h diff --git a/python/JSONtoObj.c b/python/JSONtoObj.c index cc752a47..f2a1a2c7 100644 --- a/python/JSONtoObj.c +++ b/python/JSONtoObj.c @@ -38,6 +38,7 @@ Numeric decoder derived from from TCL library #include #include +#include "ujson.h" //#define PRINTMARK() fprintf(stderr, "%s: MARK(%d)\n", __FILE__, __LINE__) @@ -187,7 +188,7 @@ PyObject* JSONToObj(PyObject* self, PyObject *args, PyObject *kwargs) /* FIXME: It's possible to give a much nicer error message here with actual failing element in input etc*/ - PyErr_Format (PyExc_ValueError, "%s", decoder.errorStr); + PyErr_Format (JSONDecodeError, "%s", decoder.errorStr); if (ret) { diff --git a/python/ujson.c b/python/ujson.c index f7c5e032..c2f37560 100644 --- a/python/ujson.c +++ b/python/ujson.c @@ -38,6 +38,7 @@ Numeric decoder derived from from TCL library #include #include "version.h" +#include "ujson.h" /* objToJSON */ PyObject* objToJSON(PyObject* self, PyObject *args, PyObject *kwargs); @@ -51,6 +52,8 @@ PyObject* objToJSONFile(PyObject* self, PyObject *args, PyObject *kwargs); /* JSONFileToObj */ PyObject* JSONFileToObj(PyObject* self, PyObject *args, PyObject *kwargs); +PyObject* JSONDecodeError; + #define ENCODER_HELP_TEXT "Use ensure_ascii=false to output UTF-8. " \ "Set encode_html_chars=True to encode < > & as unicode escape sequences. "\ @@ -188,5 +191,15 @@ PyMODINIT_FUNC PyInit_ujson(void) PyErr_Clear(); #endif + JSONDecodeError = PyErr_NewException("ujson.JSONDecodeError", PyExc_ValueError, NULL); + Py_XINCREF(JSONDecodeError); + if (PyModule_AddObject(module, "JSONDecodeError", JSONDecodeError) < 0) + { + Py_XDECREF(JSONDecodeError); + Py_CLEAR(JSONDecodeError); + Py_DECREF(module); + return NULL; + } + return module; } diff --git a/python/ujson.h b/python/ujson.h new file mode 100644 index 00000000..cb27c207 --- /dev/null +++ b/python/ujson.h @@ -0,0 +1 @@ +extern PyObject* JSONDecodeError;