17
17
"""Pyrex implementation of _read_stanza_*."""
19
from __future__ import absolute_import
22
20
cdef extern from "python-compat.h":
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 (
23
cdef extern from "stdlib.h":
25
void *realloc(void *, int)
51
28
cdef extern from "Python.h":
29
ctypedef int Py_ssize_t # Required for older pyrex versions
52
30
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)
53
38
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
54
43
int Py_UNICODE_ISLINEBREAK(Py_UNICODE)
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
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
69
52
cdef int _valid_tag_char(char c): # cannot_raise
70
return (c == c'_' or c == c'-' or
53
return (c == c'_' or c == c'-' or
71
54
(c >= c'a' and c <= c'z') or
72
55
(c >= c'A' and c <= c'Z') or
73
56
(c >= c'0' and c <= c'9'))
78
61
cdef Py_ssize_t c_len
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)
63
if not PyString_CheckExact(tag):
65
c_tag = PyString_AS_STRING(tag)
66
c_len = PyString_GET_SIZE(tag)
92
69
for i from 0 <= i < c_len:
104
81
raise ValueError("invalid tag in line %r" % line)
105
82
memcpy(value, line+i+2, len-i-2)
106
83
value_len[0] = len-i-2
107
if PY_MAJOR_VERSION >= 3:
108
return PyUnicode_FromStringAndSize(line, i)
109
return PyBytes_FromStringAndSize(line, i)
84
return PyString_FromStringAndSize(line, i)
110
85
raise ValueError('tag/value separator not found in line %r' % line)
113
cdef object _split_first_line_unicode(Py_UNICODE *line, int len,
88
cdef object _split_first_line_unicode(Py_UNICODE *line, int len,
114
89
Py_UNICODE *value, Py_ssize_t *value_len):
116
91
for i from 0 <= i < len:
120
95
PyUnicode_FromUnicode(line, len))
121
96
memcpy(value, &line[i+2], (len-i-2) * sizeof(Py_UNICODE))
122
97
value_len[0] = len-i-2
123
if PY_MAJOR_VERSION >= 3:
124
return PyUnicode_FromUnicode(line, i)
125
98
return PyUnicode_EncodeASCII(line, i, "strict")
126
99
raise ValueError("tag/value separator not found in line %r" %
127
100
PyUnicode_FromUnicode(line, len))
130
103
def _read_stanza_utf8(line_iter):
131
104
cdef char *c_line
132
105
cdef Py_ssize_t c_len
133
cdef char *accum_value
134
cdef char *new_accum_value
106
cdef char *accum_value, *new_accum_value
135
107
cdef Py_ssize_t accum_len, accum_size
139
111
accum_size = 4096
140
accum_value = <char *>PyMem_Malloc(accum_size)
112
accum_value = <char *>malloc(accum_size)
141
113
if accum_value == NULL:
142
114
raise MemoryError
144
116
for line in line_iter:
146
118
break # end of file
147
if not PyBytes_CheckExact(line):
119
if not PyString_CheckExact(line):
148
120
raise TypeError("%r is not a plain string" % line)
149
c_line = PyBytes_AS_STRING(line)
150
c_len = PyBytes_GET_SIZE(line)
121
c_line = PyString_AS_STRING(line)
122
c_len = PyString_GET_SIZE(line)
152
124
break # end of file
153
125
if c_len == 1 and c_line[0] == c"\n":
154
126
break # end of stanza
155
127
if accum_len + c_len > accum_size:
156
128
accum_size = (accum_len + c_len)
157
new_accum_value = <char *>PyMem_Realloc(accum_value, accum_size)
129
new_accum_value = <char *>realloc(accum_value, accum_size)
158
130
if new_accum_value == NULL:
159
131
raise MemoryError
166
138
accum_len = accum_len + c_len-1
167
139
else: # new tag:value line
168
140
if tag is not None:
170
(tag, PyUnicode_DecodeUTF8(accum_value, accum_len-1,
142
(tag, PyUnicode_DecodeUTF8(accum_value, accum_len-1,
172
tag = _split_first_line_utf8(c_line, c_len, accum_value,
144
tag = _split_first_line_utf8(c_line, c_len, accum_value,
174
146
if not _valid_tag(tag):
175
147
raise ValueError("invalid rio tag %r" % (tag,))
176
148
if tag is not None: # add last tag-value
178
150
(tag, PyUnicode_DecodeUTF8(accum_value, accum_len-1, "strict")))
179
151
return Stanza.from_pairs(pairs)
180
152
else: # didn't see any content
183
PyMem_Free(accum_value)
186
158
def _read_stanza_unicode(unicode_iter):
187
159
cdef Py_UNICODE *c_line
189
cdef Py_UNICODE *accum_value
190
cdef Py_UNICODE *new_accum_value
161
cdef Py_UNICODE *accum_value, *new_accum_value
191
162
cdef Py_ssize_t accum_len, accum_size
195
166
accum_size = 4096
196
accum_value = <Py_UNICODE *>PyMem_Malloc(accum_size*sizeof(Py_UNICODE))
167
accum_value = <Py_UNICODE *>malloc(accum_size*sizeof(Py_UNICODE))
197
168
if accum_value == NULL:
198
169
raise MemoryError
210
181
break # end of stanza
211
182
if accum_len + c_len > accum_size:
212
183
accum_size = accum_len + c_len
213
new_accum_value = <Py_UNICODE *>PyMem_Realloc(accum_value,
184
new_accum_value = <Py_UNICODE *>realloc(accum_value,
214
185
accum_size*sizeof(Py_UNICODE))
215
186
if new_accum_value == NULL:
216
187
raise MemoryError
224
195
accum_len = accum_len + (c_len-1)
225
196
else: # new tag:value line
226
197
if tag is not None:
228
199
(tag, PyUnicode_FromUnicode(accum_value, accum_len-1)))
229
tag = _split_first_line_unicode(c_line, c_len, accum_value,
200
tag = _split_first_line_unicode(c_line, c_len, accum_value,
231
202
if not _valid_tag(tag):
232
203
raise ValueError("invalid rio tag %r" % (tag,))