Skip to content
This repository was archived by the owner on Jan 31, 2024. It is now read-only.

Commit eb561e9

Browse files
author
Andy Chu
committed
[oilshell] Print JSON parse errors to stderr.
We could expose a proper API, but this is OK for now.
1 parent 136c868 commit eb561e9

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

decoder.c

+15-6
Original file line numberDiff line numberDiff line change
@@ -247,16 +247,12 @@ PyObject *_internal_decode(_YajlDecoder *self, char *buffer, unsigned int buflen
247247
yajl_status yrc;
248248
yrc = yajl_parse(parser, (const unsigned char *)(buffer), buflen);
249249
if (yrc != yajl_status_ok) {
250-
//fprintf(stderr, "YAJL ERROR %s\n", yajl_status_to_string(yrc));
251-
PyErr_SetString(PyExc_ValueError, yajl_status_to_string(yrc));
252-
return NULL;
250+
goto error;
253251
}
254252

255253
yrc = yajl_complete_parse(parser);
256254
if (yrc != yajl_status_ok) {
257-
//fprintf(stderr, "YAJL ERROR %s\n", yajl_status_to_string(yrc));
258-
PyErr_SetString(PyExc_ValueError, yajl_status_to_string(yrc));
259-
return NULL;
255+
goto error;
260256
}
261257

262258
yajl_free(parser);
@@ -268,4 +264,17 @@ PyObject *_internal_decode(_YajlDecoder *self, char *buffer, unsigned int buflen
268264
PyObject *root = self->root;
269265
self->root = NULL;
270266
return root;
267+
268+
unsigned char* str;
269+
error:
270+
// TODO: It would be nice to make these parse errors more consistent with
271+
// Oil. And maybe return them rather than printing on stderr.
272+
str = yajl_get_error(parser, 1, buffer, buflen);
273+
fprintf(stderr, "%s", (const char *) str);
274+
yajl_free_error(parser, str);
275+
yajl_free(parser);
276+
277+
//fprintf(stderr, "YAJL ERROR %s\n", yajl_status_to_string(yrc));
278+
PyErr_SetString(PyExc_ValueError, yajl_status_to_string(yrc));
279+
return NULL;
271280
}

tests/unit.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,8 @@ def testAll(self):
284284
rel_path = 'yajl/test/parsing/cases'
285285
cases = os.listdir(rel_path)
286286
for case in cases:
287+
if not case.endswith('.json'):
288+
continue
287289
print(case)
288290
# TODO: This causes SystemError!
289291
if case == 'deep_arrays.json':
@@ -293,7 +295,7 @@ def testAll(self):
293295
try:
294296
obj = yajl.load(f)
295297
except ValueError as e:
296-
print('\t%s: %s' % (case, e))
298+
print('\t%s:\n%s' % (case, e))
297299
else:
298300
try:
299301
j = yajl.dumps(obj)

0 commit comments

Comments
 (0)