8000 bpo-42111: Make the xxlimited module an example of best extension mod… · python/cpython@c168b50 · GitHub
[go: up one dir, main page]

Skip to content

Commit c168b50

Browse files
authored
bpo-42111: Make the xxlimited module an example of best extension module practices (GH-23226)
- Copy existing xxlimited to xxlimited53 (named for the limited API version it uses) - Build both modules, both in debug and release - Test both modules
1 parent 4aa6785 commit c168b50

File tree

11 files changed

+748
-158
lines changed

11 files changed

+748
-158
lines changed

Lib/test/test_xxlimited.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import unittest
2+
from test.support import import_helper
3+
import types
4+
5+
xxlimited = import_helper.import_module('xxlimited')
6+
xxlimited_35 = import_helper.import_module('xxlimited_35')
7+
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()
17+
with self.assertRaises(AttributeError):
18+
xxo.foo
19+
with self.assertRaises(AttributeError):
20+
del xxo.foo
21+
22+
xxo.foo = 1234
23+
self.assertEqual(xxo.foo, 1234)
24+
25+
del xxo.foo
26+
with self.assertRaises(AttributeError):
27+
xxo.foo
28+
29+
def test_foo(self):
30+
# the foo function adds 2 numbers
31+
self.assertEqual(self.module.foo(1, 2), 3)
32+
33+
def test_str(self):
34+
self.assertTrue(issubclass(self.module.Str, str))
35+
self.assertIsNot(self.module.Str, str)
36+
37+
custom_string = self.module.Str("abcd")
38+
self.assertEqual(custom_string, "abcd")
39+
self.assertEqual(custom_string.upper(), "ABCD")
40+
41+
def test_new(self):
42+
xxo = self.module.new()
43+
self.assertEqual(xxo.demo("abc"), "abc")
44+
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()
52+
self.assertEqual(xxo.demo("abc"), "abc")
53+
self.assertEqual(xxo.demo(xxo), xxo)
54+
self.assertEqual(xxo.demo(other), other)
55+
self.assertEqual(xxo.demo(0), None)
56+
57+
def test_error(self):
58+
with self.assertRaises(self.module.Error):
59+
raise self.module.Error
60+
61+
62+
class TestXXLimited35(CommonTests, unittest.TestCase):
63+
module = xxlimited_35
64+
65+
def test_xxo_demo(self):
66+
xxo = self.module.Xxo()
67+
other = self.module.Xxo()
68+
self.assertEqual(xxo.demo("abc"), "abc")
69+
self.assertEqual(xxo.demo(0), None)
70+
71+
def test_roj(self):
72+
# the roj function always fails
73+
with self.assertRaises(SystemError):
74+
self.module.roj(0)
75+
76+
def test_null(self):
77+
null1 = self.module.Null()
78+
null2 = self.module.Null()
79+
self.assertNotEqual(null1, null2)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Update the ``xxlimited`` module to be a better example of how to use the
2+
limited C API.

0 commit comments

Comments
 (0)
0