Add ctypes/test/*.py from CPython 3.11 · RustPython/RustPython@019be59 · GitHub
Skip to content

Commit 019be59

Browse files
committed
Add ctypes/test/*.py from CPython 3.11
1 parent 84caa50 commit 019be59

53 files changed

Lines changed: 7303 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Lib/ctypes/test/__init__.py

Lines changed: 16 additions & 0 deletions

Lib/ctypes/test/__main__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from ctypes.test import load_tests
2+
import unittest
3+
4+
unittest.main()

Lib/ctypes/test/test_anon.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import unittest
2+
import test.support
3+
from ctypes import *
4+
5+
class AnonTest(unittest.TestCase):
6+
7+
def test_anon(self):
8+
class ANON(Union):
9+
_fields_ = [("a", c_int),
10+
("b", c_int)]
11+
12+
class Y(Structure):
13+
_fields_ = [("x", c_int),
14+
("_", ANON),
15+
("y", c_int)]
16+
_anonymous_ = ["_"]
17+
18+
self.assertEqual(Y.a.offset, sizeof(c_int))
19+
self.assertEqual(Y.b.offset, sizeof(c_int))
20+
21+
self.assertEqual(ANON.a.offset, 0)
22+
self.assertEqual(ANON.b.offset, 0)
23+
24+
def test_anon_nonseq(self):
25+
# TypeError: _anonymous_ must be a sequence
26+
self.assertRaises(TypeError,
27+
lambda: type(Structure)("Name",
28+
(Structure,),
29+
{"_fields_": [], "_anonymous_": 42}))
30+
31+
def test_anon_nonmember(self):
32+
# AttributeError: type object 'Name' has no attribute 'x'
33+
self.assertRaises(AttributeError,
34+
lambda: type(Structure)("Name",
35+
(Structure,),
36+
{"_fields_": [],
37+
"_anonymous_": ["x"]}))
38+
39+
@test.support.cpython_only
40+
def test_issue31490(self):
41+
# There shouldn't be an assertion failure in case the class has an
42+
# attribute whose name is specified in _anonymous_ but not in _fields_.
43+
44+
# AttributeError: 'x' is specified in _anonymous_ but not in _fields_
45+
with self.assertRaises(AttributeError):
46+
class Name(Structure):
47+
_fields_ = []
48+
_anonymous_ = ["x"]
49+
x = 42
50+
51+
def test_nested(self):
52+
class ANON_S(Structure):
53+
_fields_ = [("a", c_int)]
54+
55+
class ANON_U(Union):
56+
_fields_ = [("_", ANON_S),
57+
("b", c_int)]
58+
_anonymous_ = ["_"]
59+
60+
class Y(Structure):
61+
_fields_ = [("x", c_int),
62+
("_", ANON_U),
63+
("y", c_int)]
64+
_anonymous_ = ["_"]
65+
66+
self.assertEqual(Y.x.offset, 0)
67+
self.assertEqual(Y.a.offset, sizeof(c_int))
68+
self.assertEqual(Y.b.offset, sizeof(c_int))
69+
self.assertEqual(Y._.offset, sizeof(c_int))
70+
self.assertEqual(Y.y.offset, sizeof(c_int) * 2)
71+
72+
if __name__ == "__main__":
73+
unittest.main()
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import unittest
2+
from ctypes import *
3+
from binascii import hexlify
4+
import re
5+
6+
def dump(obj):
7+
# helper function to dump memory contents in hex, with a hyphen
8+
# between the bytes.
9+
h = hexlify(memoryview(obj)).decode()
10+
return re.sub(r"(..)", r"\1-", h)[:-1]
11+
12+
13+
class Value(Structure):
14+
_fields_ = [("val", c_byte)]
15+
16+
class Container(Structure):
17+
_fields_ = [("pvalues", POINTER(Value))]
18+
19+
class Test(unittest.TestCase):
20+
def test(self):
21+
# create an array of 4 values
22+
val_array = (Value * 4)()
23+
24+
# create a container, which holds a pointer to the pvalues array.
25+
c = Container()
26+
c.pvalues = val_array
27+
28+
# memory contains 4 NUL bytes now, that's correct
29+
self.assertEqual("00-00-00-00", dump(val_array))
30+
31+
# set the values of the array through the pointer:
32+
for i in range(4):
33+
c.pvalues[i].val = i + 1
34+
35+
values = [c.pvalues[i].val for i in range(4)]
36+
37+
# These are the expected results: here s the bug!
38+
self.assertEqual(
39+
(values, dump(val_array)),
40+
([1, 2, 3, 4], "01-02-03-04")
41+
)
42+
43+
def test_2(self):
44+
45+
val_array = (Value * 4)()
46+
47+
# memory contains 4 NUL bytes now, that's correct
48+
self.assertEqual("00-00-00-00", dump(val_array))
49+
50+
ptr = cast(val_array, POINTER(Value))
51+
# set the values of the array through the pointer:
52+
for i in range(4):
53+
ptr[i].val = i + 1
54+
55+
values = [ptr[i].val for i in range(4)]
56+
57+
# These are the expected results: here s the bug!
58+
self.assertEqual(
59+
(values, dump(val_array)),
60+
([1, 2, 3, 4], "01-02-03-04")
61+
)
62+
63+
if __name__ == "__main__":
64+
unittest.main()

Lib/ctypes/test/test_arrays.py

Lines changed: 238 additions & 0 deletions

0 commit comments

Comments
 (0)