/Lib/ctypes/test/test_values.py

http://unladen-swallow.googlecode.com/ · Python · 82 lines · 47 code · 14 blank · 21 comment · 8 complexity · 80e185f16dc9257eab13457cd368ebc6 MD5 · raw file

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