/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: 2020-01-19 15:14:16 UTC
  • mto: This revision was merged to the branch mainline in revision 7455.
  • Revision ID: jelmer@jelmer.uk-20200119151416-f2x9y9rtvwxndr2l
Don't show submodules that are not checked out as deltas.

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
 
23
 
cdef extern from "stdlib.h":
24
 
    void *malloc(int)
25
 
    void *realloc(void *, int)
26
 
    void free(void *)
 
25
from cpython.bytes cimport (
 
26
    PyBytes_CheckExact,
 
27
    PyBytes_FromStringAndSize,
 
28
    PyBytes_AS_STRING,
 
29
    PyBytes_GET_SIZE,
 
30
    )
 
31
from cpython.unicode cimport (
 
32
    PyUnicode_CheckExact,
 
33
    PyUnicode_DecodeUTF8,
 
34
    # Deprecated after PEP 393 changes
 
35
    PyUnicode_AS_UNICODE,
 
36
    PyUnicode_FromUnicode,
 
37
    PyUnicode_GET_SIZE,
 
38
    )
 
39
from cpython.list cimport (
 
40
    PyList_Append,
 
41
    )
 
42
from cpython.mem cimport (
 
43
    PyMem_Free,
 
44
    PyMem_Malloc,
 
45
    PyMem_Realloc,
 
46
    )
 
47
from cpython.version cimport (
 
48
    PY_MAJOR_VERSION,
 
49
    )
27
50
 
28
51
cdef extern from "Python.h":
29
 
    ctypedef int Py_ssize_t # Required for older pyrex versions
30
52
    ctypedef int Py_UNICODE
31
 
    char *PyString_AS_STRING(object s)
32
 
    Py_ssize_t PyString_GET_SIZE(object t) except -1
33
 
    object PyUnicode_DecodeUTF8(char *string, Py_ssize_t length, char *errors)
34
 
    object PyString_FromStringAndSize(char *s, Py_ssize_t len)
35
 
    int PyString_CheckExact(object)
36
 
    int PyUnicode_CheckExact(object)
37
 
    object PyUnicode_Join(object, object)
38
53
    object PyUnicode_EncodeASCII(Py_UNICODE *, int, char *)
39
 
    Py_UNICODE *PyUnicode_AS_UNICODE(object)
40
 
    Py_UNICODE *PyUnicode_AsUnicode(object)
41
 
    Py_ssize_t PyUnicode_GET_SIZE(object) except -1
42
 
    int PyList_Append(object, object) except -1    
43
54
    int Py_UNICODE_ISLINEBREAK(Py_UNICODE)
44
 
    object PyUnicode_FromUnicode(Py_UNICODE *, int)
45
 
    void *Py_UNICODE_COPY(Py_UNICODE *, Py_UNICODE *, int)
46
 
 
47
 
cdef extern from "string.h":
48
 
    void *memcpy(void *, void *, int)
49
 
 
50
 
from bzrlib.rio import Stanza
 
55
 
 
56
    # GZ 2017-09-11: Not sure why cython unicode module lacks this?
 
57
    object PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size)
 
58
 
 
59
    # Python 3.3 or later unicode handling
 
60
    char* PyUnicode_AsUTF8AndSize(object unicode, Py_ssize_t *size)
 
61
 
 
62
from libc.string cimport (
 
63
    memcpy,
 
64
    )
 
65
 
 
66
from .rio import Stanza
 
67
 
51
68
 
52
69
cdef int _valid_tag_char(char c): # cannot_raise
53
 
    return (c == c'_' or c == c'-' or 
 
70
    return (c == c'_' or c == c'-' or
54
71
            (c >= c'a' and c <= c'z') or
55
72
            (c >= c'A' and c <= c'Z') or
56
73
            (c >= c'0' and c <= c'9'))
60
77
    cdef char *c_tag
61
78
    cdef Py_ssize_t c_len
62
79
    cdef int i
63
 
    if not PyString_CheckExact(tag):
64
 
        raise TypeError(tag)
65
 
    c_tag = PyString_AS_STRING(tag)
66
 
    c_len = PyString_GET_SIZE(tag)
 
80
    # GZ 2017-09-11: Encapsulate native string as ascii tag somewhere neater
 
81
    if PY_MAJOR_VERSION >= 3:
 
82
        if not PyUnicode_CheckExact(tag):
 
83
            raise TypeError(tag)
 
84
        c_tag = PyUnicode_AsUTF8AndSize(tag, &c_len)
 
85
    else:
 
86
        if not PyBytes_CheckExact(tag):
 
87
            raise TypeError(tag)
 
88
        c_tag = PyBytes_AS_STRING(tag)
 
89
        c_len = PyBytes_GET_SIZE(tag)
67
90
    if c_len < 1:
68
91
        return False
69
92
    for i from 0 <= i < c_len:
72
95
    return True
73
96
 
74
97
 
75
 
cdef object _split_first_line_utf8(char *line, int len, 
 
98
cdef object _split_first_line_utf8(char *line, int len,
76
99
                                   char *value, Py_ssize_t *value_len):
77
100
    cdef int i
78
101
    for i from 0 <= i < len:
81
104
                raise ValueError("invalid tag in line %r" % line)
82
105
            memcpy(value, line+i+2, len-i-2)
83
106
            value_len[0] = len-i-2
84
 
            return PyString_FromStringAndSize(line, i)
 
107
            if PY_MAJOR_VERSION >= 3:
 
108
                return PyUnicode_FromStringAndSize(line, i)
 
109
            return PyBytes_FromStringAndSize(line, i)
85
110
    raise ValueError('tag/value separator not found in line %r' % line)
86
111
 
87
112
 
88
 
cdef object _split_first_line_unicode(Py_UNICODE *line, int len, 
 
113
cdef object _split_first_line_unicode(Py_UNICODE *line, int len,
89
114
                                      Py_UNICODE *value, Py_ssize_t *value_len):
90
115
    cdef int i
91
116
    for i from 0 <= i < len:
95
120
                                 PyUnicode_FromUnicode(line, len))
96
121
            memcpy(value, &line[i+2], (len-i-2) * sizeof(Py_UNICODE))
97
122
            value_len[0] = len-i-2
 
123
            if PY_MAJOR_VERSION >= 3:
 
124
                return PyUnicode_FromUnicode(line, i)
98
125
            return PyUnicode_EncodeASCII(line, i, "strict")
99
126
    raise ValueError("tag/value separator not found in line %r" %
100
127
                     PyUnicode_FromUnicode(line, len))
103
130
def _read_stanza_utf8(line_iter):
104
131
    cdef char *c_line
105
132
    cdef Py_ssize_t c_len
106
 
    cdef char *accum_value, *new_accum_value
 
133
    cdef char *accum_value
 
134
    cdef char *new_accum_value
107
135
    cdef Py_ssize_t accum_len, accum_size
108
136
    pairs = []
109
137
    tag = None
110
138
    accum_len = 0
111
139
    accum_size = 4096
112
 
    accum_value = <char *>malloc(accum_size)
 
140
    accum_value = <char *>PyMem_Malloc(accum_size)
113
141
    if accum_value == NULL:
114
142
        raise MemoryError
115
143
    try:
116
144
        for line in line_iter:
117
145
            if line is None:
118
146
                break # end of file
119
 
            if not PyString_CheckExact(line):
 
147
            if not PyBytes_CheckExact(line):
120
148
                raise TypeError("%r is not a plain string" % line)
121
 
            c_line = PyString_AS_STRING(line)
122
 
            c_len = PyString_GET_SIZE(line)
 
149
            c_line = PyBytes_AS_STRING(line)
 
150
            c_len = PyBytes_GET_SIZE(line)
123
151
            if c_len < 1:
124
152
                break       # end of file
125
153
            if c_len == 1 and c_line[0] == c"\n":
126
154
                break       # end of stanza
127
155
            if accum_len + c_len > accum_size:
128
156
                accum_size = (accum_len + c_len)
129
 
                new_accum_value = <char *>realloc(accum_value, accum_size)
 
157
                new_accum_value = <char *>PyMem_Realloc(accum_value, accum_size)
130
158
                if new_accum_value == NULL:
131
159
                    raise MemoryError
132
160
                else:
138
166
                accum_len = accum_len + c_len-1
139
167
            else: # new tag:value line
140
168
                if tag is not None:
141
 
                    PyList_Append(pairs, 
142
 
                        (tag, PyUnicode_DecodeUTF8(accum_value, accum_len-1, 
 
169
                    PyList_Append(pairs,
 
170
                        (tag, PyUnicode_DecodeUTF8(accum_value, accum_len-1,
143
171
                                                   "strict")))
144
 
                tag = _split_first_line_utf8(c_line, c_len, accum_value, 
 
172
                tag = _split_first_line_utf8(c_line, c_len, accum_value,
145
173
                                             &accum_len)
146
174
                if not _valid_tag(tag):
147
175
                    raise ValueError("invalid rio tag %r" % (tag,))
148
176
        if tag is not None: # add last tag-value
149
 
            PyList_Append(pairs, 
 
177
            PyList_Append(pairs,
150
178
                (tag, PyUnicode_DecodeUTF8(accum_value, accum_len-1, "strict")))
151
179
            return Stanza.from_pairs(pairs)
152
180
        else:     # didn't see any content
153
181
            return None
154
182
    finally:
155
 
        free(accum_value)
 
183
        PyMem_Free(accum_value)
156
184
 
157
185
 
158
186
def _read_stanza_unicode(unicode_iter):
159
187
    cdef Py_UNICODE *c_line
160
188
    cdef int c_len
161
 
    cdef Py_UNICODE *accum_value, *new_accum_value
 
189
    cdef Py_UNICODE *accum_value
 
190
    cdef Py_UNICODE *new_accum_value
162
191
    cdef Py_ssize_t accum_len, accum_size
163
192
    pairs = []
164
193
    tag = None
165
194
    accum_len = 0
166
195
    accum_size = 4096
167
 
    accum_value = <Py_UNICODE *>malloc(accum_size*sizeof(Py_UNICODE))
 
196
    accum_value = <Py_UNICODE *>PyMem_Malloc(accum_size*sizeof(Py_UNICODE))
168
197
    if accum_value == NULL:
169
198
        raise MemoryError
170
199
    try:
181
210
                break       # end of stanza
182
211
            if accum_len + c_len > accum_size:
183
212
                accum_size = accum_len + c_len
184
 
                new_accum_value = <Py_UNICODE *>realloc(accum_value, 
 
213
                new_accum_value = <Py_UNICODE *>PyMem_Realloc(accum_value,
185
214
                    accum_size*sizeof(Py_UNICODE))
186
215
                if new_accum_value == NULL:
187
216
                    raise MemoryError
195
224
                accum_len = accum_len + (c_len-1)
196
225
            else: # new tag:value line
197
226
                if tag is not None:
198
 
                    PyList_Append(pairs, 
 
227
                    PyList_Append(pairs,
199
228
                        (tag, PyUnicode_FromUnicode(accum_value, accum_len-1)))
200
 
                tag = _split_first_line_unicode(c_line, c_len, accum_value, 
 
229
                tag = _split_first_line_unicode(c_line, c_len, accum_value,
201
230
                                                &accum_len)
202
231
                if not _valid_tag(tag):
203
232
                    raise ValueError("invalid rio tag %r" % (tag,))
208
237
        else:     # didn't see any content
209
238
            return None
210
239
    finally:
211
 
        free(accum_value)
 
240
        PyMem_Free(accum_value)