17
17
"""Pyrex implementation of _read_stanza_*."""
19
from __future__ import absolute_import
20
22
cdef extern from "python-compat.h":
23
cdef extern from "stdlib.h":
25
void *realloc(void *, int)
25
from cpython.bytes cimport (
27
PyBytes_FromStringAndSize,
31
from cpython.unicode cimport (
34
# Deprecated after PEP 393 changes
36
PyUnicode_FromUnicode,
39
from cpython.list cimport (
42
from cpython.mem cimport (
47
from cpython.version cimport (
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)
47
cdef extern from "string.h":
48
void *memcpy(void *, void *, int)
50
from bzrlib.rio import Stanza
56
# GZ 2017-09-11: Not sure why cython unicode module lacks this?
57
object PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size)
59
# Python 3.3 or later unicode handling
60
char* PyUnicode_AsUTF8AndSize(object unicode, Py_ssize_t *size)
62
from libc.string cimport (
66
from .rio import Stanza
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'))
61
78
cdef Py_ssize_t c_len
63
if not PyString_CheckExact(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):
84
c_tag = PyUnicode_AsUTF8AndSize(tag, &c_len)
86
if not PyBytes_CheckExact(tag):
88
c_tag = PyBytes_AS_STRING(tag)
89
c_len = PyBytes_GET_SIZE(tag)
69
92
for i from 0 <= i < c_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)
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):
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
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
116
144
for line in line_iter:
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)
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
138
166
accum_len = accum_len + c_len-1
139
167
else: # new tag:value line
140
168
if tag is not None:
142
(tag, PyUnicode_DecodeUTF8(accum_value, accum_len-1,
170
(tag, PyUnicode_DecodeUTF8(accum_value, accum_len-1,
144
tag = _split_first_line_utf8(c_line, c_len, accum_value,
172
tag = _split_first_line_utf8(c_line, c_len, accum_value,
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
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
183
PyMem_Free(accum_value)
158
186
def _read_stanza_unicode(unicode_iter):
159
187
cdef Py_UNICODE *c_line
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
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
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:
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,
202
231
if not _valid_tag(tag):
203
232
raise ValueError("invalid rio tag %r" % (tag,))