/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/_rio_pyx.pyx

  • Committer: Jelmer Vernooij
  • Date: 2017-09-06 04:15:55 UTC
  • mfrom: (6754.8.21 lock-context-2)
  • Revision ID: jelmer@jelmer.uk-20170906041555-jtr5qxli38167gc6
Merge lp:~jelmer/brz/lock-context-2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Pyrex implementation of _read_stanza_*."""
18
18
 
19
 
#python2.4 support
 
19
from __future__ import absolute_import
 
20
 
 
21
 
20
22
cdef extern from "python-compat.h":
21
23
    pass
22
24
 
26
28
    void free(void *)
27
29
 
28
30
cdef extern from "Python.h":
29
 
    ctypedef int Py_ssize_t # Required for older pyrex versions
30
31
    ctypedef int Py_UNICODE
31
32
    char *PyString_AS_STRING(object s)
32
33
    Py_ssize_t PyString_GET_SIZE(object t) except -1
39
40
    Py_UNICODE *PyUnicode_AS_UNICODE(object)
40
41
    Py_UNICODE *PyUnicode_AsUnicode(object)
41
42
    Py_ssize_t PyUnicode_GET_SIZE(object) except -1
42
 
    int PyList_Append(object, object) except -1    
 
43
    int PyList_Append(object, object) except -1
43
44
    int Py_UNICODE_ISLINEBREAK(Py_UNICODE)
44
45
    object PyUnicode_FromUnicode(Py_UNICODE *, int)
45
46
    void *Py_UNICODE_COPY(Py_UNICODE *, Py_UNICODE *, int)
47
48
cdef extern from "string.h":
48
49
    void *memcpy(void *, void *, int)
49
50
 
50
 
from bzrlib.rio import Stanza
 
51
from .rio import Stanza
51
52
 
52
53
cdef int _valid_tag_char(char c): # cannot_raise
53
 
    return (c == c'_' or c == c'-' or 
 
54
    return (c == c'_' or c == c'-' or
54
55
            (c >= c'a' and c <= c'z') or
55
56
            (c >= c'A' and c <= c'Z') or
56
57
            (c >= c'0' and c <= c'9'))
72
73
    return True
73
74
 
74
75
 
75
 
cdef object _split_first_line_utf8(char *line, int len, 
 
76
cdef object _split_first_line_utf8(char *line, int len,
76
77
                                   char *value, Py_ssize_t *value_len):
77
78
    cdef int i
78
79
    for i from 0 <= i < len:
85
86
    raise ValueError('tag/value separator not found in line %r' % line)
86
87
 
87
88
 
88
 
cdef object _split_first_line_unicode(Py_UNICODE *line, int len, 
 
89
cdef object _split_first_line_unicode(Py_UNICODE *line, int len,
89
90
                                      Py_UNICODE *value, Py_ssize_t *value_len):
90
91
    cdef int i
91
92
    for i from 0 <= i < len:
138
139
                accum_len = accum_len + c_len-1
139
140
            else: # new tag:value line
140
141
                if tag is not None:
141
 
                    PyList_Append(pairs, 
142
 
                        (tag, PyUnicode_DecodeUTF8(accum_value, accum_len-1, 
 
142
                    PyList_Append(pairs,
 
143
                        (tag, PyUnicode_DecodeUTF8(accum_value, accum_len-1,
143
144
                                                   "strict")))
144
 
                tag = _split_first_line_utf8(c_line, c_len, accum_value, 
 
145
                tag = _split_first_line_utf8(c_line, c_len, accum_value,
145
146
                                             &accum_len)
146
147
                if not _valid_tag(tag):
147
148
                    raise ValueError("invalid rio tag %r" % (tag,))
148
149
        if tag is not None: # add last tag-value
149
 
            PyList_Append(pairs, 
 
150
            PyList_Append(pairs,
150
151
                (tag, PyUnicode_DecodeUTF8(accum_value, accum_len-1, "strict")))
151
152
            return Stanza.from_pairs(pairs)
152
153
        else:     # didn't see any content
181
182
                break       # end of stanza
182
183
            if accum_len + c_len > accum_size:
183
184
                accum_size = accum_len + c_len
184
 
                new_accum_value = <Py_UNICODE *>realloc(accum_value, 
 
185
                new_accum_value = <Py_UNICODE *>realloc(accum_value,
185
186
                    accum_size*sizeof(Py_UNICODE))
186
187
                if new_accum_value == NULL:
187
188
                    raise MemoryError
195
196
                accum_len = accum_len + (c_len-1)
196
197
            else: # new tag:value line
197
198
                if tag is not None:
198
 
                    PyList_Append(pairs, 
 
199
                    PyList_Append(pairs,
199
200
                        (tag, PyUnicode_FromUnicode(accum_value, accum_len-1)))
200
 
                tag = _split_first_line_unicode(c_line, c_len, accum_value, 
 
201
                tag = _split_first_line_unicode(c_line, c_len, accum_value,
201
202
                                                &accum_len)
202
203
                if not _valid_tag(tag):
203
204
                    raise ValueError("invalid rio tag %r" % (tag,))