Debian Codegraph @

pypy/module/cpyext/test/test_bytearrayobject.py

raw symbols SCIP
1 from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
2
3
4 class AppTestStringObject(AppTestCpythonExtensionBase):
5 def test_basic(self):
6 module = self.import_extension('foo', [
7 ("get_hello1", "METH_NOARGS",
8 """
9 return PyByteArray_FromStringAndSize(
10 "Hello world<should not be included>", 11);
11 """),
12 ("get_hello2", "METH_NOARGS",
13 """
14 return PyByteArray_FromStringAndSize("Hello world", 12);
15 """),
16 ("test_Size", "METH_NOARGS",
17 """
18 PyObject* s = PyByteArray_FromStringAndSize("Hello world", 12);
19 int result = 0;
20
21 if(PyByteArray_Size(s) == 12) {
22 result = 1;
23 }
24 Py_DECREF(s);
25 return PyBool_FromLong(result);
26 """),
27 ("test_is_bytearray", "METH_VARARGS",
28 """
29 return PyBool_FromLong(PyByteArray_Check(PyTuple_GetItem(args, 0)));
30 """)], prologue='#include <stdlib.h>')
31 assert module.get_hello1() == b'Hello world'
32 assert module.get_hello2() == b'Hello world\x00'
33 assert module.test_Size()
34 assert module.test_is_bytearray(bytearray(b""))
35 assert not module.test_is_bytearray(())
36
37 def test_bytearray_buffer_init(self):
38 module = self.import_extension('foo', [
39 ("getbytearray", "METH_NOARGS",
40 """
41 PyObject *s, *t;
42 char* c;
43
44 s = PyByteArray_FromStringAndSize(NULL, 4);
45 if (s == NULL)
46 return NULL;
47 t = PyByteArray_FromStringAndSize(NULL, 3);
48 if (t == NULL)
49 return NULL;
50 Py_DECREF(t);
51 c = PyByteArray_AsString(s);
52 if (c == NULL)
53 {
54 PyErr_SetString(PyExc_ValueError, "non-null bytearray object expected");
55 return NULL;
56 }
57 c[0] = 'a';
58 c[1] = 'b';
59 c[2] = 0;
60 c[3] = 'c';
61 return s;
62 """),
63 ])
64 s = module.getbytearray()
65 assert len(s) == 4
66 assert s == b'ab\x00c'
67
68 def test_bytearray_mutable(self):
69 module = self.import_extension('foo', [
70 ("mutable", "METH_NOARGS",
71 """
72 PyObject *base;
73 base = PyByteArray_FromStringAndSize("test", 10);
74 if (PyByteArray_GET_SIZE(base) != 10)
75 return PyLong_FromLong(-PyByteArray_GET_SIZE(base));
76 memcpy(PyByteArray_AS_STRING(base), "works", 6);
77 Py_INCREF(base);
78 return base;
79 """),
80 ])
81 s = module.mutable()
82 if s == b'\x00' * 10:
83 assert False, "no RW access to bytearray"
84 assert s[:6] == b'works\x00'
85
86 def test_AsByteArray(self):
87 module = self.import_extension('foo', [
88 ("getbytearray", "METH_NOARGS",
89 """
90 const char *c;
91 PyObject *s2, *s1 = PyByteArray_FromStringAndSize("test", 4);
92 if (s1 == NULL)
93 return NULL;
94 c = PyByteArray_AsString(s1);
95 s2 = PyByteArray_FromStringAndSize(c, 4);
96 Py_DECREF(s1);
97 return s2;
98 """),
99 ])
100 s = module.getbytearray()
101 assert s == b'test'
102
103 def test_manipulations(self):
104 import sys
105 module = self.import_extension('foo', [
106 ("bytearray_from_bytes", "METH_VARARGS",
107 '''
108 return PyByteArray_FromStringAndSize(PyBytes_AsString(
109 PyTuple_GetItem(args, 0)), 4);
110 '''
111 ),
112 ("bytes_from_bytearray", "METH_VARARGS",
113 '''
114 char * buf;
115 int n;
116 PyObject * obj;
117 obj = PyTuple_GetItem(args, 0);
118 buf = PyByteArray_AsString(obj);
119 if (buf == NULL)
120 {
121 PyErr_SetString(PyExc_ValueError, "non-null bytearray object expected");
122 return NULL;
123 }
124 n = PyByteArray_Size(obj);
125 return PyBytes_FromStringAndSize(buf, n);
126 '''
127 ),
128 ("concat", "METH_VARARGS",
129 """
130 PyObject * ret, *right, *left;
131 PyObject *ba1, *ba2;
132 if (!PyArg_ParseTuple(args, "OO", &left, &right)) {
133 return PyUnicode_FromString("parse failed");
134 }
135 ba1 = PyByteArray_FromObject(left);
136 ba2 = PyByteArray_FromObject(right);
137 if (ba1 == NULL || ba2 == NULL)
138 {
139 /* exception should be set */
140 return NULL;
141 }
142 ret = PyByteArray_Concat(ba1, ba2);
143 return ret;
144 """)])
145 assert module.bytearray_from_bytes(b"huheduwe") == b"huhe"
146 assert module.bytes_from_bytearray(bytearray(b'abc')) == b'abc'
147 if '__pypy__' in sys.builtin_module_names:
148 # CPython only makes an assert.
149 raises(ValueError, module.bytes_from_bytearray, 4.0)
150 ret = module.concat(b'abc', b'def')
151 assert ret == b'abcdef'
152 assert not isinstance(ret, str)
153 assert isinstance(ret, bytearray)
154 raises(TypeError, module.concat, b'abc', u'def')
155
156 def test_bytearray_resize(self):
157 module = self.import_extension('foo', [
158 ("bytearray_resize", "METH_VARARGS",
159 '''
160 PyObject *obj, *ba;
161 int newsize, oldsize, ret;
162 if (!PyArg_ParseTuple(args, "Oi", &obj, &newsize)) {
163 return PyUnicode_FromString("parse failed");
164 }
165
166 ba = PyByteArray_FromObject(obj);
167 if (ba == NULL)
168 return NULL;
169 oldsize = PyByteArray_Size(ba);
170 if (oldsize == 0)
171 {
172 return PyUnicode_FromString("oldsize is 0");
173 }
174 ret = PyByteArray_Resize(ba, newsize);
175 if (ret != 0)
176 {
177 printf("ret, oldsize, newsize= %d, %d, %d\\n", ret, oldsize, newsize);
178 return NULL;
179 }
180 return ba;
181 '''
182 )])
183 ret = module.bytearray_resize(b'abc', 6)
184 assert len(ret) == 6,"%s, len=%d" % (ret, len(ret))
185 assert ret[:4] == b'abc\x00'
186 ret = module.bytearray_resize(b'abcdefghi', 4)
187 assert len(ret) == 4,"%s, len=%d" % (ret, len(ret))
188 assert ret == b'abcd'