Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(23985)

Side by Side Diff: Modules/_json.c

Issue 19361: Specialize exceptions thrown by JSON parser
Patch Set: Created 4 years, 9 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/test/test_json/test_scanstring.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #include "Python.h" 1 #include "Python.h"
2 #include "structmember.h" 2 #include "structmember.h"
3 #include "accu.h" 3 #include "accu.h"
4 4
5 #ifdef __GNUC__ 5 #ifdef __GNUC__
6 #define UNUSED __attribute__((__unused__)) 6 #define UNUSED __attribute__((__unused__))
7 #else 7 #else
8 #define UNUSED 8 #define UNUSED
9 #endif 9 #endif
10 10
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
214 output[chars++] = '"'; 214 output[chars++] = '"';
215 #ifdef Py_DEBUG 215 #ifdef Py_DEBUG
216 assert(_PyUnicode_CheckConsistency(rval, 1)); 216 assert(_PyUnicode_CheckConsistency(rval, 1));
217 #endif 217 #endif
218 return rval; 218 return rval;
219 } 219 }
220 220
221 static void 221 static void
222 raise_errmsg(char *msg, PyObject *s, Py_ssize_t end) 222 raise_errmsg(char *msg, PyObject *s, Py_ssize_t end)
223 { 223 {
224 /* Use the Python function json.decoder.errmsg to raise a nice 224 /* Use JSONDecodeError exception to raise a nice looking ValueError subclass */
225 looking ValueError exception */ 225 static PyObject *JSONDecodeError = NULL;
226 static PyObject *errmsg_fn = NULL; 226 PyObject *exc;
227 PyObject *pymsg; 227 if (JSONDecodeError == NULL) {
228 if (errmsg_fn == NULL) {
229 PyObject *decoder = PyImport_ImportModule("json.decoder"); 228 PyObject *decoder = PyImport_ImportModule("json.decoder");
230 if (decoder == NULL) 229 if (decoder == NULL)
231 return; 230 return;
232 errmsg_fn = PyObject_GetAttrString(decoder, "errmsg"); 231 JSONDecodeError = PyObject_GetAttrString(decoder, "JSONDecodeError");
233 Py_DECREF(decoder); 232 Py_DECREF(decoder);
234 if (errmsg_fn == NULL) 233 if (JSONDecodeError == NULL)
235 return; 234 return;
236 } 235 }
237 pymsg = PyObject_CallFunction(errmsg_fn, "(zOn)", msg, s, end); 236 exc = PyObject_CallFunction(JSONDecodeError, "(zOn)", msg, s, end);
238 if (pymsg) { 237 if (exc) {
239 PyErr_SetObject(PyExc_ValueError, pymsg); 238 PyErr_SetObject(JSONDecodeError, exc);
240 Py_DECREF(pymsg); 239 Py_DECREF(exc);
241 } 240 }
242 } 241 }
243 242
244 static void 243 static void
245 raise_stop_iteration(Py_ssize_t idx) 244 raise_stop_iteration(Py_ssize_t idx)
246 { 245 {
247 PyObject *value = PyLong_FromSsize_t(idx); 246 PyObject *value = PyLong_FromSsize_t(idx);
248 if (value != NULL) { 247 if (value != NULL) {
249 PyErr_SetObject(PyExc_StopIteration, value); 248 PyErr_SetObject(PyExc_StopIteration, value);
250 Py_DECREF(value); 249 Py_DECREF(value);
(...skipping 1631 matching lines...) Expand 10 before | Expand all | Expand 10 after
1882 Py_INCREF((PyObject*)&PyEncoderType); 1881 Py_INCREF((PyObject*)&PyEncoderType);
1883 if (PyModule_AddObject(m, "make_encoder", (PyObject*)&PyEncoderType) < 0) { 1882 if (PyModule_AddObject(m, "make_encoder", (PyObject*)&PyEncoderType) < 0) {
1884 Py_DECREF((PyObject*)&PyEncoderType); 1883 Py_DECREF((PyObject*)&PyEncoderType);
1885 goto fail; 1884 goto fail;
1886 } 1885 }
1887 return m; 1886 return m;
1888 fail: 1887 fail:
1889 Py_DECREF(m); 1888 Py_DECREF(m);
1890 return NULL; 1889 return NULL;
1891 } 1890 }
OLDNEW
« no previous file with comments | « Lib/test/test_json/test_scanstring.py ('k') | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+