gh-111495: Add tests for PyBytes and PyByteArray C API (GH-111496)

This commit is contained in:
Serhiy Storchaka 2023-11-01 17:31:07 +02:00 committed by GitHub
parent 937872e8ea
commit 97b3cd38d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 725 additions and 1 deletions

View File

@ -0,0 +1,162 @@
import unittest
import sys
from test.support import import_helper
_testcapi = import_helper.import_module('_testcapi')
NULL = None
class ByteArraySubclass(bytearray):
pass
class BytesLike:
def __init__(self, value):
self.value = value
def __bytes__(self):
return self.value
class CAPITest(unittest.TestCase):
def test_check(self):
# Test PyByteArray_Check()
check = _testcapi.bytearray_check
self.assertTrue(check(bytearray(b'abc')))
self.assertFalse(check(b'abc'))
self.assertTrue(check(ByteArraySubclass(b'abc')))
self.assertFalse(check(BytesLike(b'abc')))
self.assertFalse(check(3))
self.assertFalse(check([]))
self.assertFalse(check(object()))
# CRASHES check(NULL)
def test_checkexact(self):
# Test PyByteArray_CheckExact()
check = _testcapi.bytearray_checkexact
self.assertTrue(check(bytearray(b'abc')))
self.assertFalse(check(b'abc'))
self.assertFalse(check(ByteArraySubclass(b'abc')))
self.assertFalse(check(BytesLike(b'abc')))
self.assertFalse(check(3))
self.assertFalse(check([]))
self.assertFalse(check(object()))
# CRASHES check(NULL)
def test_fromstringandsize(self):
# Test PyByteArray_FromStringAndSize()
fromstringandsize = _testcapi.bytearray_fromstringandsize
self.assertEqual(fromstringandsize(b'abc'), bytearray(b'abc'))
self.assertEqual(fromstringandsize(b'abc', 2), bytearray(b'ab'))
self.assertEqual(fromstringandsize(b'abc\0def'), bytearray(b'abc\0def'))
self.assertEqual(fromstringandsize(b'', 0), bytearray())
self.assertEqual(fromstringandsize(NULL, 0), bytearray())
self.assertEqual(len(fromstringandsize(NULL, 3)), 3)
self.assertRaises(MemoryError, fromstringandsize, NULL, sys.maxsize)
self.assertRaises(SystemError, fromstringandsize, b'abc', -1)
self.assertRaises(SystemError, fromstringandsize, NULL, -1)
def test_fromobject(self):
# Test PyByteArray_FromObject()
fromobject = _testcapi.bytearray_fromobject
self.assertEqual(fromobject(b'abc'), bytearray(b'abc'))
self.assertEqual(fromobject(bytearray(b'abc')), bytearray(b'abc'))
self.assertEqual(fromobject(ByteArraySubclass(b'abc')), bytearray(b'abc'))
self.assertEqual(fromobject([97, 98, 99]), bytearray(b'abc'))
self.assertEqual(fromobject(3), bytearray(b'\0\0\0'))
self.assertRaises(TypeError, fromobject, BytesLike(b'abc'))
self.assertRaises(TypeError, fromobject, 'abc')
self.assertRaises(TypeError, fromobject, object())
# CRASHES fromobject(NULL)
def test_size(self):
# Test PyByteArray_Size()
size = _testcapi.bytearray_size
self.assertEqual(size(bytearray(b'abc')), 3)
self.assertEqual(size(ByteArraySubclass(b'abc')), 3)
# CRASHES size(b'abc')
# CRASHES size(object())
# CRASHES size(NULL)
def test_asstring(self):
"""Test PyByteArray_AsString()"""
asstring = _testcapi.bytearray_asstring
self.assertEqual(asstring(bytearray(b'abc'), 4), b'abc\0')
self.assertEqual(asstring(ByteArraySubclass(b'abc'), 4), b'abc\0')
self.assertEqual(asstring(bytearray(b'abc\0def'), 8), b'abc\0def\0')
# CRASHES asstring(b'abc', 0)
# CRASHES asstring(object()', 0)
# CRASHES asstring(NULL, 0)
def test_concat(self):
"""Test PyByteArray_Concat()"""
concat = _testcapi.bytearray_concat
ba = bytearray(b'abc')
self.assertEqual(concat(ba, b'def'), bytearray(b'abcdef'))
self.assertEqual(ba, b'abc')
self.assertEqual(concat(b'abc', b'def'), bytearray(b'abcdef'))
self.assertEqual(concat(b'a\0b', b'c\0d'), bytearray(b'a\0bc\0d'))
self.assertEqual(concat(bytearray(b'abc'), b'def'), bytearray(b'abcdef'))
self.assertEqual(concat(b'abc', bytearray(b'def')), bytearray(b'abcdef'))
self.assertEqual(concat(bytearray(b'abc'), b''), bytearray(b'abc'))
self.assertEqual(concat(b'', bytearray(b'def')), bytearray(b'def'))
self.assertEqual(concat(memoryview(b'xabcy')[1:4], b'def'),
bytearray(b'abcdef'))
self.assertEqual(concat(b'abc', memoryview(b'xdefy')[1:4]),
bytearray(b'abcdef'))
self.assertRaises(TypeError, concat, memoryview(b'axbycz')[::2], b'def')
self.assertRaises(TypeError, concat, b'abc', memoryview(b'dxeyfz')[::2])
self.assertRaises(TypeError, concat, b'abc', 'def')
self.assertRaises(TypeError, concat, 'abc', b'def')
self.assertRaises(TypeError, concat, 'abc', 'def')
self.assertRaises(TypeError, concat, [], b'def')
self.assertRaises(TypeError, concat, b'abc', [])
self.assertRaises(TypeError, concat, [], [])
# CRASHES concat(NULL, bytearray(b'def'))
# CRASHES concat(bytearray(b'abc'), NULL)
# CRASHES concat(NULL, object())
# CRASHES concat(object(), NULL)
def test_resize(self):
"""Test PyByteArray_Resize()"""
resize = _testcapi.bytearray_resize
ba = bytearray(b'abcdef')
self.assertEqual(resize(ba, 3), 0)
self.assertEqual(ba, bytearray(b'abc'))
self.assertEqual(resize(ba, 10), 0)
self.assertEqual(len(ba), 10)
self.assertEqual(ba[:3], bytearray(b'abc'))
self.assertEqual(resize(ba, 2**20), 0)
self.assertEqual(len(ba), 2**20)
self.assertEqual(ba[:3], bytearray(b'abc'))
self.assertEqual(resize(ba, 0), 0)
self.assertEqual(ba, bytearray())
ba = ByteArraySubclass(b'abcdef')
self.assertEqual(resize(ba, 3), 0)
self.assertEqual(ba, bytearray(b'abc'))
self.assertRaises(MemoryError, resize, bytearray(), sys.maxsize)
self.assertRaises(MemoryError, resize, bytearray(1000), sys.maxsize)
# CRASHES resize(bytearray(b'abc'), -1)
# CRASHES resize(b'abc', 0)
# CRASHES resize(object(), 0)
# CRASHES resize(NULL, 0)
if __name__ == "__main__":
unittest.main()

View File

@ -0,0 +1,216 @@
import unittest
import sys
from test.support import import_helper
_testcapi = import_helper.import_module('_testcapi')
NULL = None
class BytesSubclass(bytes):
pass
class BytesLike:
def __init__(self, value):
self.value = value
def __bytes__(self):
return self.value
class CAPITest(unittest.TestCase):
def test_check(self):
# Test PyBytes_Check()
check = _testcapi.bytes_check
self.assertTrue(check(b'abc'))
self.assertFalse(check('abc'))
self.assertFalse(check(bytearray(b'abc')))
self.assertTrue(check(BytesSubclass(b'abc')))
self.assertFalse(check(BytesLike(b'abc')))
self.assertFalse(check(3))
self.assertFalse(check([]))
self.assertFalse(check(object()))
# CRASHES check(NULL)
def test_checkexact(self):
# Test PyBytes_CheckExact()
check = _testcapi.bytes_checkexact
self.assertTrue(check(b'abc'))
self.assertFalse(check('abc'))
self.assertFalse(check(bytearray(b'abc')))
self.assertFalse(check(BytesSubclass(b'abc')))
self.assertFalse(check(BytesLike(b'abc')))
self.assertFalse(check(3))
self.assertFalse(check([]))
self.assertFalse(check(object()))
# CRASHES check(NULL)
def test_fromstringandsize(self):
# Test PyBytes_FromStringAndSize()
fromstringandsize = _testcapi.bytes_fromstringandsize
self.assertEqual(fromstringandsize(b'abc'), b'abc')
self.assertEqual(fromstringandsize(b'abc', 2), b'ab')
self.assertEqual(fromstringandsize(b'abc\0def'), b'abc\0def')
self.assertEqual(fromstringandsize(b'', 0), b'')
self.assertEqual(fromstringandsize(NULL, 0), b'')
self.assertEqual(len(fromstringandsize(NULL, 3)), 3)
self.assertRaises((MemoryError, OverflowError), fromstringandsize, NULL, sys.maxsize)
self.assertRaises(SystemError, fromstringandsize, b'abc', -1)
self.assertRaises(SystemError, fromstringandsize, NULL, -1)
def test_fromstring(self):
# Test PyBytes_FromString()
fromstring = _testcapi.bytes_fromstring
self.assertEqual(fromstring(b'abc\0def'), b'abc')
self.assertEqual(fromstring(b''), b'')
# CRASHES fromstring(NULL)
def test_fromobject(self):
# Test PyBytes_FromObject()
fromobject = _testcapi.bytes_fromobject
self.assertEqual(fromobject(b'abc'), b'abc')
self.assertEqual(fromobject(bytearray(b'abc')), b'abc')
self.assertEqual(fromobject(BytesSubclass(b'abc')), b'abc')
self.assertEqual(fromobject([97, 98, 99]), b'abc')
self.assertRaises(TypeError, fromobject, 3)
self.assertRaises(TypeError, fromobject, BytesLike(b'abc'))
self.assertRaises(TypeError, fromobject, 'abc')
self.assertRaises(TypeError, fromobject, object())
self.assertRaises(SystemError, fromobject, NULL)
def test_size(self):
# Test PyBytes_Size()
size = _testcapi.bytes_size
self.assertEqual(size(b'abc'), 3)
self.assertEqual(size(BytesSubclass(b'abc')), 3)
self.assertRaises(TypeError, size, bytearray(b'abc'))
self.assertRaises(TypeError, size, 'abc')
self.assertRaises(TypeError, size, object())
# CRASHES size(NULL)
def test_asstring(self):
"""Test PyBytes_AsString()"""
asstring = _testcapi.bytes_asstring
self.assertEqual(asstring(b'abc', 4), b'abc\0')
self.assertEqual(asstring(b'abc\0def', 8), b'abc\0def\0')
self.assertRaises(TypeError, asstring, 'abc', 0)
self.assertRaises(TypeError, asstring, object(), 0)
# CRASHES asstring(NULL, 0)
def test_asstringandsize(self):
"""Test PyBytes_AsStringAndSize()"""
asstringandsize = _testcapi.bytes_asstringandsize
asstringandsize_null = _testcapi.bytes_asstringandsize_null
self.assertEqual(asstringandsize(b'abc', 4), (b'abc\0', 3))
self.assertEqual(asstringandsize(b'abc\0def', 8), (b'abc\0def\0', 7))
self.assertEqual(asstringandsize_null(b'abc', 4), b'abc\0')
self.assertRaises(ValueError, asstringandsize_null, b'abc\0def', 8)
self.assertRaises(TypeError, asstringandsize, 'abc', 0)
self.assertRaises(TypeError, asstringandsize_null, 'abc', 0)
self.assertRaises(TypeError, asstringandsize, object(), 0)
self.assertRaises(TypeError, asstringandsize_null, object(), 0)
# CRASHES asstringandsize(NULL, 0)
# CRASHES asstringandsize_null(NULL, 0)
def test_repr(self):
# Test PyBytes_Repr()
bytes_repr = _testcapi.bytes_repr
self.assertEqual(bytes_repr(b'''abc''', 0), r"""b'abc'""")
self.assertEqual(bytes_repr(b'''abc''', 1), r"""b'abc'""")
self.assertEqual(bytes_repr(b'''a'b"c"d''', 0), r"""b'a\'b"c"d'""")
self.assertEqual(bytes_repr(b'''a'b"c"d''', 1), r"""b'a\'b"c"d'""")
self.assertEqual(bytes_repr(b'''a'b"c''', 0), r"""b'a\'b"c'""")
self.assertEqual(bytes_repr(b'''a'b"c''', 1), r"""b'a\'b"c'""")
self.assertEqual(bytes_repr(b'''a'b'c"d''', 0), r"""b'a\'b\'c"d'""")
self.assertEqual(bytes_repr(b'''a'b'c"d''', 1), r"""b'a\'b\'c"d'""")
self.assertEqual(bytes_repr(b'''a'b'c'd''', 0), r"""b'a\'b\'c\'d'""")
self.assertEqual(bytes_repr(b'''a'b'c'd''', 1), r'''b"a'b'c'd"''')
self.assertEqual(bytes_repr(BytesSubclass(b'abc'), 0), r"""b'abc'""")
# UDEFINED bytes_repr(object(), 0)
# CRASHES bytes_repr(NULL, 0)
def test_concat(self, concat=None):
"""Test PyBytes_Concat()"""
if concat is None:
concat = _testcapi.bytes_concat
self.assertEqual(concat(b'abc', b'def'), b'abcdef')
self.assertEqual(concat(b'a\0b', b'c\0d'), b'a\0bc\0d')
self.assertEqual(concat(bytearray(b'abc'), b'def'), b'abcdef')
self.assertEqual(concat(b'abc', bytearray(b'def')), b'abcdef')
self.assertEqual(concat(bytearray(b'abc'), b''), b'abc')
self.assertEqual(concat(b'', bytearray(b'def')), b'def')
self.assertEqual(concat(memoryview(b'xabcy')[1:4], b'def'), b'abcdef')
self.assertEqual(concat(b'abc', memoryview(b'xdefy')[1:4]), b'abcdef')
self.assertEqual(concat(b'abc', b'def', True), b'abcdef')
self.assertEqual(concat(b'abc', bytearray(b'def'), True), b'abcdef')
# Check that it does not change the singleton
self.assertEqual(concat(bytes(), b'def', True), b'def')
self.assertEqual(len(bytes()), 0)
self.assertRaises(TypeError, concat, memoryview(b'axbycz')[::2], b'def')
self.assertRaises(TypeError, concat, b'abc', memoryview(b'dxeyfz')[::2])
self.assertRaises(TypeError, concat, b'abc', 'def')
self.assertRaises(TypeError, concat, 'abc', b'def')
self.assertRaises(TypeError, concat, 'abc', 'def')
self.assertRaises(TypeError, concat, [], b'def')
self.assertRaises(TypeError, concat, b'abc', [])
self.assertRaises(TypeError, concat, [], [])
self.assertEqual(concat(NULL, b'def'), NULL)
self.assertEqual(concat(b'abc', NULL), NULL)
self.assertEqual(concat(NULL, object()), NULL)
self.assertEqual(concat(object(), NULL), NULL)
def test_concatanddel(self):
"""Test PyBytes_ConcatAndDel()"""
self.test_concat(_testcapi.bytes_concatanddel)
def test_decodeescape(self):
"""Test PyBytes_DecodeEscape()"""
decodeescape = _testcapi.bytes_decodeescape
self.assertEqual(decodeescape(b'abc'), b'abc')
self.assertEqual(decodeescape(br'\t\n\r\x0b\x0c\x00\\\'\"'),
b'''\t\n\r\v\f\0\\'"''')
self.assertEqual(decodeescape(b'\t\n\r\x0b\x0c\x00'), b'\t\n\r\v\f\0')
self.assertEqual(decodeescape(br'\xa1\xa2'), b'\xa1\xa2')
self.assertEqual(decodeescape(br'\2\24\241'), b'\x02\x14\xa1')
self.assertEqual(decodeescape(b'\xa1\xa2'), b'\xa1\xa2')
with self.assertWarns(DeprecationWarning):
self.assertEqual(decodeescape(br'\u4f60'), br'\u4f60')
with self.assertWarns(DeprecationWarning):
self.assertEqual(decodeescape(br'\z'), br'\z')
with self.assertWarns(DeprecationWarning):
self.assertEqual(decodeescape(br'\541'), b'a')
for b in b'\\', br'\x', br'\xa', br'\xz', br'\xaz':
self.assertRaises(ValueError, decodeescape, b)
self.assertRaises(ValueError, decodeescape, b, 'strict')
self.assertEqual(decodeescape(br'x\xa', 'replace'), b'x?')
self.assertEqual(decodeescape(br'x\xay', 'replace'), b'x?y')
self.assertEqual(decodeescape(br'x\xa\xy', 'replace'), b'x??y')
self.assertEqual(decodeescape(br'x\xa\xy', 'ignore'), b'xy')
self.assertRaises(ValueError, decodeescape, b'\\', 'spam')
self.assertEqual(decodeescape(NULL), b'')
# CRASHES decodeescape(NULL, NULL, 1)
if __name__ == "__main__":
unittest.main()

View File

@ -197,7 +197,9 @@ class CAPITest(unittest.TestCase):
self.assertEqual(fromstringandsize(NULL, 0), '')
self.assertRaises(SystemError, fromstringandsize, b'abc', -1)
# TODO: Test PyUnicode_FromStringAndSize(NULL, size) for size != 0
self.assertRaises(SystemError, fromstringandsize, NULL, -1)
self.assertRaises(SystemError, fromstringandsize, NULL, 3)
self.assertRaises(SystemError, fromstringandsize, NULL, sys.maxsize)
@support.cpython_only
@unittest.skipIf(_testcapi is None, 'need _testcapi module')

View File

@ -2,7 +2,113 @@
#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},
};

View File

@ -2,7 +2,245 @@
#include "util.h"
/* Test PyBytes_Check() */
static PyObject *
bytes_check(PyObject *Py_UNUSED(module), PyObject *obj)
{
NULLABLE(obj);
return PyLong_FromLong(PyBytes_Check(obj));
}
/* Test PyBytes_CheckExact() */
static PyObject *
bytes_checkexact(PyObject *Py_UNUSED(module), PyObject *obj)
{
NULLABLE(obj);
return PyLong_FromLong(PyBytes_CheckExact(obj));
}
/* Test PyBytes_FromStringAndSize() */
static PyObject *
bytes_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 PyBytes_FromStringAndSize(s, size);
}
/* Test PyBytes_FromString() */
static PyObject *
bytes_fromstring(PyObject *Py_UNUSED(module), PyObject *arg)
{
const char *s;
Py_ssize_t size;
if (!PyArg_Parse(arg, "z#", &s, &size)) {
return NULL;
}
return PyBytes_FromString(s);
}
/* Test PyBytes_FromObject() */
static PyObject *
bytes_fromobject(PyObject *Py_UNUSED(module), PyObject *arg)
{
NULLABLE(arg);
return PyBytes_FromObject(arg);
}
/* Test PyBytes_Size() */
static PyObject *
bytes_size(PyObject *Py_UNUSED(module), PyObject *arg)
{
NULLABLE(arg);
RETURN_SIZE(PyBytes_Size(arg));
}
/* Test PyUnicode_AsString() */
static PyObject *
bytes_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 = PyBytes_AsString(obj);
if (s == NULL)
return NULL;
return PyBytes_FromStringAndSize(s, buflen);
}
/* Test PyBytes_AsStringAndSize() */
static PyObject *
bytes_asstringandsize(PyObject *Py_UNUSED(module), PyObject *args)
{
PyObject *obj;
Py_ssize_t buflen;
char *s = UNINITIALIZED_PTR;
Py_ssize_t size = UNINITIALIZED_SIZE;
if (!PyArg_ParseTuple(args, "On", &obj, &buflen))
return NULL;
NULLABLE(obj);
if (PyBytes_AsStringAndSize(obj, &s, &size) < 0) {
return NULL;
}
if (s == NULL) {
return Py_BuildValue("(On)", Py_None, size);
}
else {
return Py_BuildValue("(y#n)", s, buflen, size);
}
}
static PyObject *
bytes_asstringandsize_null(PyObject *Py_UNUSED(module), PyObject *args)
{
PyObject *obj;
Py_ssize_t buflen;
char *s = UNINITIALIZED_PTR;
if (!PyArg_ParseTuple(args, "On", &obj, &buflen))
return NULL;
NULLABLE(obj);
if (PyBytes_AsStringAndSize(obj, &s, NULL) < 0) {
return NULL;
}
if (s == NULL) {
Py_RETURN_NONE;
}
else {
return PyBytes_FromStringAndSize(s, buflen);
}
}
/* Test PyBytes_Repr() */
static PyObject *
bytes_repr(PyObject *Py_UNUSED(module), PyObject *args)
{
PyObject *obj;
int smartquotes;
if (!PyArg_ParseTuple(args, "Oi", &obj, &smartquotes))
return NULL;
NULLABLE(obj);
return PyBytes_Repr(obj, smartquotes);
}
/* Test PyBytes_Concat() */
static PyObject *
bytes_concat(PyObject *Py_UNUSED(module), PyObject *args)
{
PyObject *left, *right;
int new = 0;
if (!PyArg_ParseTuple(args, "OO|p", &left, &right, &new))
return NULL;
NULLABLE(left);
NULLABLE(right);
if (new) {
assert(left != NULL);
assert(PyBytes_CheckExact(left));
left = PyBytes_FromStringAndSize(PyBytes_AS_STRING(left),
PyBytes_GET_SIZE(left));
if (left == NULL) {
return NULL;
}
}
else {
Py_XINCREF(left);
}
PyBytes_Concat(&left, right);
if (left == NULL && !PyErr_Occurred()) {
Py_RETURN_NONE;
}
return left;
}
/* Test PyBytes_ConcatAndDel() */
static PyObject *
bytes_concatanddel(PyObject *Py_UNUSED(module), PyObject *args)
{
PyObject *left, *right;
int new = 0;
if (!PyArg_ParseTuple(args, "OO|p", &left, &right, &new))
return NULL;
NULLABLE(left);
NULLABLE(right);
if (new) {
assert(left != NULL);
assert(PyBytes_CheckExact(left));
left = PyBytes_FromStringAndSize(PyBytes_AS_STRING(left),
PyBytes_GET_SIZE(left));
if (left == NULL) {
return NULL;
}
}
else {
Py_XINCREF(left);
}
Py_XINCREF(right);
PyBytes_ConcatAndDel(&left, right);
if (left == NULL && !PyErr_Occurred()) {
Py_RETURN_NONE;
}
return left;
}
/* Test PyBytes_DecodeEscape() */
static PyObject *
bytes_decodeescape(PyObject *Py_UNUSED(module), PyObject *args)
{
const char *s;
Py_ssize_t bsize;
Py_ssize_t size = -100;
const char *errors = NULL;
if (!PyArg_ParseTuple(args, "z#|zn", &s, &bsize, &errors, &size))
return NULL;
if (size == -100) {
size = bsize;
}
return PyBytes_DecodeEscape(s, size, errors, 0, NULL);
}
static PyMethodDef test_methods[] = {
{"bytes_check", bytes_check, METH_O},
{"bytes_checkexact", bytes_checkexact, METH_O},
{"bytes_fromstringandsize", bytes_fromstringandsize, METH_VARARGS},
{"bytes_fromstring", bytes_fromstring, METH_O},
{"bytes_fromobject", bytes_fromobject, METH_O},
{"bytes_size", bytes_size, METH_O},
{"bytes_asstring", bytes_asstring, METH_VARARGS},
{"bytes_asstringandsize", bytes_asstringandsize, METH_VARARGS},
{"bytes_asstringandsize_null", bytes_asstringandsize_null, METH_VARARGS},
{"bytes_repr", bytes_repr, METH_VARARGS},
{"bytes_concat", bytes_concat, METH_VARARGS},
{"bytes_concatanddel", bytes_concatanddel, METH_VARARGS},
{"bytes_decodeescape", bytes_decodeescape, METH_VARARGS},
{NULL},
};