Debian Codegraph @

tests/brain/test_ctypes.py

raw
1 # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
2 # For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE
3 # Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt
4
5 import sys
6
7 import pytest
8
9 from astroid import extract_node, nodes, util
10
11 pytestmark = pytest.mark.skipif(
12 hasattr(sys, "pypy_version_info"),
13 reason="pypy has its own implementation of _ctypes module which is different "
14 "from the one of cpython",
15 )
16
17
18 # The parameters of the test define a mapping between the ctypes redefined types
19 # and the builtin types that the "value" member holds
20 @pytest.mark.parametrize(
21 "c_type,builtin_type,type_code",
22 [
23 ("c_bool", "bool", "?"),
24 ("c_byte", "int", "b"),
25 ("c_char", "bytes", "c"),
26 ("c_double", "float", "d"),
27 pytest.param(
28 "c_buffer",
29 "bytes",
30 "<class 'ctypes.c_char'>",
31 marks=pytest.mark.xfail(
32 reason="c_buffer is Uninferable but for now we do not know why"
33 ),
34 ),
35 ("c_float", "float", "f"),
36 ("c_int", "int", "i"),
37 ("c_int16", "int", "h"),
38 ("c_int32", "int", "i"),
39 ("c_int64", "int", "l"),
40 ("c_int8", "int", "b"),
41 ("c_long", "int", "l"),
42 ("c_longdouble", "float", "g"),
43 ("c_longlong", "int", "l"),
44 ("c_short", "int", "h"),
45 ("c_size_t", "int", "L"),
46 ("c_ssize_t", "int", "l"),
47 ("c_ubyte", "int", "B"),
48 ("c_uint", "int", "I"),
49 ("c_uint16", "int", "H"),
50 ("c_uint32", "int", "I"),
51 ("c_uint64", "int", "L"),
52 ("c_uint8", "int", "B"),
53 ("c_ulong", "int", "L"),
54 ("c_ulonglong", "int", "L"),
55 ("c_ushort", "int", "H"),
56 ("c_wchar", "str", "u"),
57 ],
58 )
59 def test_ctypes_redefined_types_members(c_type, builtin_type, type_code):
60 """Test that the "value" and "_type_" member of each redefined types are
61 correct.
62 """
63 src = f"""
64 import ctypes
65 x=ctypes.{c_type}("toto")
66 x.value
67 """
68 node = extract_node(src)
69 assert isinstance(node, nodes.NodeNG)
70 node_inf = node.inferred()[0]
71 assert node_inf.pytype() == f"builtins.{builtin_type}"
72
73 src = f"""
74 import ctypes
75 x=ctypes.{c_type}("toto")
76 x._type_
77 """
78 node = extract_node(src)
79 assert isinstance(node, nodes.NodeNG)
80 node_inf = node.inferred()[0]
81 assert isinstance(node_inf, nodes.Const)
82 assert node_inf.value == type_code
83
84
85 def test_cdata_member_access() -> None:
86 """
87 Test that the base members are still accessible. Each redefined ctypes type
88 inherits from _SimpleCData which itself inherits from _CData. Checks that
89 _CData members are accessible.
90 """
91 src = """
92 import ctypes
93 x=ctypes.c_float(1.0)
94 x._objects
95 """
96 node = extract_node(src)
97 assert isinstance(node, nodes.NodeNG)
98 # ``_objects`` is a data descriptor, so the attribute resolves but the value
99 # it returns is not statically known.
100 assert node.inferred() == [util.Uninferable]
101
102
103 def test_other_ctypes_member_untouched() -> None:
104 """
105 Test that other ctypes members, which are not touched by the brain, are correctly
106 inferred.
107 """
108 src = """
109 import ctypes
110 ctypes.ARRAY(3, 2)
111 """
112 node = extract_node(src)
113 assert isinstance(node, nodes.NodeNG)
114 node_inf = node.inferred()[0]
115 assert isinstance(node_inf, nodes.Const)
116 assert node_inf.value == 6