2 A testcase which accesses *values* in a dll.
10 class ValuesTestCase(unittest
.TestCase
):
12 def test_an_integer(self
):
13 ctdll
= CDLL(_ctypes_test
.__file
__)
14 an_integer
= c_int
.in_dll(ctdll
, "an_integer")
16 self
.assertEqual(x
, ctdll
.get_an_integer())
18 self
.assertEqual(x
*2, ctdll
.get_an_integer())
20 def test_undefined(self
):
21 ctdll
= CDLL(_ctypes_test
.__file
__)
22 self
.assertRaises(ValueError, c_int
.in_dll
, ctdll
, "Undefined_Symbol")
24 class Win_ValuesTestCase(unittest
.TestCase
):
25 """This test only works when python itself is a dll/shared library"""
27 def test_optimizeflag(self
):
28 # This test accesses the Py_OptimizeFlag intger, which is
29 # exported by the Python dll.
31 # It's value is set depending on the -O and -OO flags:
32 # if not given, it is 0 and __debug__ is 1.
33 # If -O is given, the flag is 1, for -OO it is 2.
34 # docstrings are also removed in the latter case.
35 opt
= c_int
.in_dll(pydll
, "Py_OptimizeFlag").value
37 self
.assertEqual(opt
, 0)
38 elif ValuesTestCase
.__doc
__ is not None:
39 self
.assertEqual(opt
, 1)
41 self
.assertEqual(opt
, 2)
43 def test_frozentable(self
):
44 # Python exports a PyImport_FrozenModules symbol. This is a
45 # pointer to an array of struct _frozen entries. The end of the
46 # array is marked by an entry containing a NULL name and zero
49 # In standard Python, this table contains a __hello__
50 # module, and a __phello__ package containing a spam
52 class struct_frozen(Structure
):
53 _fields_
= [("name", c_char_p
),
54 ("code", POINTER(c_ubyte
)),
56 FrozenTable
= POINTER(struct_frozen
)
58 ft
= FrozenTable
.in_dll(pydll
, "PyImport_FrozenModules")
59 # ft is a pointer to the struct_frozen entries:
62 # This is dangerous. We *can* iterate over a pointer, but
63 # the loop will not terminate (maybe with an access
64 # violation;-) because the pointer instance has no size.
65 if entry
.name
is None:
67 items
.append((entry
.name
, entry
.size
))
69 if sys
.version_info
[:2] >= (2, 3):
70 expected
= [("__hello__", 104), ("__phello__", -104), ("__phello__.spam", 104)]
72 expected
= [("__hello__", 100), ("__phello__", -100), ("__phello__.spam", 100)]
73 self
.assertEqual(items
, expected
)
75 from ctypes
import _pointer_type_cache
76 del _pointer_type_cache
[struct_frozen
]
78 def test_undefined(self
):
79 self
.assertRaises(ValueError, c_int
.in_dll
, pydll
, "Undefined_Symbol")
81 if __name__
== '__main__':