| 1 |
#include "parts.h" |
| 2 |
#include "util.h" |
| 3 |
|
| 4 |
|
| 5 |
/* Test _PyBytes_Resize() */ |
| 6 |
static PyObject * |
| 7 |
bytes_resize(PyObject *Py_UNUSED(module), PyObject *args) |
| 8 |
{ |
| 9 |
PyObject *obj; |
| 10 |
Py_ssize_t newsize; |
| 11 |
int new; |
| 12 |
|
| 13 |
if (!PyArg_ParseTuple(args, "Onp", &obj, &newsize, &new)) |
| 14 |
return NULL; |
| 15 |
|
| 16 |
NULLABLE(obj); |
| 17 |
if (new) { |
| 18 |
assert(obj != NULL); |
| 19 |
assert(PyBytes_CheckExact(obj)); |
| 20 |
PyObject *newobj = PyBytes_FromStringAndSize(NULL, PyBytes_Size(obj)); |
| 21 |
if (newobj == NULL) { |
| 22 |
return NULL; |
| 23 |
} |
| 24 |
memcpy(PyBytes_AsString(newobj), PyBytes_AsString(obj), PyBytes_Size(obj)); |
| 25 |
obj = newobj; |
| 26 |
} |
| 27 |
else { |
| 28 |
Py_XINCREF(obj); |
| 29 |
} |
| 30 |
if (_PyBytes_Resize(&obj, newsize) < 0) { |
| 31 |
assert(obj == NULL); |
| 32 |
} |
| 33 |
else { |
| 34 |
assert(obj != NULL); |
| 35 |
} |
| 36 |
return obj; |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
/* Test PyBytes_Join() */ |
| 41 |
static PyObject * |
| 42 |
bytes_join(PyObject *Py_UNUSED(module), PyObject *args) |
| 43 |
{ |
| 44 |
PyObject *sep, *iterable; |
| 45 |
if (!PyArg_ParseTuple(args, "OO", &sep, &iterable)) { |
| 46 |
return NULL; |
| 47 |
} |
| 48 |
NULLABLE(sep); |
| 49 |
NULLABLE(iterable); |
| 50 |
return PyBytes_Join(sep, iterable); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
static PyMethodDef test_methods[] = { |
| 55 |
{"bytes_resize", bytes_resize, METH_VARARGS}, |
| 56 |
{"bytes_join", bytes_join, METH_VARARGS}, |
| 57 |
{NULL}, |
| 58 |
}; |
| 59 |
|
| 60 |
int |
| 61 |
_PyTestCapi_Init_Bytes(PyObject *m) |
| 62 |
{ |
| 63 |
if (PyModule_AddFunctions(m, test_methods) < 0) { |
| 64 |
return -1; |
| 65 |
} |
| 66 |
|
| 67 |
return 0; |
| 68 |
} |