Skip to content

Commit

Permalink
Add JSONDecodeError
Browse files Browse the repository at this point in the history
  • Loading branch information
JustAnotherArchivist committed Feb 5, 2022
1 parent ca1fd23 commit 047c6f3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion python/JSONtoObj.c
Expand Up @@ -38,6 +38,7 @@ Numeric decoder derived from from TCL library

#include <Python.h>
#include <ultrajson.h>
#include "ujson.h"


//#define PRINTMARK() fprintf(stderr, "%s: MARK(%d)\n", __FILE__, __LINE__)
Expand Down Expand Up @@ -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)
{
Expand Down
13 changes: 13 additions & 0 deletions python/ujson.c
Expand Up @@ -38,6 +38,7 @@ Numeric decoder derived from from TCL library

#include <Python.h>
#include "version.h"
#include "ujson.h"

/* objToJSON */
PyObject* objToJSON(PyObject* self, PyObject *args, PyObject *kwargs);
Expand All @@ -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. "\
Expand Down Expand Up @@ -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;
}
1 change: 1 addition & 0 deletions python/ujson.h
@@ -0,0 +1 @@
extern PyObject* JSONDecodeError;

0 comments on commit 047c6f3

Please sign in to comment.