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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
class AppTestStringObject(AppTestCpythonExtensionBase):
def test_basic(self):
module = self.import_extension('foo', [
("get_hello1", "METH_NOARGS",
"""
return PyByteArray_FromStringAndSize(
"Hello world<should not be included>", 11);
"""),
("get_hello2", "METH_NOARGS",
"""
return PyByteArray_FromStringAndSize("Hello world", 12);
"""),
("test_Size", "METH_NOARGS",
"""
PyObject* s = PyByteArray_FromStringAndSize("Hello world", 12);
int result = 0;
if(PyByteArray_Size(s) == 12) {
result = 1;
}
Py_DECREF(s);
return PyBool_FromLong(result);
"""),
("test_is_bytearray", "METH_VARARGS",
"""
return PyBool_FromLong(PyByteArray_Check(PyTuple_GetItem(args, 0)));
""")], prologue='#include <stdlib.h>')
assert module.get_hello1() == b'Hello world'
assert module.get_hello2() == b'Hello world\x00'
assert module.test_Size()
assert module.test_is_bytearray(bytearray(b""))
assert not module.test_is_bytearray(())
def test_bytearray_buffer_init(self):
module = self.import_extension('foo', [
("getbytearray", "METH_NOARGS",
"""
PyObject *s, *t;
char* c;
s = PyByteArray_FromStringAndSize(NULL, 4);
if (s == NULL)
return NULL;
t = PyByteArray_FromStringAndSize(NULL, 3);
if (t == NULL)
return NULL;
Py_DECREF(t);
c = PyByteArray_AsString(s);
if (c == NULL)
{
PyErr_SetString(PyExc_ValueError, "non-null bytearray object expected");
return NULL;
}
c[0] = 'a';
c[1] = 'b';
c[2] = 0;
c[3] = 'c';
return s;
"""),
])
s = module.getbytearray()
assert len(s) == 4
assert s == b'ab\x00c'
def test_bytearray_mutable(self):
module = self.import_extension('foo', [
("mutable", "METH_NOARGS",
"""
PyObject *base;
base = PyByteArray_FromStringAndSize("test", 10);
if (PyByteArray_GET_SIZE(base) != 10)
return PyLong_FromLong(-PyByteArray_GET_SIZE(base));
memcpy(PyByteArray_AS_STRING(base), "works", 6);
Py_INCREF(base);
return base;
"""),
])
s = module.mutable()
if s == b'\x00' * 10:
assert False, "no RW access to bytearray"
assert s[:6] == b'works\x00'
def test_AsByteArray(self):
module = self.import_extension('foo', [
("getbytearray", "METH_NOARGS",
"""
const char *c;
PyObject *s2, *s1 = PyByteArray_FromStringAndSize("test", 4);
if (s1 == NULL)
return NULL;
c = PyByteArray_AsString(s1);
s2 = PyByteArray_FromStringAndSize(c, 4);
Py_DECREF(s1);
return s2;
"""),
])
s = module.getbytearray()
assert s == b'test'
def test_manipulations(self):
import sys
module = self.import_extension('foo', [
("bytearray_from_bytes", "METH_VARARGS",
'''
return PyByteArray_FromStringAndSize(PyBytes_AsString(
PyTuple_GetItem(args, 0)), 4);
'''
),
("bytes_from_bytearray", "METH_VARARGS",
'''
char * buf;
int n;
PyObject * obj;
obj = PyTuple_GetItem(args, 0);
buf = PyByteArray_AsString(obj);
if (buf == NULL)
{
PyErr_SetString(PyExc_ValueError, "non-null bytearray object expected");
return NULL;
}
n = PyByteArray_Size(obj);
return PyBytes_FromStringAndSize(buf, n);
'''
),
("concat", "METH_VARARGS",
"""
PyObject * ret, *right, *left;
PyObject *ba1, *ba2;
if (!PyArg_ParseTuple(args, "OO", &left, &right)) {
return PyUnicode_FromString("parse failed");
}
ba1 = PyByteArray_FromObject(left);
ba2 = PyByteArray_FromObject(right);
if (ba1 == NULL || ba2 == NULL)
{
/* exception should be set */
return NULL;
}
ret = PyByteArray_Concat(ba1, ba2);
return ret;
""")])
assert module.bytearray_from_bytes(b"huheduwe") == b"huhe"
assert module.bytes_from_bytearray(bytearray(b'abc')) == b'abc'
if '__pypy__' in sys.builtin_module_names:
# CPython only makes an assert.
raises(ValueError, module.bytes_from_bytearray, 4.0)
ret = module.concat(b'abc', b'def')
assert ret == b'abcdef'
assert not isinstance(ret, str)
assert isinstance(ret, bytearray)
raises(TypeError, module.concat, b'abc', u'def')
def test_bytearray_resize(self):
module = self.import_extension('foo', [
("bytearray_resize", "METH_VARARGS",
'''
PyObject *obj, *ba;
int newsize, oldsize, ret;
if (!PyArg_ParseTuple(args, "Oi", &obj, &newsize)) {
return PyUnicode_FromString("parse failed");
}
ba = PyByteArray_FromObject(obj);
if (ba == NULL)
return NULL;
oldsize = PyByteArray_Size(ba);
if (oldsize == 0)
{
return PyUnicode_FromString("oldsize is 0");
}
ret = PyByteArray_Resize(ba, newsize);
if (ret != 0)
{
printf("ret, oldsize, newsize= %d, %d, %d\\n", ret, oldsize, newsize);
return NULL;
}
return ba;
'''
)])
ret = module.bytearray_resize(b'abc', 6)
assert len(ret) == 6,"%s, len=%d" % (ret, len(ret))
assert ret[:4] == b'abc\x00'
ret = module.bytearray_resize(b'abcdefghi', 4)
assert len(ret) == 4,"%s, len=%d" % (ret, len(ret))
assert ret == b'abcd'
|