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))
138
111
accum_size = 4096
139
accum_value = <char *>PyMem_Malloc(accum_size)
112
accum_value = <char *>malloc(accum_size)
140
113
if accum_value == NULL:
141
114
raise MemoryError
143
116
for line in line_iter:
145
118
break # end of file
146
if not PyBytes_CheckExact(line):
119
if not PyString_CheckExact(line):
147
120
raise TypeError("%r is not a plain string" % line)
148
c_line = PyBytes_AS_STRING(line)
149
c_len = PyBytes_GET_SIZE(line)
121
c_line = PyString_AS_STRING(line)
122
c_len = PyString_GET_SIZE(line)
151
124
break # end of file
152
125
if c_len == 1 and c_line[0] == c"\n":
153
126
break # end of stanza
154
127
if accum_len + c_len > accum_size:
155
128
accum_size = (accum_len + c_len)
156
new_accum_value = <char *>PyMem_Realloc(accum_value, accum_size)
129
new_accum_value = <char *>realloc(accum_value, accum_size)
157
130
if new_accum_value == NULL:
158
131
raise MemoryError
165
138
accum_len = accum_len + c_len-1
166
139
else: # new tag:value line
167
140
if tag is not None:
169
(tag, PyUnicode_DecodeUTF8(accum_value, accum_len-1,
142
(tag, PyUnicode_DecodeUTF8(accum_value, accum_len-1,
171
tag = _split_first_line_utf8(c_line, c_len, accum_value,
144
tag = _split_first_line_utf8(c_line, c_len, accum_value,
173
146
if not _valid_tag(tag):
174
147
raise ValueError("invalid rio tag %r" % (tag,))
175
148
if tag is not None: # add last tag-value
177
150
(tag, PyUnicode_DecodeUTF8(accum_value, accum_len-1, "strict")))
178
151
return Stanza.from_pairs(pairs)
179
152
else: # didn't see any content
182
PyMem_Free(accum_value)
185
158
def _read_stanza_unicode(unicode_iter):
208
181
break # end of stanza
209
182
if accum_len + c_len > accum_size:
210
183
accum_size = accum_len + c_len
211
new_accum_value = <Py_UNICODE *>PyMem_Realloc(accum_value,
184
new_accum_value = <Py_UNICODE *>realloc(accum_value,
212
185
accum_size*sizeof(Py_UNICODE))
213
186
if new_accum_value == NULL:
214
187
raise MemoryError
222
195
accum_len = accum_len + (c_len-1)
223
196
else: # new tag:value line
224
197
if tag is not None:
226
199
(tag, PyUnicode_FromUnicode(accum_value, accum_len-1)))
227
tag = _split_first_line_unicode(c_line, c_len, accum_value,
200
tag = _split_first_line_unicode(c_line, c_len, accum_value,
229
202
if not _valid_tag(tag):
230
203
raise ValueError("invalid rio tag %r" % (tag,))