aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Modules/_testlimitedcapi/bytearray.c
blob: 68b029e25af6773823393cbad9e92c030476a3d3 (plain) (blame)
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
#include "parts.h"
#include "util.h"


/* Test PyByteArray_Check() */
static PyObject *
bytearray_check(PyObject *Py_UNUSED(module), PyObject *obj)
{
    NULLABLE(obj);
    return PyLong_FromLong(PyByteArray_Check(obj));
}

/* Test PyByteArray_CheckExact() */
static PyObject *
bytearray_checkexact(PyObject *Py_UNUSED(module), PyObject *obj)
{
    NULLABLE(obj);
    return PyLong_FromLong(PyByteArray_CheckExact(obj));
}

/* Test PyByteArray_FromStringAndSize() */
static PyObject *
bytearray_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 PyByteArray_FromStringAndSize(s, size);
}

/* Test PyByteArray_FromObject() */
static PyObject *
bytearray_fromobject(PyObject *Py_UNUSED(module), PyObject *arg)
{
    NULLABLE(arg);
    return PyByteArray_FromObject(arg);
}

/* Test PyByteArray_Size() */
static PyObject *
bytearray_size(PyObject *Py_UNUSED(module), PyObject *arg)
{
    NULLABLE(arg);
    RETURN_SIZE(PyByteArray_Size(arg));
}

/* Test PyUnicode_AsString() */
static PyObject *
bytearray_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 = PyByteArray_AsString(obj);
    if (s == NULL)
        return NULL;

    return PyByteArray_FromStringAndSize(s, buflen);
}

/* Test PyByteArray_Concat() */
static PyObject *
bytearray_concat(PyObject *Py_UNUSED(module), PyObject *args)
{
    PyObject *left, *right;

    if (!PyArg_ParseTuple(args, "OO", &left, &right))
        return NULL;

    NULLABLE(left);
    NULLABLE(right);
    return PyByteArray_Concat(left, right);
}

/* Test PyByteArray_Resize() */
static PyObject *
bytearray_resize(PyObject *Py_UNUSED(module), PyObject *args)
{
    PyObject *obj;
    Py_ssize_t size;

    if (!PyArg_ParseTuple(args, "On", &obj, &size))
        return NULL;

    NULLABLE(obj);
    RETURN_INT(PyByteArray_Resize(obj, size));
}


static PyMethodDef test_methods[] = {
    {"bytearray_check", bytearray_check, METH_O},
    {"bytearray_checkexact", bytearray_checkexact, METH_O},
    {"bytearray_fromstringandsize", bytearray_fromstringandsize, METH_VARARGS},
    {"bytearray_fromobject", bytearray_fromobject, METH_O},
    {"bytearray_size", bytearray_size, METH_O},
    {"bytearray_asstring", bytearray_asstring, METH_VARARGS},
    {"bytearray_concat", bytearray_concat, METH_VARARGS},
    {"bytearray_resize", bytearray_resize, METH_VARARGS},
    {NULL},
};

int
_PyTestLimitedCAPI_Init_ByteArray(PyObject *m)
{
    if (PyModule_AddFunctions(m, test_methods) < 0) {
        return -1;
    }

    return 0;
}