Skip to content

Commit 611ad22

Browse files
committed
Add test
1 parent 66101e0 commit 611ad22

3 files changed

Lines changed: 36 additions & 1 deletion

File tree

‎Lib/test/test_capi/test_abstract.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,20 @@ def test_object_delattrstring(self):
339339
# CRASHES delattrstring(obj, NULL)
340340
# CRASHES delattrstring(NULL, b'a')
341341

342+
def test_copy_to_object(self):
343+
copy_to_object = _testcapi.object_copy_to_object
344+
s1 = copy_to_object(bytes(3), 'abc', 'C')
345+
s2 = copy_to_object(bytes(3), 'abc', 'F')
346+
s3 = copy_to_object(bytes(3), 'abc', 'A')
347+
self.assertEqual(s1, s2)
348+
self.assertEqual(s2, s3)
349+
self.assertEqual(s1, s3)
350+
self.assertRaises(BufferError, copy_to_object, bytes(2), 'abc', 'C')
351+
self.assertRaises(BufferError, copy_to_object, bytes(2), 'abc', 'F')
352+
self.assertRaises(BufferError, copy_to_object, bytes(2), 'abc', 'A')
353+
self.assertRaises(TypeError, copy_to_object, list(), 'abc', 'C')
354+
self.assertRaises(TypeError, copy_to_object, list(), 'abc', 'F')
355+
self.assertRaises(TypeError, copy_to_object, list(), 'abc', 'A')
342356

343357
def test_mapping_check(self):
344358
check = _testlimitedcapi.mapping_check

‎Modules/_testcapi/abstract.c‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,25 @@ object_hasattrstringwitherror(PyObject *self, PyObject *args)
7878
RETURN_INT(PyObject_HasAttrStringWithError(obj, attr_name));
7979
}
8080

81+
static PyObject *
82+
object_copy_to_object(PyObject *self, PyObject *args)
83+
{
84+
PyObject *obj;
85+
Py_ssize_t len;
86+
int result;
87+
char *buf, fort;
88+
89+
if (!PyArg_ParseTuple(args, "Os#C", &obj, &buf, &len, &fort)) {
90+
return NULL;
91+
}
92+
result = PyObject_CopyToObject(obj, buf, len, fort);
93+
if (result < 0) {
94+
return NULL;
95+
}
96+
Py_INCREF(obj);
97+
return obj;
98+
}
99+
81100
static PyObject *
82101
mapping_getoptionalitemstring(PyObject *self, PyObject *args)
83102
{
@@ -162,6 +181,8 @@ static PyMethodDef test_methods[] = {
162181
{"object_getoptionalattrstring", object_getoptionalattrstring, METH_VARARGS},
163182
{"object_hasattrwitherror", object_hasattrwitherror, METH_VARARGS},
164183
{"object_hasattrstringwitherror", object_hasattrstringwitherror, METH_VARARGS},
184+
{"object_copy_to_object", object_copy_to_object, METH_VARARGS},
185+
165186
{"mapping_getoptionalitem", mapping_getoptionalitem, METH_VARARGS},
166187
{"mapping_getoptionalitemstring", mapping_getoptionalitemstring, METH_VARARGS},
167188

‎Objects/abstract.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ PyObject_CopyToObject(PyObject *obj, void *buf, Py_ssize_t len, char fort)
741741
Py_buffer view_obj;
742742
Py_ssize_t *indices, elem_num = 1;
743743

744-
assert(buf == NULL);
744+
assert(buf != NULL);
745745
if (!PyObject_CheckBuffer(obj)) {
746746
PyErr_SetString(PyExc_TypeError,
747747
"object supporting the buffer protocol required");

0 commit comments

Comments
 (0)