| 1 |
""" |
| 2 |
A testcase which accesses *values* in a dll. |
| 3 |
""" |
| 4 |
|
| 5 |
import unittest |
| 6 |
import sys |
| 7 |
from ctypes import * |
| 8 |
|
| 9 |
import _ctypes_test |
| 10 |
|
| 11 |
class ValuesTestCase(unittest.TestCase): |
| 12 |
|
| 13 |
def test_an_integer(self): |
| 14 |
ctdll = CDLL(_ctypes_test.__file__) |
| 15 |
an_integer = c_int.in_dll(ctdll, "an_integer") |
| 16 |
x = an_integer.value |
| 17 |
self.assertEqual(x, ctdll.get_an_integer()) |
| 18 |
an_integer.value *= 2 |
| 19 |
self.assertEqual(x*2, ctdll.get_an_integer()) |
| 20 |
|
| 21 |
def test_undefined(self): |
| 22 |
ctdll = CDLL(_ctypes_test.__file__) |
| 23 |
self.assertRaises(ValueError, c_int.in_dll, ctdll, "Undefined_Symbol") |
| 24 |
|
| 25 |
class PythonValuesTestCase(unittest.TestCase): |
| 26 |
"""This test only works when python itself is a dll/shared library""" |
| 27 |
|
| 28 |
def test_optimizeflag(self): |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
opt = c_int.in_dll(pythonapi, "Py_OptimizeFlag").value |
| 37 |
if __debug__: |
| 38 |
self.assertEqual(opt, 0) |
| 39 |
elif ValuesTestCase.__doc__ is not None: |
| 40 |
self.assertEqual(opt, 1) |
| 41 |
else: |
| 42 |
self.assertEqual(opt, 2) |
| 43 |
|
| 44 |
def test_frozentable(self): |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
class struct_frozen(Structure): |
| 54 |
_fields_ = [("name", c_char_p), |
| 55 |
("code", POINTER(c_ubyte)), |
| 56 |
("size", c_int)] |
| 57 |
FrozenTable = POINTER(struct_frozen) |
| 58 |
|
| 59 |
ft = FrozenTable.in_dll(pythonapi, "PyImport_FrozenModules") |
| 60 |
|
| 61 |
items = [] |
| 62 |
for entry in ft: |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
if entry.name is None: |
| 67 |
break |
| 68 |
items.append((entry.name, entry.size)) |
| 69 |
|
| 70 |
expected = [("__hello__", 104), |
| 71 |
("__phello__", -104), |
| 72 |
("__phello__.spam", 104)] |
| 73 |
self.assertEqual(items, expected) |
| 74 |
|
| 75 |
from ctypes import _pointer_type_cache |
| 76 |
del _pointer_type_cache[struct_frozen] |
| 77 |
|
| 78 |
def test_undefined(self): |
| 79 |
self.assertRaises(ValueError, c_int.in_dll, pythonapi, |
| 80 |
"Undefined_Symbol") |
| 81 |
|
| 82 |
if __name__ == '__main__': |
| 83 |
unittest.main() |