~ai-2/ijson/ptr-trunc

« back to all changes in this revision

Viewing changes to ijson/parse.py

  • Committer: Ivan Sagalaev
  • Date: 2010-09-28 09:30:05 UTC
  • Revision ID: isagalaev@yandex-team.ru-20100928093005-0bzqym08ktiuqqz2
Simplification of the main look in basic_parse.
Tests for incomplete and empty input buffers.

Show diffs side-by-side

added added

removed removed

Lines of Context:
56
56
class JSONError(Exception):
57
57
    pass
58
58
 
 
59
class IncompleteJSONError(JSONError):
 
60
    def __init__(self):
 
61
        super(IncompleteJSONError, self).__init__('Incomplete or empty JSON data')
 
62
 
59
63
def basic_parse(f, allow_comments=False, check_utf8=False, buf_size=64 * 1024):
60
64
    '''
61
65
    An iterator returning events from a JSON being parsed. This basic parser
95
99
    config = Config(allow_comments, check_utf8)
96
100
    handle = yajl.yajl_alloc(byref(callbacks), byref(config), None, None)
97
101
    try:
98
 
        result = None
99
 
        buffer = f.read(buf_size)
100
 
        if not buffer:
101
 
            raise JSONError("empty JSON description")
102
 
        while buffer or result == YAJL_INSUFFICIENT_DATA:
 
102
        while True:
 
103
            buffer = f.read(buf_size)
103
104
            if buffer:
104
105
                result = yajl.yajl_parse(handle, buffer, len(buffer))
105
106
            else:
106
107
                result = yajl.yajl_parse_complete(handle)
107
 
                if result == YAJL_INSUFFICIENT_DATA:
108
 
                    raise JSONError("empty JSON description")
109
108
            if result == YAJL_ERROR:
110
109
                error = yajl.yajl_get_error(handle, 1, buffer, len(buffer))
111
110
                raise JSONError(error)
112
 
            if events:
113
 
                for event in events:
114
 
                    yield event
115
 
                events = []
116
 
            if result == YAJL_CANCELLED:
 
111
            if not buffer and not events:
 
112
                if result == YAJL_INSUFFICIENT_DATA:
 
113
                    raise IncompleteJSONError()
117
114
                break
118
 
            buffer = f.read(buf_size)
 
115
 
 
116
            for event in events:
 
117
                yield event
 
118
            events = []
119
119
    finally:
120
120
        yajl.yajl_free(handle)
121
121