| 1 | n/a | """ |
|---|
| 2 | n/a | A testcase which accesses *values* in a dll. |
|---|
| 3 | n/a | """ |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | import unittest |
|---|
| 6 | n/a | import sys |
|---|
| 7 | n/a | from ctypes import * |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | import _ctypes_test |
|---|
| 10 | n/a | |
|---|
| 11 | n/a | class ValuesTestCase(unittest.TestCase): |
|---|
| 12 | n/a | |
|---|
| 13 | n/a | def test_an_integer(self): |
|---|
| 14 | n/a | # This test checks and changes an integer stored inside the |
|---|
| 15 | n/a | # _ctypes_test dll/shared lib. |
|---|
| 16 | n/a | ctdll = CDLL(_ctypes_test.__file__) |
|---|
| 17 | n/a | an_integer = c_int.in_dll(ctdll, "an_integer") |
|---|
| 18 | n/a | x = an_integer.value |
|---|
| 19 | n/a | self.assertEqual(x, ctdll.get_an_integer()) |
|---|
| 20 | n/a | an_integer.value *= 2 |
|---|
| 21 | n/a | self.assertEqual(x*2, ctdll.get_an_integer()) |
|---|
| 22 | n/a | # To avoid test failures when this test is repeated several |
|---|
| 23 | n/a | # times the original value must be restored |
|---|
| 24 | n/a | an_integer.value = x |
|---|
| 25 | n/a | self.assertEqual(x, ctdll.get_an_integer()) |
|---|
| 26 | n/a | |
|---|
| 27 | n/a | def test_undefined(self): |
|---|
| 28 | n/a | ctdll = CDLL(_ctypes_test.__file__) |
|---|
| 29 | n/a | self.assertRaises(ValueError, c_int.in_dll, ctdll, "Undefined_Symbol") |
|---|
| 30 | n/a | |
|---|
| 31 | n/a | class PythonValuesTestCase(unittest.TestCase): |
|---|
| 32 | n/a | """This test only works when python itself is a dll/shared library""" |
|---|
| 33 | n/a | |
|---|
| 34 | n/a | def test_optimizeflag(self): |
|---|
| 35 | n/a | # This test accesses the Py_OptimizeFlag integer, which is |
|---|
| 36 | n/a | # exported by the Python dll and should match the sys.flags value |
|---|
| 37 | n/a | |
|---|
| 38 | n/a | opt = c_int.in_dll(pythonapi, "Py_OptimizeFlag").value |
|---|
| 39 | n/a | self.assertEqual(opt, sys.flags.optimize) |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | def test_frozentable(self): |
|---|
| 42 | n/a | # Python exports a PyImport_FrozenModules symbol. This is a |
|---|
| 43 | n/a | # pointer to an array of struct _frozen entries. The end of the |
|---|
| 44 | n/a | # array is marked by an entry containing a NULL name and zero |
|---|
| 45 | n/a | # size. |
|---|
| 46 | n/a | |
|---|
| 47 | n/a | # In standard Python, this table contains a __hello__ |
|---|
| 48 | n/a | # module, and a __phello__ package containing a spam |
|---|
| 49 | n/a | # module. |
|---|
| 50 | n/a | class struct_frozen(Structure): |
|---|
| 51 | n/a | _fields_ = [("name", c_char_p), |
|---|
| 52 | n/a | ("code", POINTER(c_ubyte)), |
|---|
| 53 | n/a | ("size", c_int)] |
|---|
| 54 | n/a | FrozenTable = POINTER(struct_frozen) |
|---|
| 55 | n/a | |
|---|
| 56 | n/a | ft = FrozenTable.in_dll(pythonapi, "PyImport_FrozenModules") |
|---|
| 57 | n/a | # ft is a pointer to the struct_frozen entries: |
|---|
| 58 | n/a | items = [] |
|---|
| 59 | n/a | # _frozen_importlib changes size whenever importlib._bootstrap |
|---|
| 60 | n/a | # changes, so it gets a special case. We should make sure it's |
|---|
| 61 | n/a | # found, but don't worry about its size too much. The same |
|---|
| 62 | n/a | # applies to _frozen_importlib_external. |
|---|
| 63 | n/a | bootstrap_seen = [] |
|---|
| 64 | n/a | bootstrap_expected = [ |
|---|
| 65 | n/a | b'_frozen_importlib', |
|---|
| 66 | n/a | b'_frozen_importlib_external', |
|---|
| 67 | n/a | ] |
|---|
| 68 | n/a | for entry in ft: |
|---|
| 69 | n/a | # This is dangerous. We *can* iterate over a pointer, but |
|---|
| 70 | n/a | # the loop will not terminate (maybe with an access |
|---|
| 71 | n/a | # violation;-) because the pointer instance has no size. |
|---|
| 72 | n/a | if entry.name is None: |
|---|
| 73 | n/a | break |
|---|
| 74 | n/a | |
|---|
| 75 | n/a | if entry.name in bootstrap_expected: |
|---|
| 76 | n/a | bootstrap_seen.append(entry.name) |
|---|
| 77 | n/a | self.assertTrue(entry.size, |
|---|
| 78 | n/a | "{!r} was reported as having no size".format(entry.name)) |
|---|
| 79 | n/a | continue |
|---|
| 80 | n/a | items.append((entry.name.decode("ascii"), entry.size)) |
|---|
| 81 | n/a | |
|---|
| 82 | n/a | expected = [("__hello__", 139), |
|---|
| 83 | n/a | ("__phello__", -139), |
|---|
| 84 | n/a | ("__phello__.spam", 139), |
|---|
| 85 | n/a | ] |
|---|
| 86 | n/a | self.assertEqual(items, expected, "PyImport_FrozenModules example " |
|---|
| 87 | n/a | "in Doc/library/ctypes.rst may be out of date") |
|---|
| 88 | n/a | |
|---|
| 89 | n/a | self.assertEqual(sorted(bootstrap_seen), bootstrap_expected, |
|---|
| 90 | n/a | "frozen bootstrap modules did not match PyImport_FrozenModules") |
|---|
| 91 | n/a | |
|---|
| 92 | n/a | from ctypes import _pointer_type_cache |
|---|
| 93 | n/a | del _pointer_type_cache[struct_frozen] |
|---|
| 94 | n/a | |
|---|
| 95 | n/a | def test_undefined(self): |
|---|
| 96 | n/a | self.assertRaises(ValueError, c_int.in_dll, pythonapi, |
|---|
| 97 | n/a | "Undefined_Symbol") |
|---|
| 98 | n/a | |
|---|
| 99 | n/a | if __name__ == '__main__': |
|---|
| 100 | n/a | unittest.main() |
|---|