1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
#include "parts.h"
#include "util.h"
/* Test PyBytes_Check() */
static PyObject *
bytes_check(PyObject *Py_UNUSED(module), PyObject *obj)
{
NULLABLE(obj);
return PyLong_FromLong(PyBytes_Check(obj));
}
/* Test PyBytes_CheckExact() */
static PyObject *
bytes_checkexact(PyObject *Py_UNUSED(module), PyObject *obj)
{
NULLABLE(obj);
return PyLong_FromLong(PyBytes_CheckExact(obj));
}
/* Test PyBytes_FromStringAndSize() */
static PyObject *
bytes_fromstringandsize(PyObject *Py_UNUSED(module), PyObject *args)
{
const char *s;
Py_ssize_t bsize;
Py_ssize_t size = -100;
if (!PyArg_ParseTuple(args, "z#|n", &s, &bsize, &size)) {
return NULL;
}
if (size == -100) {
size = bsize;
}
return PyBytes_FromStringAndSize(s, size);
}
/* Test PyBytes_FromString() */
static PyObject *
bytes_fromstring(PyObject *Py_UNUSED(module), PyObject *arg)
{
const char *s;
Py_ssize_t size;
if (!PyArg_Parse(arg, "z#", &s, &size)) {
return NULL;
}
return PyBytes_FromString(s);
}
/* Test PyBytes_FromObject() */
static PyObject *
bytes_fromobject(PyObject *Py_UNUSED(module), PyObject *arg)
{
NULLABLE(arg);
return PyBytes_FromObject(arg);
}
/* Test PyBytes_Size() */
static PyObject *
bytes_size(PyObject *Py_UNUSED(module), PyObject *arg)
{
NULLABLE(arg);
RETURN_SIZE(PyBytes_Size(arg));
}
/* Test PyUnicode_AsString() */
static PyObject *
bytes_asstring(PyObject *Py_UNUSED(module), PyObject *args)
{
PyObject *obj;
Py_ssize_t buflen;
const char *s;
if (!PyArg_ParseTuple(args, "On", &obj, &buflen))
return NULL;
NULLABLE(obj);
s = PyBytes_AsString(obj);
if (s == NULL)
return NULL;
return PyBytes_FromStringAndSize(s, buflen);
}
/* Test PyBytes_AsStringAndSize() */
static PyObject *
bytes_asstringandsize(PyObject *Py_UNUSED(module), PyObject *args)
{
PyObject *obj;
Py_ssize_t buflen;
char *s = UNINITIALIZED_PTR;
Py_ssize_t size = UNINITIALIZED_SIZE;
if (!PyArg_ParseTuple(args, "On", &obj, &buflen))
return NULL;
NULLABLE(obj);
if (PyBytes_AsStringAndSize(obj, &s, &size) < 0) {
return NULL;
}
if (s == NULL) {
return Py_BuildValue("(On)", Py_None, size);
}
else {
return Py_BuildValue("(y#n)", s, buflen, size);
}
}
static PyObject *
bytes_asstringandsize_null(PyObject *Py_UNUSED(module), PyObject *args)
{
PyObject *obj;
Py_ssize_t buflen;
char *s = UNINITIALIZED_PTR;
if (!PyArg_ParseTuple(args, "On", &obj, &buflen))
return NULL;
NULLABLE(obj);
if (PyBytes_AsStringAndSize(obj, &s, NULL) < 0) {
return NULL;
}
if (s == NULL) {
Py_RETURN_NONE;
}
else {
return PyBytes_FromStringAndSize(s, buflen);
}
}
/* Test PyBytes_Repr() */
static PyObject *
bytes_repr(PyObject *Py_UNUSED(module), PyObject *args)
{
PyObject *obj;
int smartquotes;
if (!PyArg_ParseTuple(args, "Oi", &obj, &smartquotes))
return NULL;
NULLABLE(obj);
return PyBytes_Repr(obj, smartquotes);
}
/* Test PyBytes_Concat() */
static PyObject *
bytes_concat(PyObject *Py_UNUSED(module), PyObject *args)
{
PyObject *left, *right;
int new = 0;
if (!PyArg_ParseTuple(args, "OO|p", &left, &right, &new))
return NULL;
NULLABLE(left);
NULLABLE(right);
if (new) {
assert(left != NULL);
assert(PyBytes_CheckExact(left));
left = PyBytes_FromStringAndSize(PyBytes_AsString(left),
PyBytes_Size(left));
if (left == NULL) {
return NULL;
}
}
else {
Py_XINCREF(left);
}
PyBytes_Concat(&left, right);
if (left == NULL && !PyErr_Occurred()) {
Py_RETURN_NONE;
}
return left;
}
/* Test PyBytes_ConcatAndDel() */
static PyObject *
bytes_concatanddel(PyObject *Py_UNUSED(module), PyObject *args)
{
PyObject *left, *right;
int new = 0;
if (!PyArg_ParseTuple(args, "OO|p", &left, &right, &new))
return NULL;
NULLABLE(left);
NULLABLE(right);
if (new) {
assert(left != NULL);
assert(PyBytes_CheckExact(left));
left = PyBytes_FromStringAndSize(PyBytes_AsString(left),
PyBytes_Size(left));
if (left == NULL) {
return NULL;
}
}
else {
Py_XINCREF(left);
}
Py_XINCREF(right);
PyBytes_ConcatAndDel(&left, right);
if (left == NULL && !PyErr_Occurred()) {
Py_RETURN_NONE;
}
return left;
}
/* Test PyBytes_DecodeEscape() */
static PyObject *
bytes_decodeescape(PyObject *Py_UNUSED(module), PyObject *args)
{
const char *s;
Py_ssize_t bsize;
Py_ssize_t size = -100;
const char *errors = NULL;
if (!PyArg_ParseTuple(args, "z#|zn", &s, &bsize, &errors, &size))
return NULL;
if (size == -100) {
size = bsize;
}
return PyBytes_DecodeEscape(s, size, errors, 0, NULL);
}
static PyMethodDef test_methods[] = {
{"bytes_check", bytes_check, METH_O},
{"bytes_checkexact", bytes_checkexact, METH_O},
{"bytes_fromstringandsize", bytes_fromstringandsize, METH_VARARGS},
{"bytes_fromstring", bytes_fromstring, METH_O},
{"bytes_fromobject", bytes_fromobject, METH_O},
{"bytes_size", bytes_size, METH_O},
{"bytes_asstring", bytes_asstring, METH_VARARGS},
{"bytes_asstringandsize", bytes_asstringandsize, METH_VARARGS},
{"bytes_asstringandsize_null", bytes_asstringandsize_null, METH_VARARGS},
{"bytes_repr", bytes_repr, METH_VARARGS},
{"bytes_concat", bytes_concat, METH_VARARGS},
{"bytes_concatanddel", bytes_concatanddel, METH_VARARGS},
{"bytes_decodeescape", bytes_decodeescape, METH_VARARGS},
{NULL},
};
int
_PyTestCapi_Init_Bytes(PyObject *m)
{
if (PyModule_AddFunctions(m, test_methods) < 0) {
return -1;
}
return 0;
}
|