mirror of https://github.com/python/cpython
gh-84489: C API: Add tests for Py_BuildValue() (GH-110596)
This commit is contained in:
parent
3dd593e2f2
commit
5c6e85480a
|
@ -298,6 +298,86 @@ class CAPITest(unittest.TestCase):
|
|||
# test _Py_CheckFunctionResult() instead.
|
||||
self.assertIn('returned a result with an exception set', err)
|
||||
|
||||
def test_buildvalue(self):
|
||||
# Test Py_BuildValue() with object arguments
|
||||
buildvalue = _testcapi.py_buildvalue
|
||||
self.assertEqual(buildvalue(''), None)
|
||||
self.assertEqual(buildvalue('()'), ())
|
||||
self.assertEqual(buildvalue('[]'), [])
|
||||
self.assertEqual(buildvalue('{}'), {})
|
||||
self.assertEqual(buildvalue('()[]{}'), ((), [], {}))
|
||||
self.assertEqual(buildvalue('O', 1), 1)
|
||||
self.assertEqual(buildvalue('(O)', 1), (1,))
|
||||
self.assertEqual(buildvalue('[O]', 1), [1])
|
||||
self.assertRaises(SystemError, buildvalue, '{O}', 1)
|
||||
self.assertEqual(buildvalue('OO', 1, 2), (1, 2))
|
||||
self.assertEqual(buildvalue('(OO)', 1, 2), (1, 2))
|
||||
self.assertEqual(buildvalue('[OO]', 1, 2), [1, 2])
|
||||
self.assertEqual(buildvalue('{OO}', 1, 2), {1: 2})
|
||||
self.assertEqual(buildvalue('{OOOO}', 1, 2, 3, 4), {1: 2, 3: 4})
|
||||
self.assertEqual(buildvalue('((O))', 1), ((1,),))
|
||||
self.assertEqual(buildvalue('((OO))', 1, 2), ((1, 2),))
|
||||
|
||||
self.assertEqual(buildvalue(' \t,:'), None)
|
||||
self.assertEqual(buildvalue(' O ', 1), 1)
|
||||
self.assertEqual(buildvalue('\tO\t', 1), 1)
|
||||
self.assertEqual(buildvalue('O,O', 1, 2), (1, 2))
|
||||
self.assertEqual(buildvalue('O, O', 1, 2), (1, 2))
|
||||
self.assertEqual(buildvalue('O,\tO', 1, 2), (1, 2))
|
||||
self.assertEqual(buildvalue('O O', 1, 2), (1, 2))
|
||||
self.assertEqual(buildvalue('O\tO', 1, 2), (1, 2))
|
||||
self.assertEqual(buildvalue('(O,O)', 1, 2), (1, 2))
|
||||
self.assertEqual(buildvalue('(O, O)', 1, 2), (1, 2))
|
||||
self.assertEqual(buildvalue(' ( O O) ', 1, 2), (1, 2))
|
||||
self.assertEqual(buildvalue('\t(\tO\tO)\t', 1, 2), (1, 2))
|
||||
self.assertEqual(buildvalue('[O,O]', 1, 2), [1, 2])
|
||||
self.assertEqual(buildvalue('[O, O]', 1, 2), [1, 2])
|
||||
self.assertEqual(buildvalue(' [ O O] ', 1, 2), [1, 2])
|
||||
self.assertEqual(buildvalue('{O:O}', 1, 2), {1: 2})
|
||||
self.assertEqual(buildvalue('{O:O,O:O}', 1, 2, 3, 4), {1: 2, 3: 4})
|
||||
self.assertEqual(buildvalue('{O: O, O: O}', 1, 2, 3, 4), {1: 2, 3: 4})
|
||||
self.assertEqual(buildvalue(' { O O O O} ', 1, 2, 3, 4), {1: 2, 3: 4})
|
||||
self.assertEqual(buildvalue('\t{\tO\tO\tO\tO}\t', 1, 2, 3, 4), {1: 2, 3: 4})
|
||||
|
||||
self.assertRaises(SystemError, buildvalue, 'O', NULL)
|
||||
self.assertRaises(SystemError, buildvalue, '(O)', NULL)
|
||||
self.assertRaises(SystemError, buildvalue, '[O]', NULL)
|
||||
self.assertRaises(SystemError, buildvalue, '{O}', NULL)
|
||||
self.assertRaises(SystemError, buildvalue, 'OO', 1, NULL)
|
||||
self.assertRaises(SystemError, buildvalue, 'OO', NULL, 2)
|
||||
self.assertRaises(SystemError, buildvalue, '(OO)', 1, NULL)
|
||||
self.assertRaises(SystemError, buildvalue, '(OO)', NULL, 2)
|
||||
self.assertRaises(SystemError, buildvalue, '[OO]', 1, NULL)
|
||||
self.assertRaises(SystemError, buildvalue, '[OO]', NULL, 2)
|
||||
self.assertRaises(SystemError, buildvalue, '{OO}', 1, NULL)
|
||||
self.assertRaises(SystemError, buildvalue, '{OO}', NULL, 2)
|
||||
|
||||
def test_buildvalue_ints(self):
|
||||
# Test Py_BuildValue() with integer arguments
|
||||
buildvalue = _testcapi.py_buildvalue_ints
|
||||
from _testcapi import SHRT_MIN, SHRT_MAX, USHRT_MAX, INT_MIN, INT_MAX, UINT_MAX
|
||||
self.assertEqual(buildvalue('i', INT_MAX), INT_MAX)
|
||||
self.assertEqual(buildvalue('i', INT_MIN), INT_MIN)
|
||||
self.assertEqual(buildvalue('I', UINT_MAX), UINT_MAX)
|
||||
|
||||
self.assertEqual(buildvalue('h', SHRT_MAX), SHRT_MAX)
|
||||
self.assertEqual(buildvalue('h', SHRT_MIN), SHRT_MIN)
|
||||
self.assertEqual(buildvalue('H', USHRT_MAX), USHRT_MAX)
|
||||
|
||||
self.assertEqual(buildvalue('b', 127), 127)
|
||||
self.assertEqual(buildvalue('b', -128), -128)
|
||||
self.assertEqual(buildvalue('B', 255), 255)
|
||||
|
||||
self.assertEqual(buildvalue('c', ord('A')), b'A')
|
||||
self.assertEqual(buildvalue('c', 255), b'\xff')
|
||||
self.assertEqual(buildvalue('c', 256), b'\x00')
|
||||
self.assertEqual(buildvalue('c', -1), b'\xff')
|
||||
|
||||
self.assertEqual(buildvalue('C', 255), chr(255))
|
||||
self.assertEqual(buildvalue('C', 256), chr(256))
|
||||
self.assertEqual(buildvalue('C', sys.maxunicode), chr(sys.maxunicode))
|
||||
self.assertRaises(ValueError, buildvalue, 'C', -1)
|
||||
self.assertRaises(ValueError, buildvalue, 'C', sys.maxunicode+1)
|
||||
def test_buildvalue_N(self):
|
||||
_testcapi.test_buildvalue_N()
|
||||
|
||||
|
|
|
@ -386,6 +386,41 @@ raise_error(void *unused)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
py_buildvalue(PyObject *self, PyObject *args)
|
||||
{
|
||||
const char *fmt;
|
||||
PyObject *objs[10] = {NULL};
|
||||
if (!PyArg_ParseTuple(args, "s|OOOOOOOOOO", &fmt,
|
||||
&objs[0], &objs[1], &objs[2], &objs[3], &objs[4],
|
||||
&objs[5], &objs[6], &objs[7], &objs[8], &objs[9]))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
for(int i = 0; i < 10; i++) {
|
||||
NULLABLE(objs[i]);
|
||||
}
|
||||
return Py_BuildValue(fmt,
|
||||
objs[0], objs[1], objs[2], objs[3], objs[4],
|
||||
objs[5], objs[6], objs[7], objs[8], objs[9]);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
py_buildvalue_ints(PyObject *self, PyObject *args)
|
||||
{
|
||||
const char *fmt;
|
||||
unsigned int values[10] = {0};
|
||||
if (!PyArg_ParseTuple(args, "s|IIIIIIIIII", &fmt,
|
||||
&values[0], &values[1], &values[2], &values[3], &values[4],
|
||||
&values[5], &values[6], &values[7], &values[8], &values[9]))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
return Py_BuildValue(fmt,
|
||||
values[0], values[1], values[2], values[3], values[4],
|
||||
values[5], values[6], values[7], values[8], values[9]);
|
||||
}
|
||||
|
||||
static int
|
||||
test_buildvalue_N_error(const char *fmt)
|
||||
{
|
||||
|
@ -3252,6 +3287,8 @@ static PyMethodDef TestMethods[] = {
|
|||
#endif
|
||||
{"getbuffer_with_null_view", getbuffer_with_null_view, METH_O},
|
||||
{"PyBuffer_SizeFromFormat", test_PyBuffer_SizeFromFormat, METH_VARARGS},
|
||||
{"py_buildvalue", py_buildvalue, METH_VARARGS},
|
||||
{"py_buildvalue_ints", py_buildvalue_ints, METH_VARARGS},
|
||||
{"test_buildvalue_N", test_buildvalue_N, METH_NOARGS},
|
||||
{"test_get_statictype_slots", test_get_statictype_slots, METH_NOARGS},
|
||||
{"test_get_type_name", test_get_type_name, METH_NOARGS},
|
||||
|
|
Loading…
Reference in New Issue