Skip to content

Commit 1cc02ca

Browse files
authored
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.
1 parent d8712fa commit 1cc02ca

17 files changed

+101
-80
lines changed

Lib/test/test_capi/test_bytearray.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import unittest
22
from test.support import import_helper
33

4-
_testcapi = import_helper.import_module('_testcapi')
4+
_testlimitedcapi = import_helper.import_module('_testlimitedcapi')
55
from _testcapi import PY_SSIZE_T_MIN, PY_SSIZE_T_MAX
66

77
NULL = None
@@ -19,7 +19,7 @@ def __bytes__(self):
1919
class CAPITest(unittest.TestCase):
2020
def test_check(self):
2121
# Test PyByteArray_Check()
22-
check = _testcapi.bytearray_check
22+
check = _testlimitedcapi.bytearray_check
2323
self.assertTrue(check(bytearray(b'abc')))
2424
self.assertFalse(check(b'abc'))
2525
self.assertTrue(check(ByteArraySubclass(b'abc')))
@@ -32,7 +32,7 @@ def test_check(self):
3232

3333
def test_checkexact(self):
3434
# Test PyByteArray_CheckExact()
35-
check = _testcapi.bytearray_checkexact
35+
check = _testlimitedcapi.bytearray_checkexact
3636
self.assertTrue(check(bytearray(b'abc')))
3737
self.assertFalse(check(b'abc'))
3838
self.assertFalse(check(ByteArraySubclass(b'abc')))
@@ -45,7 +45,7 @@ def test_checkexact(self):
4545

4646
def test_fromstringandsize(self):
4747
# Test PyByteArray_FromStringAndSize()
48-
fromstringandsize = _testcapi.bytearray_fromstringandsize
48+
fromstringandsize = _testlimitedcapi.bytearray_fromstringandsize
4949

5050
self.assertEqual(fromstringandsize(b'abc'), bytearray(b'abc'))
5151
self.assertEqual(fromstringandsize(b'abc', 2), bytearray(b'ab'))
@@ -62,7 +62,7 @@ def test_fromstringandsize(self):
6262

6363
def test_fromobject(self):
6464
# Test PyByteArray_FromObject()
65-
fromobject = _testcapi.bytearray_fromobject
65+
fromobject = _testlimitedcapi.bytearray_fromobject
6666

6767
self.assertEqual(fromobject(b'abc'), bytearray(b'abc'))
6868
self.assertEqual(fromobject(bytearray(b'abc')), bytearray(b'abc'))
@@ -77,7 +77,7 @@ def test_fromobject(self):
7777

7878
def test_size(self):
7979
# Test PyByteArray_Size()
80-
size = _testcapi.bytearray_size
80+
size = _testlimitedcapi.bytearray_size
8181

8282
self.assertEqual(size(bytearray(b'abc')), 3)
8383
self.assertEqual(size(ByteArraySubclass(b'abc')), 3)
@@ -88,7 +88,7 @@ def test_size(self):
8888

8989
def test_asstring(self):
9090
"""Test PyByteArray_AsString()"""
91-
asstring = _testcapi.bytearray_asstring
91+
asstring = _testlimitedcapi.bytearray_asstring
9292

9393
self.assertEqual(asstring(bytearray(b'abc'), 4), b'abc\0')
9494
self.assertEqual(asstring(ByteArraySubclass(b'abc'), 4), b'abc\0')
@@ -100,7 +100,7 @@ def test_asstring(self):
100100

101101
def test_concat(self):
102102
"""Test PyByteArray_Concat()"""
103-
concat = _testcapi.bytearray_concat
103+
concat = _testlimitedcapi.bytearray_concat
104104

105105
ba = bytearray(b'abc')
106106
self.assertEqual(concat(ba, b'def'), bytearray(b'abcdef'))
@@ -133,7 +133,7 @@ def test_concat(self):
133133

134134
def test_resize(self):
135135
"""Test PyByteArray_Resize()"""
136-
resize = _testcapi.bytearray_resize
136+
resize = _testlimitedcapi.bytearray_resize
137137

138138
ba = bytearray(b'abcdef')
139139
self.assertEqual(resize(ba, 3), 0)

Lib/test/test_capi/test_bytes.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import unittest
22
from test.support import import_helper
33

4-
_testcapi = import_helper.import_module('_testcapi')
4+
_testlimitedcapi = import_helper.import_module('_testlimitedcapi')
55
from _testcapi import PY_SSIZE_T_MIN, PY_SSIZE_T_MAX
66

77
NULL = None
@@ -19,7 +19,7 @@ def __bytes__(self):
1919
class CAPITest(unittest.TestCase):
2020
def test_check(self):
2121
# Test PyBytes_Check()
22-
check = _testcapi.bytes_check
22+
check = _testlimitedcapi.bytes_check
2323
self.assertTrue(check(b'abc'))
2424
self.assertFalse(check('abc'))
2525
self.assertFalse(check(bytearray(b'abc')))
@@ -33,7 +33,7 @@ def test_check(self):
3333

3434
def test_checkexact(self):
3535
# Test PyBytes_CheckExact()
36-
check = _testcapi.bytes_checkexact
36+
check = _testlimitedcapi.bytes_checkexact
3737
self.assertTrue(check(b'abc'))
3838
self.assertFalse(check('abc'))
3939
self.assertFalse(check(bytearray(b'abc')))
@@ -47,7 +47,7 @@ def test_checkexact(self):
4747

4848
def test_fromstringandsize(self):
4949
# Test PyBytes_FromStringAndSize()
50-
fromstringandsize = _testcapi.bytes_fromstringandsize
50+
fromstringandsize = _testlimitedcapi.bytes_fromstringandsize
5151

5252
self.assertEqual(fromstringandsize(b'abc'), b'abc')
5353
self.assertEqual(fromstringandsize(b'abc', 2), b'ab')
@@ -65,7 +65,7 @@ def test_fromstringandsize(self):
6565

6666
def test_fromstring(self):
6767
# Test PyBytes_FromString()
68-
fromstring = _testcapi.bytes_fromstring
68+
fromstring = _testlimitedcapi.bytes_fromstring
6969

7070
self.assertEqual(fromstring(b'abc\0def'), b'abc')
7171
self.assertEqual(fromstring(b''), b'')
@@ -74,7 +74,7 @@ def test_fromstring(self):
7474

7575
def test_fromobject(self):
7676
# Test PyBytes_FromObject()
77-
fromobject = _testcapi.bytes_fromobject
77+
fromobject = _testlimitedcapi.bytes_fromobject
7878

7979
self.assertEqual(fromobject(b'abc'), b'abc')
8080
self.assertEqual(fromobject(bytearray(b'abc')), b'abc')
@@ -88,7 +88,7 @@ def test_fromobject(self):
8888

8989
def test_size(self):
9090
# Test PyBytes_Size()
91-
size = _testcapi.bytes_size
91+
size = _testlimitedcapi.bytes_size
9292

9393
self.assertEqual(size(b'abc'), 3)
9494
self.assertEqual(size(BytesSubclass(b'abc')), 3)
@@ -100,7 +100,7 @@ def test_size(self):
100100

101101
def test_asstring(self):
102102
"""Test PyBytes_AsString()"""
103-
asstring = _testcapi.bytes_asstring
103+
asstring = _testlimitedcapi.bytes_asstring
104104

105105
self.assertEqual(asstring(b'abc', 4), b'abc\0')
106106
self.assertEqual(asstring(b'abc\0def', 8), b'abc\0def\0')
@@ -111,8 +111,8 @@ def test_asstring(self):
111111

112112
def test_asstringandsize(self):
113113
"""Test PyBytes_AsStringAndSize()"""
114-
asstringandsize = _testcapi.bytes_asstringandsize
115-
asstringandsize_null = _testcapi.bytes_asstringandsize_null
114+
asstringandsize = _testlimitedcapi.bytes_asstringandsize
115+
asstringandsize_null = _testlimitedcapi.bytes_asstringandsize_null
116116

117117
self.assertEqual(asstringandsize(b'abc', 4), (b'abc\0', 3))
118118
self.assertEqual(asstringandsize(b'abc\0def', 8), (b'abc\0def\0', 7))
@@ -128,7 +128,7 @@ def test_asstringandsize(self):
128128

129129
def test_repr(self):
130130
# Test PyBytes_Repr()
131-
bytes_repr = _testcapi.bytes_repr
131+
bytes_repr = _testlimitedcapi.bytes_repr
132132

133133
self.assertEqual(bytes_repr(b'''abc''', 0), r"""b'abc'""")
134134
self.assertEqual(bytes_repr(b'''abc''', 1), r"""b'abc'""")
@@ -149,7 +149,7 @@ def test_repr(self):
149149
def test_concat(self, concat=None):
150150
"""Test PyBytes_Concat()"""
151151
if concat is None:
152-
concat = _testcapi.bytes_concat
152+
concat = _testlimitedcapi.bytes_concat
153153

154154
self.assertEqual(concat(b'abc', b'def'), b'abcdef')
155155
self.assertEqual(concat(b'a\0b', b'c\0d'), b'a\0bc\0d')
@@ -182,11 +182,11 @@ def test_concat(self, concat=None):
182182

183183
def test_concatanddel(self):
184184
"""Test PyBytes_ConcatAndDel()"""
185-
self.test_concat(_testcapi.bytes_concatanddel)
185+
self.test_concat(_testlimitedcapi.bytes_concatanddel)
186186

187187
def test_decodeescape(self):
188188
"""Test PyBytes_DecodeEscape()"""
189-
decodeescape = _testcapi.bytes_decodeescape
189+
decodeescape = _testlimitedcapi.bytes_decodeescape
190190

191191
self.assertEqual(decodeescape(b'abc'), b'abc')
192192
self.assertEqual(decodeescape(br'\t\n\r\x0b\x0c\x00\\\'\"'),

Lib/test/test_capi/test_sys.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
from test.support import import_helper
66

77
try:
8-
import _testcapi
8+
import _testlimitedcapi
99
except ImportError:
10-
_testcapi = None
10+
_testlimitedcapi = None
1111

1212
NULL = None
1313

@@ -20,10 +20,10 @@ class CAPITest(unittest.TestCase):
2020
maxDiff = None
2121

2222
@support.cpython_only
23-
@unittest.skipIf(_testcapi is None, 'need _testcapi module')
23+
@unittest.skipIf(_testlimitedcapi is None, 'need _testlimitedcapi module')
2424
def test_sys_getobject(self):
2525
# Test PySys_GetObject()
26-
getobject = _testcapi.sys_getobject
26+
getobject = _testlimitedcapi.sys_getobject
2727

2828
self.assertIs(getobject(b'stdout'), sys.stdout)
2929
with support.swap_attr(sys, '\U0001f40d', 42):
@@ -38,10 +38,10 @@ def test_sys_getobject(self):
3838
# CRASHES getobject(NULL)
3939

4040
@support.cpython_only
41-
@unittest.skipIf(_testcapi is None, 'need _testcapi module')
41+
@unittest.skipIf(_testlimitedcapi is None, 'need _testlimitedcapi module')
4242
def test_sys_setobject(self):
4343
# Test PySys_SetObject()
44-
setobject = _testcapi.sys_setobject
44+
setobject = _testlimitedcapi.sys_setobject
4545

4646
value = ['value']
4747
value2 = ['value2']
@@ -70,10 +70,10 @@ def test_sys_setobject(self):
7070
# CRASHES setobject(NULL, value)
7171

7272
@support.cpython_only
73-
@unittest.skipIf(_testcapi is None, 'need _testcapi module')
73+
@unittest.skipIf(_testlimitedcapi is None, 'need _testlimitedcapi module')
7474
def test_sys_getxoptions(self):
7575
# Test PySys_GetXOptions()
76-
getxoptions = _testcapi.sys_getxoptions
76+
getxoptions = _testlimitedcapi.sys_getxoptions
7777

7878
self.assertIs(getxoptions(), sys._xoptions)
7979

Modules/Setup.stdlib.in

+2-2
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@
162162
@MODULE__XXTESTFUZZ_TRUE@_xxtestfuzz _xxtestfuzz/_xxtestfuzz.c _xxtestfuzz/fuzzer.c
163163
@MODULE__TESTBUFFER_TRUE@_testbuffer _testbuffer.c
164164
@MODULE__TESTINTERNALCAPI_TRUE@_testinternalcapi _testinternalcapi.c _testinternalcapi/test_lock.c _testinternalcapi/pytime.c _testinternalcapi/set.c _testinternalcapi/test_critical_sections.c
165-
@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
166-
@MODULE__TESTLIMITEDCAPI_TRUE@_testlimitedcapi _testlimitedcapi.c _testlimitedcapi/vectorcall_limited.c _testlimitedcapi/heaptype_relative.c
165+
@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
166+
@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
167167
@MODULE__TESTCLINIC_TRUE@_testclinic _testclinic.c
168168
@MODULE__TESTCLINIC_LIMITED_TRUE@_testclinic_limited _testclinic_limited.c
169169

Modules/_testcapi/parts.h

-4
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131
int _PyTestCapi_Init_Vectorcall(PyObject *module);
3232
int _PyTestCapi_Init_Heaptype(PyObject *module);
3333
int _PyTestCapi_Init_Abstract(PyObject *module);
34-
int _PyTestCapi_Init_ByteArray(PyObject *module);
35-
int _PyTestCapi_Init_Bytes(PyObject *module);
3634
int _PyTestCapi_Init_Unicode(PyObject *module);
3735
int _PyTestCapi_Init_GetArgs(PyObject *module);
3836
int _PyTestCapi_Init_DateTime(PyObject *module);
@@ -52,12 +50,10 @@ int _PyTestCapi_Init_Exceptions(PyObject *module);
5250
int _PyTestCapi_Init_Code(PyObject *module);
5351
int _PyTestCapi_Init_Buffer(PyObject *module);
5452
int _PyTestCapi_Init_PyAtomic(PyObject *module);
55-
int _PyTestCapi_Init_PyOS(PyObject *module);
5653
int _PyTestCapi_Init_File(PyObject *module);
5754
int _PyTestCapi_Init_Codec(PyObject *module);
5855
int _PyTestCapi_Init_Immortal(PyObject *module);
5956
int _PyTestCapi_Init_GC(PyObject *module);
60-
int _PyTestCapi_Init_Sys(PyObject *module);
6157
int _PyTestCapi_Init_Hash(PyObject *module);
6258
int _PyTestCapi_Init_Time(PyObject *module);
6359

Modules/_testcapimodule.c

-12
Original file line numberDiff line numberDiff line change
@@ -4017,12 +4017,6 @@ PyInit__testcapi(void)
40174017
if (_PyTestCapi_Init_Abstract(m) < 0) {
40184018
return NULL;
40194019
}
4020-
if (_PyTestCapi_Init_ByteArray(m) < 0) {
4021-
return NULL;
4022-
}
4023-
if (_PyTestCapi_Init_Bytes(m) < 0) {
4024-
return NULL;
4025-
}
40264020
if (_PyTestCapi_Init_Unicode(m) < 0) {
40274021
return NULL;
40284022
}
@@ -4077,18 +4071,12 @@ PyInit__testcapi(void)
40774071
if (_PyTestCapi_Init_Buffer(m) < 0) {
40784072
return NULL;
40794073
}
4080-
if (_PyTestCapi_Init_PyOS(m) < 0) {
4081-
return NULL;
4082-
}
40834074
if (_PyTestCapi_Init_File(m) < 0) {
40844075
return NULL;
40854076
}
40864077
if (_PyTestCapi_Init_Codec(m) < 0) {
40874078
return NULL;
40884079
}
4089-
if (_PyTestCapi_Init_Sys(m) < 0) {
4090-
return NULL;
4091-
}
40924080
if (_PyTestCapi_Init_Immortal(m) < 0) {
40934081
return NULL;
40944082
}

Modules/_testlimitedcapi.c

+13-1
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,23 @@ PyInit__testlimitedcapi(void)
2626
return NULL;
2727
}
2828

29-
if (_PyTestCapi_Init_VectorcallLimited(mod) < 0) {
29+
if (_PyTestCapi_Init_ByteArray(mod) < 0) {
30+
return NULL;
31+
}
32+
if (_PyTestCapi_Init_Bytes(mod) < 0) {
3033
return NULL;
3134
}
3235
if (_PyTestCapi_Init_HeaptypeRelative(mod) < 0) {
3336
return NULL;
3437
}
38+
if (_PyTestCapi_Init_PyOS(mod) < 0) {
39+
return NULL;
40+
}
41+
if (_PyTestCapi_Init_Sys(mod) < 0) {
42+
return NULL;
43+
}
44+
if (_PyTestCapi_Init_VectorcallLimited(mod) < 0) {
45+
return NULL;
46+
}
3547
return mod;
3648
}
File renamed without changes.

Modules/_testcapi/bytes.c renamed to Modules/_testlimitedcapi/bytes.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ bytes_concat(PyObject *Py_UNUSED(module), PyObject *args)
160160
if (new) {
161161
assert(left != NULL);
162162
assert(PyBytes_CheckExact(left));
163-
left = PyBytes_FromStringAndSize(PyBytes_AS_STRING(left),
164-
PyBytes_GET_SIZE(left));
163+
left = PyBytes_FromStringAndSize(PyBytes_AsString(left),
164+
PyBytes_Size(left));
165165
if (left == NULL) {
166166
return NULL;
167167
}
@@ -191,8 +191,8 @@ bytes_concatanddel(PyObject *Py_UNUSED(module), PyObject *args)
191191
if (new) {
192192
assert(left != NULL);
193193
assert(PyBytes_CheckExact(left));
194-
left = PyBytes_FromStringAndSize(PyBytes_AS_STRING(left),
195-
PyBytes_GET_SIZE(left));
194+
left = PyBytes_FromStringAndSize(PyBytes_AsString(left),
195+
PyBytes_Size(left));
196196
if (left == NULL) {
197197
return NULL;
198198
}

Modules/_testlimitedcapi/parts.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121
# error "Py_BUILD_CORE macro must not be defined"
2222
#endif
2323

24-
int _PyTestCapi_Init_VectorcallLimited(PyObject *module);
24+
int _PyTestCapi_Init_ByteArray(PyObject *module);
25+
int _PyTestCapi_Init_Bytes(PyObject *module);
2526
int _PyTestCapi_Init_HeaptypeRelative(PyObject *module);
27+
int _PyTestCapi_Init_PyOS(PyObject *module);
28+
int _PyTestCapi_Init_Sys(PyObject *module);
29+
int _PyTestCapi_Init_VectorcallLimited(PyObject *module);
2630

2731
#endif // Py_TESTLIMITEDCAPI_PARTS_H
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)