Source

pyyaml / ext / _yaml.pyx

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430

import yaml

def get_version_string():
    return yaml_get_version_string()

def get_version():
    cdef int major, minor, patch
    yaml_get_version(&major, &minor, &patch)
    return (major, minor, patch)

def test_scanner(data):
    cdef yaml_parser_t *parser
    cdef yaml_token_t *token
    cdef int done
    if PyString_CheckExact(data) == 0:
        raise TypeError("string input required")
    parser = yaml_parser_new()
    if parser == NULL:
        raise MemoryError
    yaml_parser_set_input_string(parser, PyString_AS_STRING(data), PyString_GET_SIZE(data))
    done = 0
    while done == 0:
        token = yaml_parser_get_token(parser)
        if token == NULL:
            raise MemoryError
        if token.type == YAML_STREAM_END_TOKEN:
            done = 1
        yaml_token_delete(token)
    yaml_parser_delete(parser)

def test_parser(data):
    cdef yaml_parser_t *parser
    cdef yaml_event_t *event
    cdef int done
    if PyString_CheckExact(data) == 0:
        raise TypeError("string input required")
    parser = yaml_parser_new()
    if parser == NULL:
        raise MemoryError
    yaml_parser_set_input_string(parser, PyString_AS_STRING(data), PyString_GET_SIZE(data))
    done = 0
    while done == 0:
        event = yaml_parser_get_event(parser)
        if event == NULL:
            raise MemoryError
        if event.type == YAML_STREAM_END_EVENT:
            done = 1
        yaml_event_delete(event)
    yaml_parser_delete(parser)

cdef class ScannerAndParser:

    cdef yaml_parser_t *parser
    cdef int eof
    cdef object stream
    cdef yaml_token_t *cached_token
    cdef yaml_event_t *cached_event
    cdef object cached_obj

    def __init__(self, stream):
        if hasattr(stream, 'read'):
            stream = stream.read()
        if PyUnicode_CheckExact(stream) != 0:
            stream = stream.encode('utf-8')
        if PyString_CheckExact(stream) == 0:
            raise TypeError("a string or stream input is required")
        self.parser = yaml_parser_new()
        if self.parser == NULL:
            raise MemoryError
        yaml_parser_set_input_string(self.parser, PyString_AS_STRING(stream), PyString_GET_SIZE(stream))
        self.eof = 0
        self.stream = stream
        self.cached_token = NULL
        self.cached_obj = None

    def __dealloc__(self):
        if self.parser != NULL:
            yaml_parser_delete(self.parser)
            self.parser = NULL

    cdef object _convert_token(self, yaml_token_t *token):
        if token == NULL:
            if self.parser.error == YAML_MEMORY_ERROR:
                raise MemoryError
            elif self.parser.error == YAML_READER_ERROR:
                raise yaml.reader.ReaderError("<input>",
                        self.parser.problem_offset,
                        self.parser.problem_value,
                        '?', self.parser.problem)
            elif self.parser.error == YAML_SCANNER_ERROR:
                if self.parser.context != NULL:
                    raise yaml.scanner.ScannerError(
                            self.parser.context,
                            yaml.Mark("<input>",
                                self.parser.context_mark.index,
                                self.parser.context_mark.line,
                                self.parser.context_mark.column,
                                None, None),
                            self.parser.problem,
                            yaml.Mark("<input>",
                                self.parser.problem_mark.index,
                                self.parser.problem_mark.line,
                                self.parser.problem_mark.column,
                                None, None))
                else:
                    raise yaml.scanner.ScannerError(None, None,
                            self.parser.problem,
                            yaml.Mark("<input>",
                                self.parser.problem_mark.index,
                                self.parser.problem_mark.line,
                                self.parser.problem_mark.column,
                                None, None))
            else:
                raise RuntimeError("neither error nor token produced")
        start_mark = yaml.Mark("<input>",
                token.start_mark.index,
                token.start_mark.line,
                token.start_mark.column,
                None, None)
        end_mark = yaml.Mark("<input>",
                token.end_mark.index,
                token.end_mark.line,
                token.end_mark.column,
                None, None)
        if token.type == YAML_STREAM_START_TOKEN:
            return yaml.StreamStartToken(start_mark, end_mark)
        elif token.type == YAML_STREAM_END_TOKEN:
            return yaml.StreamEndToken(start_mark, end_mark)
        elif token.type == YAML_VERSION_DIRECTIVE_TOKEN:
            return yaml.DirectiveToken('YAML',
                    (token.data.version_directive.major,
                        token.data.version_directive.minor),
                    start_mark, end_mark)
        elif token.type == YAML_TAG_DIRECTIVE_TOKEN:
            return yaml.DirectiveToken('TAG',
                    (token.data.tag_directive.handle,
                        token.data.tag_directive.prefix),
                    start_mark, end_mark)
        elif token.type == YAML_DOCUMENT_START_TOKEN:
            return yaml.DocumentStartToken(start_mark, end_mark)
        elif token.type == YAML_DOCUMENT_END_TOKEN:
            return yaml.DocumentEndToken(start_mark, end_mark)
        elif token.type == YAML_BLOCK_SEQUENCE_START_TOKEN:
            return yaml.BlockSequenceStartToken(start_mark, end_mark)
        elif token.type == YAML_BLOCK_MAPPING_START_TOKEN:
            return yaml.BlockMappingStartToken(start_mark, end_mark)
        elif token.type == YAML_BLOCK_END_TOKEN:
            return yaml.BlockEndToken(start_mark, end_mark)
        elif token.type == YAML_FLOW_SEQUENCE_START_TOKEN:
            return yaml.FlowSequenceStartToken(start_mark, end_mark)
        elif token.type == YAML_FLOW_SEQUENCE_END_TOKEN:
            return yaml.FlowSequenceEndToken(start_mark, end_mark)
        elif token.type == YAML_FLOW_MAPPING_START_TOKEN:
            return yaml.FlowMappingStartToken(start_mark, end_mark)
        elif token.type == YAML_FLOW_MAPPING_END_TOKEN:
            return yaml.FlowMappingEndToken(start_mark, end_mark)
        elif token.type == YAML_BLOCK_ENTRY_TOKEN:
            return yaml.BlockEntryToken(start_mark, end_mark)
        elif token.type == YAML_FLOW_ENTRY_TOKEN:
            return yaml.FlowEntryToken(start_mark, end_mark)
        elif token.type == YAML_KEY_TOKEN:
            return yaml.KeyToken(start_mark, end_mark)
        elif token.type == YAML_VALUE_TOKEN:
            return yaml.ValueToken(start_mark, end_mark)
        elif token.type == YAML_ALIAS_TOKEN:
            return yaml.AliasToken(token.data.alias.value,
                    start_mark, end_mark)
        elif token.type == YAML_ANCHOR_TOKEN:
            return yaml.AnchorToken(token.data.anchor.value,
                    start_mark, end_mark)
        elif token.type == YAML_TAG_TOKEN:
            handle = token.data.tag.handle
            if handle == '':
                handle = None
            return yaml.TagToken((handle, token.data.tag.suffix),
                    start_mark, end_mark)
        elif token.type == YAML_SCALAR_TOKEN:
            value = PyString_FromStringAndSize(token.data.scalar.value, token.data.scalar.length)
            return yaml.ScalarToken(unicode(value, 'utf-8'),
                    bool(token.data.scalar.style == YAML_PLAIN_SCALAR_STYLE),
                    start_mark, end_mark)
        else:
            raise RuntimeError("unknown token type")

    cdef object _convert_event(self, yaml_event_t *event):
        if event == NULL:
            if self.parser.error == YAML_MEMORY_ERROR:
                raise MemoryError
            elif self.parser.error == YAML_READER_ERROR:
                raise yaml.reader.ReaderError("<input>",
                        self.parser.problem_offset,
                        self.parser.problem_value,
                        '?', self.parser.problem)
            elif self.parser.error == YAML_SCANNER_ERROR:
                if self.parser.context != NULL:
                    raise yaml.scanner.ScannerError(
                            self.parser.context,
                            yaml.Mark("<input>",
                                self.parser.context_mark.index,
                                self.parser.context_mark.line,
                                self.parser.context_mark.column,
                                None, None),
                            self.parser.problem,
                            yaml.Mark("<input>",
                                self.parser.problem_mark.index,
                                self.parser.problem_mark.line,
                                self.parser.problem_mark.column,
                                None, None))
                else:
                    raise yaml.scanner.ScannerError(None, None,
                            self.parser.problem,
                            yaml.Mark("<input>",
                                self.parser.problem_mark.index,
                                self.parser.problem_mark.line,
                                self.parser.problem_mark.column,
                                None, None))
            elif self.parser.error == YAML_PARSER_ERROR:
                if self.parser.context != NULL:
                    raise yaml.parser.ParserError(
                            self.parser.context,
                            yaml.Mark("<input>",
                                self.parser.context_mark.index,
                                self.parser.context_mark.line,
                                self.parser.context_mark.column,
                                None, None),
                            self.parser.problem,
                            yaml.Mark("<input>",
                                self.parser.problem_mark.index,
                                self.parser.problem_mark.line,
                                self.parser.problem_mark.column,
                                None, None))
                else:
                    raise yaml.parser.ParserError(None, None,
                            self.parser.problem,
                            yaml.Mark("<input>",
                                self.parser.problem_mark.index,
                                self.parser.problem_mark.line,
                                self.parser.problem_mark.column,
                                None, None))
            else:
                raise RuntimeError("neither error nor event produced")
        start_mark = yaml.Mark("<input>",
                event.start_mark.index,
                event.start_mark.line,
                event.start_mark.column,
                None, None)
        end_mark = yaml.Mark("<input>",
                event.end_mark.index,
                event.end_mark.line,
                event.end_mark.column,
                None, None)
        if event.type == YAML_STREAM_START_EVENT:
            return yaml.StreamStartEvent(start_mark, end_mark)
        elif event.type == YAML_STREAM_END_EVENT:
            return yaml.StreamEndEvent(start_mark, end_mark)
        elif event.type == YAML_DOCUMENT_START_EVENT:
            return yaml.DocumentStartEvent(start_mark, end_mark,
                    (event.data.document_start.implicit == 0))
        elif event.type == YAML_DOCUMENT_END_EVENT:
            return yaml.DocumentEndEvent(start_mark, end_mark,
                    (event.data.document_end.implicit == 0))
        elif event.type == YAML_SCALAR_EVENT:
            if event.data.scalar.anchor == NULL:
                anchor = None
            else:
                anchor = event.data.scalar.anchor
            if event.data.scalar.tag == NULL:
                tag = None
            else:
                tag = event.data.scalar.tag
            implicit = (event.data.scalar.plain_implicit == 1, event.data.scalar.quoted_implicit == 1)
            flow_style = (event.data.sequence_start.style == YAML_FLOW_SEQUENCE_STYLE)
            value = PyString_FromStringAndSize(event.data.scalar.value, event.data.scalar.length)
            return yaml.ScalarEvent(anchor, tag, implicit, unicode(value, 'utf-8'),
                    start_mark, end_mark)
        elif event.type == YAML_ALIAS_EVENT:
            if event.data.alias.anchor == NULL:
                anchor = None
            else:
                anchor = event.data.alias.anchor
            return yaml.AliasEvent(anchor, start_mark, end_mark)
        elif event.type == YAML_SEQUENCE_START_EVENT:
            if event.data.sequence_start.anchor == NULL:
                anchor = None
            else:
                anchor = event.data.sequence_start.anchor
            if event.data.sequence_start.tag == NULL:
                tag = None
            else:
                tag = event.data.sequence_start.tag
            implicit = (event.data.sequence_start.implicit == 1)
            flow_style = (event.data.sequence_start.style == YAML_FLOW_SEQUENCE_STYLE)
            return yaml.SequenceStartEvent(anchor, tag, implicit,
                    start_mark, end_mark, flow_style)
        elif event.type == YAML_MAPPING_START_EVENT:
            if event.data.mapping_start.anchor == NULL:
                anchor = None
            else:
                anchor = event.data.mapping_start.anchor
            if event.data.mapping_start.tag == NULL:
                tag = None
            else:
                tag = event.data.mapping_start.tag
            implicit = (event.data.mapping_start.implicit == 1)
            flow_style = (event.data.mapping_start.style == YAML_FLOW_MAPPING_STYLE)
            return yaml.MappingStartEvent(anchor, tag, implicit,
                    start_mark, end_mark, flow_style)
        elif event.type == YAML_SEQUENCE_END_EVENT:
            return yaml.SequenceEndEvent(start_mark, end_mark)
        elif event.type == YAML_MAPPING_END_EVENT:
            return yaml.MappingEndEvent(start_mark, end_mark)
        else:
            raise RuntimeError("unknown event type")

    def get_token(self):
        cdef yaml_token_t *token
        if self.cached_token != NULL:
            yaml_token_delete(yaml_parser_get_token(self.parser))
            obj = self.cached_obj
            self.cached_token = NULL
            self.cached_obj = None
            return obj
        if self.eof != 0:
            return None
        token = yaml_parser_get_token(self.parser)
        obj = self._convert_token(token)
        if token.type == YAML_STREAM_END_TOKEN:
            self.eof = 1
        yaml_token_delete(token)
        return obj

    def peek_token(self):
        cdef yaml_token_t *token
        if self.cached_token != NULL:
            return self.cached_obj
        if self.eof != 0:
            return None
        token = yaml_parser_peek_token(self.parser)
        obj = self._convert_token(token)
        if token.type == YAML_STREAM_END_TOKEN:
            self.eof = 1
        self.cached_token = token
        self.cached_obj = obj
        return obj

    def check_token(self, *choices):
        cdef yaml_token_t *token
        if self.cached_token != NULL:
            obj = self.cached_obj
        elif self.eof != 0:
            return False
        else:
            token = yaml_parser_peek_token(self.parser)
            obj = self._convert_token(token)
            if token.type == YAML_STREAM_END_TOKEN:
                self.eof = 1
            self.cached_token = token
            self.cached_obj = obj
        if not choices:
            return True
        for choice in choices:
            if isinstance(obj, choice):
                return True
        return False

    def get_event(self):
        cdef yaml_event_t *event
        if self.cached_event != NULL:
            yaml_event_delete(yaml_parser_get_event(self.parser))
            obj = self.cached_obj
            self.cached_event = NULL
            self.cached_obj = None
            return obj
        if self.eof != 0:
            return None
        event = yaml_parser_get_event(self.parser)
        obj = self._convert_event(event)
        if event.type == YAML_STREAM_END_EVENT:
            self.eof = 1
        yaml_event_delete(event)
        return obj

    def peek_event(self):
        cdef yaml_event_t *event
        if self.cached_event != NULL:
            return self.cached_obj
        if self.eof != 0:
            return None
        event = yaml_parser_peek_event(self.parser)
        obj = self._convert_event(event)
        if event.type == YAML_STREAM_END_EVENT:
            self.eof = 1
        self.cached_event = event
        self.cached_obj = obj
        return obj

    def check_event(self, *choices):
        cdef yaml_event_t *event
        if self.cached_event != NULL:
            obj = self.cached_obj
        elif self.eof != 0:
            return False
        else:
            event = yaml_parser_peek_event(self.parser)
            obj = self._convert_event(event)
            if event.type == YAML_STREAM_END_EVENT:
                self.eof = 1
            self.cached_event = event
            self.cached_obj = obj
        if not choices:
            return True
        for choice in choices:
            if isinstance(obj, choice):
                return True
        return False

class Loader(ScannerAndParser,
        yaml.composer.Composer,
        yaml.constructor.Constructor,
        yaml.resolver.Resolver):

    def __init__(self, stream):
        ScannerAndParser.__init__(self, stream)
        yaml.composer.Composer.__init__(self)
        yaml.constructor.Constructor.__init__(self)
        yaml.resolver.Resolver.__init__(self)

yaml.ExtLoader = Loader