/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-05-24 00:42:36 UTC
  • mto: This revision was merged to the branch mainline in revision 7505.
  • Revision ID: jelmer@jelmer.uk-20200524004236-jdj6obo4k5lznqw2
Cleanup Windows functions.

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