Skip to content

Commit b650305

Browse files
[3.15] gh-140550: Update xxlimited with 3.15 limited API (GH-142827) (GH-149785)
(cherry picked from commit fa81cd9) Co-authored-by: Petr Viktorin <encukou@gmail.com>
1 parent 63a4007 commit b650305

16 files changed

Lines changed: 1062 additions & 164 deletions

File tree

Lib/test/test_xxlimited.py

Lines changed: 76 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,39 @@
11
import unittest
22
from test.support import import_helper
3-
import types
43

54
xxlimited = import_helper.import_module('xxlimited')
6-
xxlimited_35 = import_helper.import_module('xxlimited_35')
75

8-
9-
class CommonTests:
10-
module: types.ModuleType
11-
12-
def test_xxo_new(self):
13-
xxo = self.module.Xxo()
14-
15-
def test_xxo_attributes(self):
16-
xxo = self.module.Xxo()
6+
# if import of xxlimited succeeded, the other ones should be importable.
7+
import xxlimited_3_13
8+
import xxlimited_35
9+
10+
MODULES = {
11+
(3, 15): xxlimited,
12+
(3, 13): xxlimited_3_13,
13+
(3, 5): xxlimited_35,
14+
}
15+
16+
def test_with_xxlimited_modules(since=None, until=None):
17+
def _decorator(func):
18+
def _wrapper(self, *args, **kwargs):
19+
for version, module in MODULES.items():
20+
if since and version < since:
21+
continue
22+
if until and version >= until:
23+
continue
24+
with self.subTest(version=version):
25+
func(self, module, *args, **kwargs)
26+
return _wrapper
27+
return _decorator
28+
29+
class XXLimitedTests(unittest.TestCase):
30+
@test_with_xxlimited_modules()
31+
def test_xxo_new(self, module):
32+
xxo = module.Xxo()
33+
34+
@test_with_xxlimited_modules()
35+
def test_xxo_attributes(self, module):
36+
xxo = module.Xxo()
1737
with self.assertRaises(AttributeError):
1838
xxo.foo
1939
with self.assertRaises(AttributeError):
@@ -26,40 +46,61 @@ def test_xxo_attributes(self):
2646
with self.assertRaises(AttributeError):
2747
xxo.foo
2848

29-
def test_foo(self):
49+
@test_with_xxlimited_modules()
50+
def test_foo(self, module):
3051
# the foo function adds 2 numbers
31-
self.assertEqual(self.module.foo(1, 2), 3)
52+
self.assertEqual(module.foo(1, 2), 3)
3253

33-
def test_str(self):
34-
self.assertIsSubclass(self.module.Str, str)
35-
self.assertIsNot(self.module.Str, str)
54+
@test_with_xxlimited_modules()
55+
def test_str(self, module):
56+
self.assertIsSubclass(module.Str, str)
57+
self.assertIsNot(module.Str, str)
3658

37-
custom_string = self.module.Str("abcd")
59+
custom_string = module.Str("abcd")
3860
self.assertEqual(custom_string, "abcd")
3961
self.assertEqual(custom_string.upper(), "ABCD")
4062

41-
def test_new(self):
42-
xxo = self.module.new()
63+
@test_with_xxlimited_modules()
64+
def test_new(self, module):
65+
xxo = module.new()
4366
self.assertEqual(xxo.demo("abc"), "abc")
4467

45-
46-
class TestXXLimited(CommonTests, unittest.TestCase):
47-
module = xxlimited
48-
49-
def test_xxo_demo(self):
50-
xxo = self.module.Xxo()
51-
other = self.module.Xxo()
68+
@test_with_xxlimited_modules()
69+
def test_xxo_demo(self, module):
70+
xxo = module.Xxo()
5271
self.assertEqual(xxo.demo("abc"), "abc")
72+
self.assertEqual(xxo.demo(0), None)
73+
self.assertEqual(xxo.__module__, module.__name__)
74+
with self.assertRaises(TypeError):
75+
module.Xxo('arg')
76+
with self.assertRaises(TypeError):
77+
module.Xxo(kwarg='arg')
78+
79+
@test_with_xxlimited_modules(since=(3, 13))
80+
def test_xxo_demo_extra(self, module):
81+
xxo = module.Xxo()
82+
other = module.Xxo()
5383
self.assertEqual(xxo.demo(xxo), xxo)
5484
self.assertEqual(xxo.demo(other), other)
55-
self.assertEqual(xxo.demo(0), None)
5685

57-
def test_error(self):
58-
with self.assertRaises(self.module.Error):
59-
raise self.module.Error
60-
61-
def test_buffer(self):
62-
xxo = self.module.Xxo()
86+
@test_with_xxlimited_modules(since=(3, 15))
87+
def test_xxo_subclass(self, module):
88+
class Sub(module.Xxo):
89+
pass
90+
sub = Sub()
91+
sub.a = 123
92+
self.assertEqual(sub.a, 123)
93+
with self.assertRaisesRegex(AttributeError, "cannot set 'reserved'"):
94+
sub.reserved = 123
95+
96+
@test_with_xxlimited_modules(since=(3, 13))
97+
def test_error(self, module):
98+
with self.assertRaises(module.Error):
99+
raise module.Error
100+
101+
@test_with_xxlimited_modules(since=(3, 13))
102+
def test_buffer(self, module):
103+
xxo = module.Xxo()
63104
self.assertEqual(xxo.x_exports, 0)
64105
b1 = memoryview(xxo)
65106
self.assertEqual(xxo.x_exports, 1)
@@ -69,21 +110,13 @@ def test_buffer(self):
69110
self.assertEqual(b1[0], 1)
70111
self.assertEqual(b2[0], 1)
71112

72-
73-
class TestXXLimited35(CommonTests, unittest.TestCase):
74-
module = xxlimited_35
75-
76-
def test_xxo_demo(self):
77-
xxo = self.module.Xxo()
78-
other = self.module.Xxo()
79-
self.assertEqual(xxo.demo("abc"), "abc")
80-
self.assertEqual(xxo.demo(0), None)
81-
113+
@test_with_xxlimited_modules(until=(3, 5))
82114
def test_roj(self):
83115
# the roj function always fails
84116
with self.assertRaises(SystemError):
85117
self.module.roj(0)
86118

119+
@test_with_xxlimited_modules(until=(3, 5))
87120
def test_null(self):
88121
null1 = self.module.Null()
89122
null2 = self.module.Null()

Modules/Setup

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ PYTHONPATH=$(COREPYTHONPATH)
273273
#xx xxmodule.c
274274
#xxlimited xxlimited.c
275275
#xxlimited_35 xxlimited_35.c
276+
#xxlimited_3_13 xxlimited_3_13.c
276277
#xxsubtype xxsubtype.c
277278

278279
# Testing

Modules/Setup.stdlib.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@
190190
# Limited API template modules; must be built as shared modules.
191191
@MODULE_XXLIMITED_TRUE@xxlimited xxlimited.c
192192
@MODULE_XXLIMITED_35_TRUE@xxlimited_35 xxlimited_35.c
193+
@MODULE_XXLIMITED_3_13_TRUE@xxlimited_3_13 xxlimited_3_13.c
193194

194195

195196
# for performance

0 commit comments

Comments
 (0)