1 #include "parts.h"
2 #include "util.h"
3
4
5 /* Test PyByteArray_Check() */
6 static PyObject *
bytearray_check(PyObject * Py_UNUSED (module),PyObject * obj)7 bytearray_check(PyObject *Py_UNUSED(module), PyObject *obj)
8 {
9 NULLABLE(obj);
10 return PyLong_FromLong(PyByteArray_Check(obj));
11 }
12
13 /* Test PyByteArray_CheckExact() */
14 static PyObject *
bytearray_checkexact(PyObject * Py_UNUSED (module),PyObject * obj)15 bytearray_checkexact(PyObject *Py_UNUSED(module), PyObject *obj)
16 {
17 NULLABLE(obj);
18 return PyLong_FromLong(PyByteArray_CheckExact(obj));
19 }
20
21 /* Test PyByteArray_FromStringAndSize() */
22 static PyObject *
bytearray_fromstringandsize(PyObject * Py_UNUSED (module),PyObject * args)23 bytearray_fromstringandsize(PyObject *Py_UNUSED(module), PyObject *args)
24 {
25 const char *s;
26 Py_ssize_t bsize;
27 Py_ssize_t size = -100;
28
29 if (!PyArg_ParseTuple(args, "z#|n", &s, &bsize, &size)) {
30 return NULL;
31 }
32
33 if (size == -100) {
34 size = bsize;
35 }
36 return PyByteArray_FromStringAndSize(s, size);
37 }
38
39 /* Test PyByteArray_FromObject() */
40 static PyObject *
bytearray_fromobject(PyObject * Py_UNUSED (module),PyObject * arg)41 bytearray_fromobject(PyObject *Py_UNUSED(module), PyObject *arg)
42 {
43 NULLABLE(arg);
44 return PyByteArray_FromObject(arg);
45 }
46
47 /* Test PyByteArray_Size() */
48 static PyObject *
bytearray_size(PyObject * Py_UNUSED (module),PyObject * arg)49 bytearray_size(PyObject *Py_UNUSED(module), PyObject *arg)
50 {
51 NULLABLE(arg);
52 RETURN_SIZE(PyByteArray_Size(arg));
53 }
54
55 /* Test PyUnicode_AsString() */
56 static PyObject *
bytearray_asstring(PyObject * Py_UNUSED (module),PyObject * args)57 bytearray_asstring(PyObject *Py_UNUSED(module), PyObject *args)
58 {
59 PyObject *obj;
60 Py_ssize_t buflen;
61 const char *s;
62
63 if (!PyArg_ParseTuple(args, "On", &obj, &buflen))
64 return NULL;
65
66 NULLABLE(obj);
67 s = PyByteArray_AsString(obj);
68 if (s == NULL)
69 return NULL;
70
71 return PyByteArray_FromStringAndSize(s, buflen);
72 }
73
74 /* Test PyByteArray_Concat() */
75 static PyObject *
bytearray_concat(PyObject * Py_UNUSED (module),PyObject * args)76 bytearray_concat(PyObject *Py_UNUSED(module), PyObject *args)
77 {
78 PyObject *left, *right;
79
80 if (!PyArg_ParseTuple(args, "OO", &left, &right))
81 return NULL;
82
83 NULLABLE(left);
84 NULLABLE(right);
85 return PyByteArray_Concat(left, right);
86 }
87
88 /* Test PyByteArray_Resize() */
89 static PyObject *
bytearray_resize(PyObject * Py_UNUSED (module),PyObject * args)90 bytearray_resize(PyObject *Py_UNUSED(module), PyObject *args)
91 {
92 PyObject *obj;
93 Py_ssize_t size;
94
95 if (!PyArg_ParseTuple(args, "On", &obj, &size))
96 return NULL;
97
98 NULLABLE(obj);
99 RETURN_INT(PyByteArray_Resize(obj, size));
100 }
101
102
103 static PyMethodDef test_methods[] = {
104 {"bytearray_check", bytearray_check, METH_O},
105 {"bytearray_checkexact", bytearray_checkexact, METH_O},
106 {"bytearray_fromstringandsize", bytearray_fromstringandsize, METH_VARARGS},
107 {"bytearray_fromobject", bytearray_fromobject, METH_O},
108 {"bytearray_size", bytearray_size, METH_O},
109 {"bytearray_asstring", bytearray_asstring, METH_VARARGS},
110 {"bytearray_concat", bytearray_concat, METH_VARARGS},
111 {"bytearray_resize", bytearray_resize, METH_VARARGS},
112 {NULL},
113 };
114
115 int
_PyTestLimitedCAPI_Init_ByteArray(PyObject * m)116 _PyTestLimitedCAPI_Init_ByteArray(PyObject *m)
117 {
118 if (PyModule_AddFunctions(m, test_methods) < 0) {
119 return -1;
120 }
121
122 return 0;
123 }
124