From 1cc02ca063f50b8c527fbdde9957b03c145c1575 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 11 Mar 2024 11:28:16 +0100 Subject: gh-116417: Move 4 limited C API test files to _testlimitedcapi (#116571) Move the following files from Modules/_testcapi/ to Modules/_testlimitedcapi/: * bytearray.c * bytes.c * pyos.c * sys.c Changes: * Replace PyBytes_AS_STRING() with PyBytes_AsString(). * Replace PyBytes_GET_SIZE() with PyBytes_Size(). * Update related test_capi tests. * Copy Modules/_testcapi/util.h to Modules/_testlimitedcapi/util.h. --- Lib/test/test_capi/test_bytearray.py | 18 +-- Lib/test/test_capi/test_bytes.py | 28 ++-- Lib/test/test_capi/test_sys.py | 16 +- Modules/Setup.stdlib.in | 4 +- Modules/_testcapi/bytearray.c | 123 --------------- Modules/_testcapi/bytes.c | 255 ------------------------------- Modules/_testcapi/parts.h | 4 - Modules/_testcapi/pyos.c | 60 -------- Modules/_testcapi/sys.c | 56 ------- Modules/_testcapimodule.c | 12 -- Modules/_testlimitedcapi.c | 14 +- Modules/_testlimitedcapi/bytearray.c | 123 +++++++++++++++ Modules/_testlimitedcapi/bytes.c | 255 +++++++++++++++++++++++++++++++ Modules/_testlimitedcapi/parts.h | 6 +- Modules/_testlimitedcapi/pyos.c | 60 ++++++++ Modules/_testlimitedcapi/sys.c | 56 +++++++ Modules/_testlimitedcapi/util.h | 33 ++++ PCbuild/_testcapi.vcxproj | 4 - PCbuild/_testcapi.vcxproj.filters | 12 -- PCbuild/_testlimitedcapi.vcxproj | 8 +- PCbuild/_testlimitedcapi.vcxproj.filters | 14 +- 21 files changed, 591 insertions(+), 570 deletions(-) delete mode 100644 Modules/_testcapi/bytearray.c delete mode 100644 Modules/_testcapi/bytes.c delete mode 100644 Modules/_testcapi/pyos.c delete mode 100644 Modules/_testcapi/sys.c create mode 100644 Modules/_testlimitedcapi/bytearray.c create mode 100644 Modules/_testlimitedcapi/bytes.c create mode 100644 Modules/_testlimitedcapi/pyos.c create mode 100644 Modules/_testlimitedcapi/sys.c create mode 100644 Modules/_testlimitedcapi/util.h diff --git a/Lib/test/test_capi/test_bytearray.py b/Lib/test/test_capi/test_bytearray.py index 833122c..39099f6 100644 --- a/Lib/test/test_capi/test_bytearray.py +++ b/Lib/test/test_capi/test_bytearray.py @@ -1,7 +1,7 @@ import unittest from test.support import import_helper -_testcapi = import_helper.import_module('_testcapi') +_testlimitedcapi = import_helper.import_module('_testlimitedcapi') from _testcapi import PY_SSIZE_T_MIN, PY_SSIZE_T_MAX NULL = None @@ -19,7 +19,7 @@ class BytesLike: class CAPITest(unittest.TestCase): def test_check(self): # Test PyByteArray_Check() - check = _testcapi.bytearray_check + check = _testlimitedcapi.bytearray_check self.assertTrue(check(bytearray(b'abc'))) self.assertFalse(check(b'abc')) self.assertTrue(check(ByteArraySubclass(b'abc'))) @@ -32,7 +32,7 @@ class CAPITest(unittest.TestCase): def test_checkexact(self): # Test PyByteArray_CheckExact() - check = _testcapi.bytearray_checkexact + check = _testlimitedcapi.bytearray_checkexact self.assertTrue(check(bytearray(b'abc'))) self.assertFalse(check(b'abc')) self.assertFalse(check(ByteArraySubclass(b'abc'))) @@ -45,7 +45,7 @@ class CAPITest(unittest.TestCase): def test_fromstringandsize(self): # Test PyByteArray_FromStringAndSize() - fromstringandsize = _testcapi.bytearray_fromstringandsize + fromstringandsize = _testlimitedcapi.bytearray_fromstringandsize self.assertEqual(fromstringandsize(b'abc'), bytearray(b'abc')) self.assertEqual(fromstringandsize(b'abc', 2), bytearray(b'ab')) @@ -62,7 +62,7 @@ class CAPITest(unittest.TestCase): def test_fromobject(self): # Test PyByteArray_FromObject() - fromobject = _testcapi.bytearray_fromobject + fromobject = _testlimitedcapi.bytearray_fromobject self.assertEqual(fromobject(b'abc'), bytearray(b'abc')) self.assertEqual(fromobject(bytearray(b'abc')), bytearray(b'abc')) @@ -77,7 +77,7 @@ class CAPITest(unittest.TestCase): def test_size(self): # Test PyByteArray_Size() - size = _testcapi.bytearray_size + size = _testlimitedcapi.bytearray_size self.assertEqual(size(bytearray(b'abc')), 3) self.assertEqual(size(ByteArraySubclass(b'abc')), 3) @@ -88,7 +88,7 @@ class CAPITest(unittest.TestCase): def test_asstring(self): """Test PyByteArray_AsString()""" - asstring = _testcapi.bytearray_asstring + asstring = _testlimitedcapi.bytearray_asstring self.assertEqual(asstring(bytearray(b'abc'), 4), b'abc\0') self.assertEqual(asstring(ByteArraySubclass(b'abc'), 4), b'abc\0') @@ -100,7 +100,7 @@ class CAPITest(unittest.TestCase): def test_concat(self): """Test PyByteArray_Concat()""" - concat = _testcapi.bytearray_concat + concat = _testlimitedcapi.bytearray_concat ba = bytearray(b'abc') self.assertEqual(concat(ba, b'def'), bytearray(b'abcdef')) @@ -133,7 +133,7 @@ class CAPITest(unittest.TestCase): def test_resize(self): """Test PyByteArray_Resize()""" - resize = _testcapi.bytearray_resize + resize = _testlimitedcapi.bytearray_resize ba = bytearray(b'abcdef') self.assertEqual(resize(ba, 3), 0) diff --git a/Lib/test/test_capi/test_bytes.py b/Lib/test/test_capi/test_bytes.py index bb5d724..a2ba770 100644 --- a/Lib/test/test_capi/test_bytes.py +++ b/Lib/test/test_capi/test_bytes.py @@ -1,7 +1,7 @@ import unittest from test.support import import_helper -_testcapi = import_helper.import_module('_testcapi') +_testlimitedcapi = import_helper.import_module('_testlimitedcapi') from _testcapi import PY_SSIZE_T_MIN, PY_SSIZE_T_MAX NULL = None @@ -19,7 +19,7 @@ class BytesLike: class CAPITest(unittest.TestCase): def test_check(self): # Test PyBytes_Check() - check = _testcapi.bytes_check + check = _testlimitedcapi.bytes_check self.assertTrue(check(b'abc')) self.assertFalse(check('abc')) self.assertFalse(check(bytearray(b'abc'))) @@ -33,7 +33,7 @@ class CAPITest(unittest.TestCase): def test_checkexact(self): # Test PyBytes_CheckExact() - check = _testcapi.bytes_checkexact + check = _testlimitedcapi.bytes_checkexact self.assertTrue(check(b'abc')) self.assertFalse(check('abc')) self.assertFalse(check(bytearray(b'abc'))) @@ -47,7 +47,7 @@ class CAPITest(unittest.TestCase): def test_fromstringandsize(self): # Test PyBytes_FromStringAndSize() - fromstringandsize = _testcapi.bytes_fromstringandsize + fromstringandsize = _testlimitedcapi.bytes_fromstringandsize self.assertEqual(fromstringandsize(b'abc'), b'abc') self.assertEqual(fromstringandsize(b'abc', 2), b'ab') @@ -65,7 +65,7 @@ class CAPITest(unittest.TestCase): def test_fromstring(self): # Test PyBytes_FromString() - fromstring = _testcapi.bytes_fromstring + fromstring = _testlimitedcapi.bytes_fromstring self.assertEqual(fromstring(b'abc\0def'), b'abc') self.assertEqual(fromstring(b''), b'') @@ -74,7 +74,7 @@ class CAPITest(unittest.TestCase): def test_fromobject(self): # Test PyBytes_FromObject() - fromobject = _testcapi.bytes_fromobject + fromobject = _testlimitedcapi.bytes_fromobject self.assertEqual(fromobject(b'abc'), b'abc') self.assertEqual(fromobject(bytearray(b'abc')), b'abc') @@ -88,7 +88,7 @@ class CAPITest(unittest.TestCase): def test_size(self): # Test PyBytes_Size() - size = _testcapi.bytes_size + size = _testlimitedcapi.bytes_size self.assertEqual(size(b'abc'), 3) self.assertEqual(size(BytesSubclass(b'abc')), 3) @@ -100,7 +100,7 @@ class CAPITest(unittest.TestCase): def test_asstring(self): """Test PyBytes_AsString()""" - asstring = _testcapi.bytes_asstring + asstring = _testlimitedcapi.bytes_asstring self.assertEqual(asstring(b'abc', 4), b'abc\0') self.assertEqual(asstring(b'abc\0def', 8), b'abc\0def\0') @@ -111,8 +111,8 @@ class CAPITest(unittest.TestCase): def test_asstringandsize(self): """Test PyBytes_AsStringAndSize()""" - asstringandsize = _testcapi.bytes_asstringandsize - asstringandsize_null = _testcapi.bytes_asstringandsize_null + asstringandsize = _testlimitedcapi.bytes_asstringandsize + asstringandsize_null = _testlimitedcapi.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)) @@ -128,7 +128,7 @@ class CAPITest(unittest.TestCase): def test_repr(self): # Test PyBytes_Repr() - bytes_repr = _testcapi.bytes_repr + bytes_repr = _testlimitedcapi.bytes_repr self.assertEqual(bytes_repr(b'''abc''', 0), r"""b'abc'""") self.assertEqual(bytes_repr(b'''abc''', 1), r"""b'abc'""") @@ -149,7 +149,7 @@ class CAPITest(unittest.TestCase): def test_concat(self, concat=None): """Test PyBytes_Concat()""" if concat is None: - concat = _testcapi.bytes_concat + concat = _testlimitedcapi.bytes_concat self.assertEqual(concat(b'abc', b'def'), b'abcdef') self.assertEqual(concat(b'a\0b', b'c\0d'), b'a\0bc\0d') @@ -182,11 +182,11 @@ class CAPITest(unittest.TestCase): def test_concatanddel(self): """Test PyBytes_ConcatAndDel()""" - self.test_concat(_testcapi.bytes_concatanddel) + self.test_concat(_testlimitedcapi.bytes_concatanddel) def test_decodeescape(self): """Test PyBytes_DecodeEscape()""" - decodeescape = _testcapi.bytes_decodeescape + decodeescape = _testlimitedcapi.bytes_decodeescape self.assertEqual(decodeescape(b'abc'), b'abc') self.assertEqual(decodeescape(br'\t\n\r\x0b\x0c\x00\\\'\"'), diff --git a/Lib/test/test_capi/test_sys.py b/Lib/test/test_capi/test_sys.py index 08bf037..54a8e02 100644 --- a/Lib/test/test_capi/test_sys.py +++ b/Lib/test/test_capi/test_sys.py @@ -5,9 +5,9 @@ from test import support from test.support import import_helper try: - import _testcapi + import _testlimitedcapi except ImportError: - _testcapi = None + _testlimitedcapi = None NULL = None @@ -20,10 +20,10 @@ class CAPITest(unittest.TestCase): maxDiff = None @support.cpython_only - @unittest.skipIf(_testcapi is None, 'need _testcapi module') + @unittest.skipIf(_testlimitedcapi is None, 'need _testlimitedcapi module') def test_sys_getobject(self): # Test PySys_GetObject() - getobject = _testcapi.sys_getobject + getobject = _testlimitedcapi.sys_getobject self.assertIs(getobject(b'stdout'), sys.stdout) with support.swap_attr(sys, '\U0001f40d', 42): @@ -38,10 +38,10 @@ class CAPITest(unittest.TestCase): # CRASHES getobject(NULL) @support.cpython_only - @unittest.skipIf(_testcapi is None, 'need _testcapi module') + @unittest.skipIf(_testlimitedcapi is None, 'need _testlimitedcapi module') def test_sys_setobject(self): # Test PySys_SetObject() - setobject = _testcapi.sys_setobject + setobject = _testlimitedcapi.sys_setobject value = ['value'] value2 = ['value2'] @@ -70,10 +70,10 @@ class CAPITest(unittest.TestCase): # CRASHES setobject(NULL, value) @support.cpython_only - @unittest.skipIf(_testcapi is None, 'need _testcapi module') + @unittest.skipIf(_testlimitedcapi is None, 'need _testlimitedcapi module') def test_sys_getxoptions(self): # Test PySys_GetXOptions() - getxoptions = _testcapi.sys_getxoptions + getxoptions = _testlimitedcapi.sys_getxoptions self.assertIs(getxoptions(), sys._xoptions) diff --git a/Modules/Setup.stdlib.in b/Modules/Setup.stdlib.in index e8eaafd..deada66 100644 --- a/Modules/Setup.stdlib.in +++ b/Modules/Setup.stdlib.in @@ -162,8 +162,8 @@ @MODULE__XXTESTFUZZ_TRUE@_xxtestfuzz _xxtestfuzz/_xxtestfuzz.c _xxtestfuzz/fuzzer.c @MODULE__TESTBUFFER_TRUE@_testbuffer _testbuffer.c @MODULE__TESTINTERNALCAPI_TRUE@_testinternalcapi _testinternalcapi.c _testinternalcapi/test_lock.c _testinternalcapi/pytime.c _testinternalcapi/set.c _testinternalcapi/test_critical_sections.c -@MODULE__TESTCAPI_TRUE@_testcapi _testcapimodule.c _testcapi/vectorcall.c _testcapi/heaptype.c _testcapi/abstract.c _testcapi/bytearray.c _testcapi/bytes.c _testcapi/unicode.c _testcapi/dict.c _testcapi/set.c _testcapi/list.c _testcapi/tuple.c _testcapi/getargs.c _testcapi/datetime.c _testcapi/docstring.c _testcapi/mem.c _testcapi/watchers.c _testcapi/long.c _testcapi/float.c _testcapi/complex.c _testcapi/numbers.c _testcapi/structmember.c _testcapi/exceptions.c _testcapi/code.c _testcapi/buffer.c _testcapi/pyatomic.c _testcapi/pyos.c _testcapi/file.c _testcapi/codec.c _testcapi/immortal.c _testcapi/gc.c _testcapi/sys.c _testcapi/hash.c _testcapi/time.c -@MODULE__TESTLIMITEDCAPI_TRUE@_testlimitedcapi _testlimitedcapi.c _testlimitedcapi/vectorcall_limited.c _testlimitedcapi/heaptype_relative.c +@MODULE__TESTCAPI_TRUE@_testcapi _testcapimodule.c _testcapi/vectorcall.c _testcapi/heaptype.c _testcapi/abstract.c _testcapi/unicode.c _testcapi/dict.c _testcapi/set.c _testcapi/list.c _testcapi/tuple.c _testcapi/getargs.c _testcapi/datetime.c _testcapi/docstring.c _testcapi/mem.c _testcapi/watchers.c _testcapi/long.c _testcapi/float.c _testcapi/complex.c _testcapi/numbers.c _testcapi/structmember.c _testcapi/exceptions.c _testcapi/code.c _testcapi/buffer.c _testcapi/pyatomic.c _testcapi/file.c _testcapi/codec.c _testcapi/immortal.c _testcapi/gc.c _testcapi/hash.c _testcapi/time.c +@MODULE__TESTLIMITEDCAPI_TRUE@_testlimitedcapi _testlimitedcapi.c _testlimitedcapi/bytearray.c _testlimitedcapi/bytes.c _testlimitedcapi/heaptype_relative.c _testlimitedcapi/pyos.c _testlimitedcapi/sys.c _testlimitedcapi/vectorcall_limited.c @MODULE__TESTCLINIC_TRUE@_testclinic _testclinic.c @MODULE__TESTCLINIC_LIMITED_TRUE@_testclinic_limited _testclinic_limited.c diff --git a/Modules/_testcapi/bytearray.c b/Modules/_testcapi/bytearray.c deleted file mode 100644 index dc47ed2..0000000 --- a/Modules/_testcapi/bytearray.c +++ /dev/null @@ -1,123 +0,0 @@ -#include "parts.h" -#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}, -}; - -int -_PyTestCapi_Init_ByteArray(PyObject *m) -{ - if (PyModule_AddFunctions(m, test_methods) < 0) { - return -1; - } - - return 0; -} diff --git a/Modules/_testcapi/bytes.c b/Modules/_testcapi/bytes.c deleted file mode 100644 index da10503..0000000 --- a/Modules/_testcapi/bytes.c +++ /dev/null @@ -1,255 +0,0 @@ -#include "parts.h" -#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}, -}; - -int -_PyTestCapi_Init_Bytes(PyObject *m) -{ - if (PyModule_AddFunctions(m, test_methods) < 0) { - return -1; - } - - return 0; -} diff --git a/Modules/_testcapi/parts.h b/Modules/_testcapi/parts.h index 2a043cf..f9bdd83 100644 --- a/Modules/_testcapi/parts.h +++ b/Modules/_testcapi/parts.h @@ -31,8 +31,6 @@ int _PyTestCapi_Init_Vectorcall(PyObject *module); int _PyTestCapi_Init_Heaptype(PyObject *module); int _PyTestCapi_Init_Abstract(PyObject *module); -int _PyTestCapi_Init_ByteArray(PyObject *module); -int _PyTestCapi_Init_Bytes(PyObject *module); int _PyTestCapi_Init_Unicode(PyObject *module); int _PyTestCapi_Init_GetArgs(PyObject *module); int _PyTestCapi_Init_DateTime(PyObject *module); @@ -52,12 +50,10 @@ int _PyTestCapi_Init_Exceptions(PyObject *module); int _PyTestCapi_Init_Code(PyObject *module); int _PyTestCapi_Init_Buffer(PyObject *module); int _PyTestCapi_Init_PyAtomic(PyObject *module); -int _PyTestCapi_Init_PyOS(PyObject *module); int _PyTestCapi_Init_File(PyObject *module); int _PyTestCapi_Init_Codec(PyObject *module); int _PyTestCapi_Init_Immortal(PyObject *module); int _PyTestCapi_Init_GC(PyObject *module); -int _PyTestCapi_Init_Sys(PyObject *module); int _PyTestCapi_Init_Hash(PyObject *module); int _PyTestCapi_Init_Time(PyObject *module); diff --git a/Modules/_testcapi/pyos.c b/Modules/_testcapi/pyos.c deleted file mode 100644 index 63140e9..0000000 --- a/Modules/_testcapi/pyos.c +++ /dev/null @@ -1,60 +0,0 @@ -#include "parts.h" - - -static PyObject * -test_PyOS_mystrnicmp(PyObject *self, PyObject *Py_UNUSED(ignored)) -{ - assert(PyOS_mystrnicmp("", "", 0) == 0); - assert(PyOS_mystrnicmp("", "", 1) == 0); - - assert(PyOS_mystrnicmp("insert", "ins", 3) == 0); - assert(PyOS_mystrnicmp("ins", "insert", 3) == 0); - assert(PyOS_mystrnicmp("insect", "insert", 3) == 0); - - assert(PyOS_mystrnicmp("insert", "insert", 6) == 0); - assert(PyOS_mystrnicmp("Insert", "insert", 6) == 0); - assert(PyOS_mystrnicmp("INSERT", "insert", 6) == 0); - assert(PyOS_mystrnicmp("insert", "insert", 10) == 0); - - assert(PyOS_mystrnicmp("invert", "insert", 6) == ('v' - 's')); - assert(PyOS_mystrnicmp("insert", "invert", 6) == ('s' - 'v')); - assert(PyOS_mystrnicmp("insert", "ins\0rt", 6) == 'e'); - - // GH-21845 - assert(PyOS_mystrnicmp("insert\0a", "insert\0b", 8) == 0); - - Py_RETURN_NONE; -} - -static PyObject * -test_PyOS_mystricmp(PyObject *self, PyObject *Py_UNUSED(ignored)) -{ - assert(PyOS_mystricmp("", "") == 0); - assert(PyOS_mystricmp("insert", "insert") == 0); - assert(PyOS_mystricmp("Insert", "insert") == 0); - assert(PyOS_mystricmp("INSERT", "insert") == 0); - assert(PyOS_mystricmp("insert", "ins") == 'e'); - assert(PyOS_mystricmp("ins", "insert") == -'e'); - - // GH-21845 - assert(PyOS_mystricmp("insert", "ins\0rt") == 'e'); - assert(PyOS_mystricmp("invert", "insert") == ('v' - 's')); - - Py_RETURN_NONE; -} - -static PyMethodDef test_methods[] = { - {"test_PyOS_mystrnicmp", test_PyOS_mystrnicmp, METH_NOARGS, NULL}, - {"test_PyOS_mystricmp", test_PyOS_mystricmp, METH_NOARGS, NULL}, - {NULL}, -}; - -int -_PyTestCapi_Init_PyOS(PyObject *mod) -{ - if (PyModule_AddFunctions(mod, test_methods) < 0) { - return -1; - } - - return 0; -} diff --git a/Modules/_testcapi/sys.c b/Modules/_testcapi/sys.c deleted file mode 100644 index aa40e3c..0000000 --- a/Modules/_testcapi/sys.c +++ /dev/null @@ -1,56 +0,0 @@ -#include "parts.h" -#include "util.h" - - -static PyObject * -sys_getobject(PyObject *Py_UNUSED(module), PyObject *arg) -{ - const char *name; - Py_ssize_t size; - if (!PyArg_Parse(arg, "z#", &name, &size)) { - return NULL; - } - PyObject *result = PySys_GetObject(name); - if (result == NULL) { - result = PyExc_AttributeError; - } - return Py_NewRef(result); -} - -static PyObject * -sys_setobject(PyObject *Py_UNUSED(module), PyObject *args) -{ - const char *name; - Py_ssize_t size; - PyObject *value; - if (!PyArg_ParseTuple(args, "z#O", &name, &size, &value)) { - return NULL; - } - NULLABLE(value); - RETURN_INT(PySys_SetObject(name, value)); -} - -static PyObject * -sys_getxoptions(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(ignored)) -{ - PyObject *result = PySys_GetXOptions(); - return Py_XNewRef(result); -} - - -static PyMethodDef test_methods[] = { - {"sys_getobject", sys_getobject, METH_O}, - {"sys_setobject", sys_setobject, METH_VARARGS}, - {"sys_getxoptions", sys_getxoptions, METH_NOARGS}, - {NULL}, -}; - -int -_PyTestCapi_Init_Sys(PyObject *m) -{ - if (PyModule_AddFunctions(m, test_methods) < 0) { - return -1; - } - - return 0; -} diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index b653604..b5e646f 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -4017,12 +4017,6 @@ PyInit__testcapi(void) if (_PyTestCapi_Init_Abstract(m) < 0) { return NULL; } - if (_PyTestCapi_Init_ByteArray(m) < 0) { - return NULL; - } - if (_PyTestCapi_Init_Bytes(m) < 0) { - return NULL; - } if (_PyTestCapi_Init_Unicode(m) < 0) { return NULL; } @@ -4077,18 +4071,12 @@ PyInit__testcapi(void) if (_PyTestCapi_Init_Buffer(m) < 0) { return NULL; } - if (_PyTestCapi_Init_PyOS(m) < 0) { - return NULL; - } if (_PyTestCapi_Init_File(m) < 0) { return NULL; } if (_PyTestCapi_Init_Codec(m) < 0) { return NULL; } - if (_PyTestCapi_Init_Sys(m) < 0) { - return NULL; - } if (_PyTestCapi_Init_Immortal(m) < 0) { return NULL; } diff --git a/Modules/_testlimitedcapi.c b/Modules/_testlimitedcapi.c index da09e3f..49bf6a3 100644 --- a/Modules/_testlimitedcapi.c +++ b/Modules/_testlimitedcapi.c @@ -26,11 +26,23 @@ PyInit__testlimitedcapi(void) return NULL; } - if (_PyTestCapi_Init_VectorcallLimited(mod) < 0) { + if (_PyTestCapi_Init_ByteArray(mod) < 0) { + return NULL; + } + if (_PyTestCapi_Init_Bytes(mod) < 0) { return NULL; } if (_PyTestCapi_Init_HeaptypeRelative(mod) < 0) { return NULL; } + if (_PyTestCapi_Init_PyOS(mod) < 0) { + return NULL; + } + if (_PyTestCapi_Init_Sys(mod) < 0) { + return NULL; + } + if (_PyTestCapi_Init_VectorcallLimited(mod) < 0) { + return NULL; + } return mod; } diff --git a/Modules/_testlimitedcapi/bytearray.c b/Modules/_testlimitedcapi/bytearray.c new file mode 100644 index 0000000..dc47ed2 --- /dev/null +++ b/Modules/_testlimitedcapi/bytearray.c @@ -0,0 +1,123 @@ +#include "parts.h" +#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}, +}; + +int +_PyTestCapi_Init_ByteArray(PyObject *m) +{ + if (PyModule_AddFunctions(m, test_methods) < 0) { + return -1; + } + + return 0; +} diff --git a/Modules/_testlimitedcapi/bytes.c b/Modules/_testlimitedcapi/bytes.c new file mode 100644 index 0000000..a14c4f9 --- /dev/null +++ b/Modules/_testlimitedcapi/bytes.c @@ -0,0 +1,255 @@ +#include "parts.h" +#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_AsString(left), + PyBytes_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_AsString(left), + PyBytes_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}, +}; + +int +_PyTestCapi_Init_Bytes(PyObject *m) +{ + if (PyModule_AddFunctions(m, test_methods) < 0) { + return -1; + } + + return 0; +} diff --git a/Modules/_testlimitedcapi/parts.h b/Modules/_testlimitedcapi/parts.h index 83590a7..039576d 100644 --- a/Modules/_testlimitedcapi/parts.h +++ b/Modules/_testlimitedcapi/parts.h @@ -21,7 +21,11 @@ # error "Py_BUILD_CORE macro must not be defined" #endif -int _PyTestCapi_Init_VectorcallLimited(PyObject *module); +int _PyTestCapi_Init_ByteArray(PyObject *module); +int _PyTestCapi_Init_Bytes(PyObject *module); int _PyTestCapi_Init_HeaptypeRelative(PyObject *module); +int _PyTestCapi_Init_PyOS(PyObject *module); +int _PyTestCapi_Init_Sys(PyObject *module); +int _PyTestCapi_Init_VectorcallLimited(PyObject *module); #endif // Py_TESTLIMITEDCAPI_PARTS_H diff --git a/Modules/_testlimitedcapi/pyos.c b/Modules/_testlimitedcapi/pyos.c new file mode 100644 index 0000000..63140e9 --- /dev/null +++ b/Modules/_testlimitedcapi/pyos.c @@ -0,0 +1,60 @@ +#include "parts.h" + + +static PyObject * +test_PyOS_mystrnicmp(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + assert(PyOS_mystrnicmp("", "", 0) == 0); + assert(PyOS_mystrnicmp("", "", 1) == 0); + + assert(PyOS_mystrnicmp("insert", "ins", 3) == 0); + assert(PyOS_mystrnicmp("ins", "insert", 3) == 0); + assert(PyOS_mystrnicmp("insect", "insert", 3) == 0); + + assert(PyOS_mystrnicmp("insert", "insert", 6) == 0); + assert(PyOS_mystrnicmp("Insert", "insert", 6) == 0); + assert(PyOS_mystrnicmp("INSERT", "insert", 6) == 0); + assert(PyOS_mystrnicmp("insert", "insert", 10) == 0); + + assert(PyOS_mystrnicmp("invert", "insert", 6) == ('v' - 's')); + assert(PyOS_mystrnicmp("insert", "invert", 6) == ('s' - 'v')); + assert(PyOS_mystrnicmp("insert", "ins\0rt", 6) == 'e'); + + // GH-21845 + assert(PyOS_mystrnicmp("insert\0a", "insert\0b", 8) == 0); + + Py_RETURN_NONE; +} + +static PyObject * +test_PyOS_mystricmp(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + assert(PyOS_mystricmp("", "") == 0); + assert(PyOS_mystricmp("insert", "insert") == 0); + assert(PyOS_mystricmp("Insert", "insert") == 0); + assert(PyOS_mystricmp("INSERT", "insert") == 0); + assert(PyOS_mystricmp("insert", "ins") == 'e'); + assert(PyOS_mystricmp("ins", "insert") == -'e'); + + // GH-21845 + assert(PyOS_mystricmp("insert", "ins\0rt") == 'e'); + assert(PyOS_mystricmp("invert", "insert") == ('v' - 's')); + + Py_RETURN_NONE; +} + +static PyMethodDef test_methods[] = { + {"test_PyOS_mystrnicmp", test_PyOS_mystrnicmp, METH_NOARGS, NULL}, + {"test_PyOS_mystricmp", test_PyOS_mystricmp, METH_NOARGS, NULL}, + {NULL}, +}; + +int +_PyTestCapi_Init_PyOS(PyObject *mod) +{ + if (PyModule_AddFunctions(mod, test_methods) < 0) { + return -1; + } + + return 0; +} diff --git a/Modules/_testlimitedcapi/sys.c b/Modules/_testlimitedcapi/sys.c new file mode 100644 index 0000000..aa40e3c --- /dev/null +++ b/Modules/_testlimitedcapi/sys.c @@ -0,0 +1,56 @@ +#include "parts.h" +#include "util.h" + + +static PyObject * +sys_getobject(PyObject *Py_UNUSED(module), PyObject *arg) +{ + const char *name; + Py_ssize_t size; + if (!PyArg_Parse(arg, "z#", &name, &size)) { + return NULL; + } + PyObject *result = PySys_GetObject(name); + if (result == NULL) { + result = PyExc_AttributeError; + } + return Py_NewRef(result); +} + +static PyObject * +sys_setobject(PyObject *Py_UNUSED(module), PyObject *args) +{ + const char *name; + Py_ssize_t size; + PyObject *value; + if (!PyArg_ParseTuple(args, "z#O", &name, &size, &value)) { + return NULL; + } + NULLABLE(value); + RETURN_INT(PySys_SetObject(name, value)); +} + +static PyObject * +sys_getxoptions(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(ignored)) +{ + PyObject *result = PySys_GetXOptions(); + return Py_XNewRef(result); +} + + +static PyMethodDef test_methods[] = { + {"sys_getobject", sys_getobject, METH_O}, + {"sys_setobject", sys_setobject, METH_VARARGS}, + {"sys_getxoptions", sys_getxoptions, METH_NOARGS}, + {NULL}, +}; + +int +_PyTestCapi_Init_Sys(PyObject *m) +{ + if (PyModule_AddFunctions(m, test_methods) < 0) { + return -1; + } + + return 0; +} diff --git a/Modules/_testlimitedcapi/util.h b/Modules/_testlimitedcapi/util.h new file mode 100644 index 0000000..f26d765 --- /dev/null +++ b/Modules/_testlimitedcapi/util.h @@ -0,0 +1,33 @@ +#define NULLABLE(x) do { \ + if (x == Py_None) { \ + x = NULL; \ + } \ + } while (0); + +#define RETURN_INT(value) do { \ + int _ret = (value); \ + if (_ret == -1) { \ + assert(PyErr_Occurred()); \ + return NULL; \ + } \ + assert(!PyErr_Occurred()); \ + return PyLong_FromLong(_ret); \ + } while (0) + +#define RETURN_SIZE(value) do { \ + Py_ssize_t _ret = (value); \ + if (_ret == -1) { \ + assert(PyErr_Occurred()); \ + return NULL; \ + } \ + assert(!PyErr_Occurred()); \ + return PyLong_FromSsize_t(_ret); \ + } while (0) + +/* Marker to check that pointer value was set. */ +static const char uninitialized[] = "uninitialized"; +#define UNINITIALIZED_PTR ((void *)uninitialized) +/* Marker to check that Py_ssize_t value was set. */ +#define UNINITIALIZED_SIZE ((Py_ssize_t)236892191) +/* Marker to check that integer value was set. */ +#define UNINITIALIZED_INT (63256717) diff --git a/PCbuild/_testcapi.vcxproj b/PCbuild/_testcapi.vcxproj index 3ca4c5f..6522cb1 100644 --- a/PCbuild/_testcapi.vcxproj +++ b/PCbuild/_testcapi.vcxproj @@ -98,8 +98,6 @@ - - @@ -118,10 +116,8 @@ - - diff --git a/PCbuild/_testcapi.vcxproj.filters b/PCbuild/_testcapi.vcxproj.filters index 651eb1d..772a9a8 100644 --- a/PCbuild/_testcapi.vcxproj.filters +++ b/PCbuild/_testcapi.vcxproj.filters @@ -30,12 +30,6 @@ Source Files - - Source Files - - - Source Files - Source Files @@ -90,18 +84,12 @@ Source Files - - Source Files - Source Files Source Files - - Source Files - Source Files diff --git a/PCbuild/_testlimitedcapi.vcxproj b/PCbuild/_testlimitedcapi.vcxproj index 1b27942..1afeaca 100644 --- a/PCbuild/_testlimitedcapi.vcxproj +++ b/PCbuild/_testlimitedcapi.vcxproj @@ -94,8 +94,12 @@ - + + + + + @@ -113,4 +117,4 @@ - \ No newline at end of file + diff --git a/PCbuild/_testlimitedcapi.vcxproj.filters b/PCbuild/_testlimitedcapi.vcxproj.filters index c4764a5..b3eeb86 100644 --- a/PCbuild/_testlimitedcapi.vcxproj.filters +++ b/PCbuild/_testlimitedcapi.vcxproj.filters @@ -9,12 +9,12 @@ - - Source Files - - - Source Files - + + + + + + @@ -22,4 +22,4 @@ Resource Files - \ No newline at end of file + -- cgit v0.12