Debian Codegraph @

Modules/_testcapi/bytes.c

raw
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 static PyMethodDef test_methods[] = {
41 {"bytes_resize", bytes_resize, METH_VARARGS},
42 {NULL},
43 };
44
45 int
46 _PyTestCapi_Init_Bytes(PyObject *m)
47 {
48 if (PyModule_AddFunctions(m, test_methods) < 0) {
49 return -1;
50 }
51
52 return 0;
53 }