| 1 |
""" |
| 2 |
A testcase which accesses *values* in a dll. |
| 3 |
""" |
| 4 |
|
| 5 |
import _imp |
| 6 |
import importlib.util |
| 7 |
import sys |
| 8 |
import unittest |
| 9 |
from ctypes import (Structure, CDLL, POINTER, pythonapi, |
| 10 |
c_ubyte, c_char_p, c_int) |
| 11 |
from test.support import import_helper, thread_unsafe |
| 12 |
|
| 13 |
|
| 14 |
class ValuesTestCase(unittest.TestCase): |
| 15 |
|
| 16 |
def setUp(self): |
| 17 |
_ctypes_test = import_helper.import_module("_ctypes_test") |
| 18 |
self.ctdll = CDLL(_ctypes_test.__file__) |
| 19 |
|
| 20 |
@thread_unsafe("static global variables aren't thread-safe") |
| 21 |
def test_an_integer(self): |
| 22 |
# This test checks and changes an integer stored inside the |
| 23 |
# _ctypes_test dll/shared lib. |
| 24 |
ctdll = self.ctdll |
| 25 |
an_integer = c_int.in_dll(ctdll, "an_integer") |
| 26 |
x = an_integer.value |
| 27 |
self.assertEqual(x, ctdll.get_an_integer()) |
| 28 |
an_integer.value *= 2 |
| 29 |
self.assertEqual(x*2, ctdll.get_an_integer()) |
| 30 |
# To avoid test failures when this test is repeated several |
| 31 |
# times the original value must be restored |
| 32 |
an_integer.value = x |
| 33 |
self.assertEqual(x, ctdll.get_an_integer()) |
| 34 |
|
| 35 |
def test_undefined(self): |
| 36 |
self.assertRaises(ValueError, c_int.in_dll, self.ctdll, "Undefined_Symbol") |
| 37 |
|
| 38 |
|
| 39 |
class PythonValuesTestCase(unittest.TestCase): |
| 40 |
"""This test only works when python itself is a dll/shared library""" |
| 41 |
|
| 42 |
def test_optimizeflag(self): |
| 43 |
# This test accesses the Py_OptimizeFlag integer, which is |
| 44 |
# exported by the Python dll and should match the sys.flags value |
| 45 |
|
| 46 |
opt = c_int.in_dll(pythonapi, "Py_OptimizeFlag").value |
| 47 |
self.assertEqual(opt, sys.flags.optimize) |
| 48 |
|
| 49 |
@thread_unsafe('overrides frozen modules') |
| 50 |
def test_frozentable(self): |
| 51 |
# Python exports a PyImport_FrozenModules symbol. This is a |
| 52 |
# pointer to an array of struct _frozen entries. The end of the |
| 53 |
# array is marked by an entry containing a NULL name and zero |
| 54 |
# size. |
| 55 |
|
| 56 |
# In standard Python, this table contains a __hello__ |
| 57 |
# module, and a __phello__ package containing a spam |
| 58 |
# module. |
| 59 |
class struct_frozen(Structure): |
| 60 |
_fields_ = [("name", c_char_p), |
| 61 |
("code", POINTER(c_ubyte)), |
| 62 |
("size", c_int), |
| 63 |
("is_package", c_int), |
| 64 |
] |
| 65 |
FrozenTable = POINTER(struct_frozen) |
| 66 |
|
| 67 |
modules = [] |
| 68 |
for group in ["Bootstrap", "Stdlib", "Test"]: |
| 69 |
ft = FrozenTable.in_dll(pythonapi, f"_PyImport_Frozen{group}") |
| 70 |
# ft is a pointer to the struct_frozen entries: |
| 71 |
for entry in ft: |
| 72 |
# This is dangerous. We *can* iterate over a pointer, but |
| 73 |
# the loop will not terminate (maybe with an access |
| 74 |
# violation;-) because the pointer instance has no size. |
| 75 |
if entry.name is None: |
| 76 |
break |
| 77 |
modname = entry.name.decode("ascii") |
| 78 |
modules.append(modname) |
| 79 |
with self.subTest(modname): |
| 80 |
if entry.size != 0: |
| 81 |
# Do a sanity check on entry.size and entry.code. |
| 82 |
self.assertGreater(abs(entry.size), 10) |
| 83 |
self.assertTrue([entry.code[i] for i in range(abs(entry.size))]) |
| 84 |
# Check the module's package-ness. |
| 85 |
with import_helper.frozen_modules(): |
| 86 |
spec = importlib.util.find_spec(modname) |
| 87 |
if entry.is_package: |
| 88 |
# It's a package. |
| 89 |
self.assertIsNotNone(spec.submodule_search_locations) |
| 90 |
else: |
| 91 |
self.assertIsNone(spec.submodule_search_locations) |
| 92 |
|
| 93 |
with import_helper.frozen_modules(): |
| 94 |
expected = _imp._frozen_module_names() |
| 95 |
self.maxDiff = None |
| 96 |
self.assertEqual(modules, expected, |
| 97 |
"_PyImport_FrozenBootstrap example " |
| 98 |
"in Doc/library/ctypes.rst may be out of date") |
| 99 |
|
| 100 |
def test_undefined(self): |
| 101 |
self.assertRaises(ValueError, c_int.in_dll, pythonapi, |
| 102 |
"Undefined_Symbol") |
| 103 |
|
| 104 |
|
| 105 |
if __name__ == '__main__': |
| 106 |
unittest.main() |