| 1 |
""" |
| 2 |
A testcase which accesses *values* in a dll. |
| 3 |
""" |
| 4 |
|
| 5 |
import _imp |
| 6 |
import importlib.util |
| 7 |
import unittest |
| 8 |
import sys |
| 9 |
from ctypes import * |
| 10 |
from test.support import import_helper, cpython_only |
| 11 |
|
| 12 |
import _ctypes_test |
| 13 |
|
| 14 |
class ValuesTestCase(unittest.TestCase): |
| 15 |
|
| 16 |
def test_an_integer(self): |
| 17 |
|
| 18 |
|
| 19 |
ctdll = CDLL(_ctypes_test.__file__) |
| 20 |
an_integer = c_int.in_dll(ctdll, "an_integer") |
| 21 |
x = an_integer.value |
| 22 |
self.assertEqual(x, ctdll.get_an_integer()) |
| 23 |
an_integer.value *= 2 |
| 24 |
self.assertEqual(x*2, ctdll.get_an_integer()) |
| 25 |
|
| 26 |
|
| 27 |
an_integer.value = x |
| 28 |
self.assertEqual(x, ctdll.get_an_integer()) |
| 29 |
|
| 30 |
def test_undefined(self): |
| 31 |
ctdll = CDLL(_ctypes_test.__file__) |
| 32 |
self.assertRaises(ValueError, c_int.in_dll, ctdll, "Undefined_Symbol") |
| 33 |
|
| 34 |
@cpython_only |
| 35 |
class PythonValuesTestCase(unittest.TestCase): |
| 36 |
"""This test only works when python itself is a dll/shared library""" |
| 37 |
|
| 38 |
def test_optimizeflag(self): |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
opt = c_int.in_dll(pythonapi, "Py_OptimizeFlag").value |
| 43 |
self.assertEqual(opt, sys.flags.optimize) |
| 44 |
|
| 45 |
def test_frozentable(self): |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
class struct_frozen(Structure): |
| 55 |
_fields_ = [("name", c_char_p), |
| 56 |
("code", POINTER(c_ubyte)), |
| 57 |
("size", c_int), |
| 58 |
("is_package", c_int), |
| 59 |
("get_code", POINTER(c_ubyte)), |
| 60 |
] |
| 61 |
FrozenTable = POINTER(struct_frozen) |
| 62 |
|
| 63 |
modules = [] |
| 64 |
for group in ["Bootstrap", "Stdlib", "Test"]: |
| 65 |
ft = FrozenTable.in_dll(pythonapi, f"_PyImport_Frozen{group}") |
| 66 |
|
| 67 |
for entry in ft: |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
if entry.name is None: |
| 72 |
break |
| 73 |
modname = entry.name.decode("ascii") |
| 74 |
modules.append(modname) |
| 75 |
with self.subTest(modname): |
| 76 |
if entry.size != 0: |
| 77 |
|
| 78 |
self.assertGreater(abs(entry.size), 10) |
| 79 |
self.assertTrue([entry.code[i] for i in range(abs(entry.size))]) |
| 80 |
|
| 81 |
with import_helper.frozen_modules(): |
| 82 |
spec = importlib.util.find_spec(modname) |
| 83 |
if entry.is_package: |
| 84 |
|
| 85 |
self.assertIsNotNone(spec.submodule_search_locations) |
| 86 |
else: |
| 87 |
self.assertIsNone(spec.submodule_search_locations) |
| 88 |
|
| 89 |
with import_helper.frozen_modules(): |
| 90 |
expected = _imp._frozen_module_names() |
| 91 |
self.maxDiff = None |
| 92 |
self.assertEqual(modules, expected, |
| 93 |
"_PyImport_FrozenBootstrap example " |
| 94 |
"in Doc/library/ctypes.rst may be out of date") |
| 95 |
|
| 96 |
from ctypes import _pointer_type_cache |
| 97 |
del _pointer_type_cache[struct_frozen] |
| 98 |
|
| 99 |
def test_undefined(self): |
| 100 |
self.assertRaises(ValueError, c_int.in_dll, pythonapi, |
| 101 |
"Undefined_Symbol") |
| 102 |
|
| 103 |
if __name__ == '__main__': |
| 104 |
unittest.main() |