Skip to content

Commit e44b7ca

Browse files
committed
Move tests to _testcapi/, add tests for the new API
1 parent 779d14e commit e44b7ca

File tree

7 files changed

+289
-174
lines changed

7 files changed

+289
-174
lines changed

Lib/test/test_capi/test_structmembers.py

+65-26
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,50 @@
44

55
# Skip this test if the _testcapi module isn't available.
66
import_helper.import_module('_testcapi')
7-
from _testcapi import _test_structmembersType, \
8-
CHAR_MAX, CHAR_MIN, UCHAR_MAX, \
9-
SHRT_MAX, SHRT_MIN, USHRT_MAX, \
10-
INT_MAX, INT_MIN, UINT_MAX, \
11-
LONG_MAX, LONG_MIN, ULONG_MAX, \
12-
LLONG_MAX, LLONG_MIN, ULLONG_MAX, \
13-
PY_SSIZE_T_MAX, PY_SSIZE_T_MIN
14-
15-
ts=_test_structmembersType(False, # T_BOOL
16-
1, # T_BYTE
17-
2, # T_UBYTE
18-
3, # T_SHORT
19-
4, # T_USHORT
20-
5, # T_INT
21-
6, # T_UINT
22-
7, # T_LONG
23-
8, # T_ULONG
24-
23, # T_PYSSIZET
25-
9.99999,# T_FLOAT
26-
10.1010101010, # T_DOUBLE
27-
"hi" # T_STRING_INPLACE
28-
)
29-
30-
class ReadWriteTests(unittest.TestCase):
7+
from _testcapi import (_test_structmembersType_OldAPI,
8+
_test_structmembersType_NewAPI,
9+
CHAR_MAX, CHAR_MIN, UCHAR_MAX,
10+
SHRT_MAX, SHRT_MIN, USHRT_MAX,
11+
INT_MAX, INT_MIN, UINT_MAX,
12+
LONG_MAX, LONG_MIN, ULONG_MAX,
13+
LLONG_MAX, LLONG_MIN, ULLONG_MAX,
14+
PY_SSIZE_T_MAX, PY_SSIZE_T_MIN,
15+
)
16+
17+
# There are two classes: one using <structmember.h> and another using
18+
# `Py_`-prefixed API. They should behave the same in Python
19+
20+
def _make_test_object(cls):
21+
return cls(False, # T_BOOL
22+
1, # T_BYTE
23+
2, # T_UBYTE
24+
3, # T_SHORT
25+
4, # T_USHORT
26+
5, # T_INT
27+
6, # T_UINT
28+
7, # T_LONG
29+
8, # T_ULONG
30+
23, # T_PYSSIZET
31+
9.99999,# T_FLOAT
32+
10.1010101010, # T_DOUBLE
33+
"hi", # T_STRING_INPLACE
34+
)
35+
36+
37+
class ReadWriteTests:
38+
def setUp(self):
39+
self.ts = _make_test_object(self.cls)
3140

3241
def test_bool(self):
42+
ts = self.ts
3343
ts.T_BOOL = True
3444
self.assertEqual(ts.T_BOOL, True)
3545
ts.T_BOOL = False
3646
self.assertEqual(ts.T_BOOL, False)
3747
self.assertRaises(TypeError, setattr, ts, 'T_BOOL', 1)
3848

3949
def test_byte(self):
50+
ts = self.ts
4051
ts.T_BYTE = CHAR_MAX
4152
self.assertEqual(ts.T_BYTE, CHAR_MAX)
4253
ts.T_BYTE = CHAR_MIN
@@ -45,6 +56,7 @@ def test_byte(self):
4556
self.assertEqual(ts.T_UBYTE, UCHAR_MAX)
4657

4758
def test_short(self):
59+
ts = self.ts
4860
ts.T_SHORT = SHRT_MAX
4961
self.assertEqual(ts.T_SHORT, SHRT_MAX)
5062
ts.T_SHORT = SHRT_MIN
@@ -53,6 +65,7 @@ def test_short(self):
5365
self.assertEqual(ts.T_USHORT, USHRT_MAX)
5466

5567
def test_int(self):
68+
ts = self.ts
5669
ts.T_INT = INT_MAX
5770
self.assertEqual(ts.T_INT, INT_MAX)
5871
ts.T_INT = INT_MIN
@@ -61,6 +74,7 @@ def test_int(self):
6174
self.assertEqual(ts.T_UINT, UINT_MAX)
6275

6376
def test_long(self):
77+
ts = self.ts
6478
ts.T_LONG = LONG_MAX
6579
self.assertEqual(ts.T_LONG, LONG_MAX)
6680
ts.T_LONG = LONG_MIN
@@ -69,13 +83,17 @@ def test_long(self):
6983
self.assertEqual(ts.T_ULONG, ULONG_MAX)
7084

7185
def test_py_ssize_t(self):
86+
ts = self.ts
7287
ts.T_PYSSIZET = PY_SSIZE_T_MAX
7388
self.assertEqual(ts.T_PYSSIZET, PY_SSIZE_T_MAX)
7489
ts.T_PYSSIZET = PY_SSIZE_T_MIN
7590
self.assertEqual(ts.T_PYSSIZET, PY_SSIZE_T_MIN)
7691

77-
@unittest.skipUnless(hasattr(ts, "T_LONGLONG"), "long long not present")
7892
def test_longlong(self):
93+
ts = self.ts
94+
if not hasattr(ts, "T_LONGLONG"):
95+
self.skipTest("long long not present")
96+
7997
ts.T_LONGLONG = LLONG_MAX
8098
self.assertEqual(ts.T_LONGLONG, LLONG_MAX)
8199
ts.T_LONGLONG = LLONG_MIN
@@ -91,6 +109,7 @@ def test_longlong(self):
91109
self.assertEqual(ts.T_ULONGLONG, 4)
92110

93111
def test_bad_assignments(self):
112+
ts = self.ts
94113
integer_attributes = [
95114
'T_BOOL',
96115
'T_BYTE', 'T_UBYTE',
@@ -109,37 +128,57 @@ def test_bad_assignments(self):
109128
self.assertRaises(TypeError, setattr, ts, attr, nonint)
110129

111130
def test_inplace_string(self):
131+
ts = self.ts
112132
self.assertEqual(ts.T_STRING_INPLACE, "hi")
113133
self.assertRaises(TypeError, setattr, ts, "T_STRING_INPLACE", "s")
114134
self.assertRaises(TypeError, delattr, ts, "T_STRING_INPLACE")
115135

136+
class ReadWriteTests_OldAPI(ReadWriteTests, unittest.TestCase):
137+
cls = _test_structmembersType_OldAPI
138+
139+
class ReadWriteTests_NewAPI(ReadWriteTests, unittest.TestCase):
140+
cls = _test_structmembersType_NewAPI
116141

117-
class TestWarnings(unittest.TestCase):
142+
class TestWarnings:
143+
def setUp(self):
144+
self.ts = _make_test_object(self.cls)
118145

119146
def test_byte_max(self):
147+
ts = self.ts
120148
with warnings_helper.check_warnings(('', RuntimeWarning)):
121149
ts.T_BYTE = CHAR_MAX+1
122150

123151
def test_byte_min(self):
152+
ts = self.ts
124153
with warnings_helper.check_warnings(('', RuntimeWarning)):
125154
ts.T_BYTE = CHAR_MIN-1
126155

127156
def test_ubyte_max(self):
157+
ts = self.ts
128158
with warnings_helper.check_warnings(('', RuntimeWarning)):
129159
ts.T_UBYTE = UCHAR_MAX+1
130160

131161
def test_short_max(self):
162+
ts = self.ts
132163
with warnings_helper.check_warnings(('', RuntimeWarning)):
133164
ts.T_SHORT = SHRT_MAX+1
134165

135166
def test_short_min(self):
167+
ts = self.ts
136168
with warnings_helper.check_warnings(('', RuntimeWarning)):
137169
ts.T_SHORT = SHRT_MIN-1
138170

139171
def test_ushort_max(self):
172+
ts = self.ts
140173
with warnings_helper.check_warnings(('', RuntimeWarning)):
141174
ts.T_USHORT = USHRT_MAX+1
142175

176+
class TestWarnings_OldAPI(TestWarnings, unittest.TestCase):
177+
cls = _test_structmembersType_OldAPI
178+
179+
class TestWarnings_NewAPI(TestWarnings, unittest.TestCase):
180+
cls = _test_structmembersType_NewAPI
181+
143182

144183
if __name__ == "__main__":
145184
unittest.main()

Modules/Setup.stdlib.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@
169169
@MODULE__XXTESTFUZZ_TRUE@_xxtestfuzz _xxtestfuzz/_xxtestfuzz.c _xxtestfuzz/fuzzer.c
170170
@MODULE__TESTBUFFER_TRUE@_testbuffer _testbuffer.c
171171
@MODULE__TESTINTERNALCAPI_TRUE@_testinternalcapi _testinternalcapi.c
172-
@MODULE__TESTCAPI_TRUE@_testcapi _testcapimodule.c _testcapi/vectorcall.c _testcapi/vectorcall_limited.c _testcapi/heaptype.c _testcapi/unicode.c _testcapi/getargs.c _testcapi/pytime.c _testcapi/datetime.c
172+
@MODULE__TESTCAPI_TRUE@_testcapi _testcapimodule.c _testcapi/vectorcall.c _testcapi/vectorcall_limited.c _testcapi/heaptype.c _testcapi/unicode.c _testcapi/getargs.c _testcapi/pytime.c _testcapi/datetime.c _testcapi/structmember.c
173173

174174
# Some testing modules MUST be built as shared libraries.
175175
*shared*

Modules/_testcapi/parts.h

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ int _PyTestCapi_Init_Unicode(PyObject *module);
3030
int _PyTestCapi_Init_GetArgs(PyObject *module);
3131
int _PyTestCapi_Init_PyTime(PyObject *module);
3232
int _PyTestCapi_Init_DateTime(PyObject *module);
33+
int _PyTestCapi_Init_Structmember(PyObject *module);
3334

3435
#ifdef LIMITED_API_AVAILABLE
3536
int _PyTestCapi_Init_VectorcallLimited(PyObject *module);

0 commit comments

Comments
 (0)