/Lib/ctypes/test/test_python_api.py

http://unladen-swallow.googlecode.com/ · Python · 90 lines · 60 code · 22 blank · 8 comment · 4 complexity · 3af7ff251c9906c509d443f009ea05c4 MD5 · raw file

  1. from ctypes import *
  2. import unittest, sys
  3. from ctypes.test import is_resource_enabled
  4. ################################################################
  5. # This section should be moved into ctypes\__init__.py, when it's ready.
  6. from _ctypes import PyObj_FromPtr
  7. ################################################################
  8. from sys import getrefcount as grc
  9. if sys.version_info > (2, 4):
  10. c_py_ssize_t = c_size_t
  11. else:
  12. c_py_ssize_t = c_int
  13. class PythonAPITestCase(unittest.TestCase):
  14. def test_PyString_FromStringAndSize(self):
  15. PyString_FromStringAndSize = pythonapi.PyString_FromStringAndSize
  16. PyString_FromStringAndSize.restype = py_object
  17. PyString_FromStringAndSize.argtypes = c_char_p, c_py_ssize_t
  18. self.failUnlessEqual(PyString_FromStringAndSize("abcdefghi", 3), "abc")
  19. def test_PyString_FromString(self):
  20. pythonapi.PyString_FromString.restype = py_object
  21. pythonapi.PyString_FromString.argtypes = (c_char_p,)
  22. s = "abc"
  23. refcnt = grc(s)
  24. pyob = pythonapi.PyString_FromString(s)
  25. self.failUnlessEqual(grc(s), refcnt)
  26. self.failUnlessEqual(s, pyob)
  27. del pyob
  28. self.failUnlessEqual(grc(s), refcnt)
  29. if is_resource_enabled("refcount"):
  30. # This test is unreliable, because it is possible that code in
  31. # unittest changes the refcount of the '42' integer. So, it
  32. # is disabled by default.
  33. def test_PyInt_Long(self):
  34. ref42 = grc(42)
  35. pythonapi.PyInt_FromLong.restype = py_object
  36. self.failUnlessEqual(pythonapi.PyInt_FromLong(42), 42)
  37. self.failUnlessEqual(grc(42), ref42)
  38. pythonapi.PyInt_AsLong.argtypes = (py_object,)
  39. pythonapi.PyInt_AsLong.restype = c_long
  40. res = pythonapi.PyInt_AsLong(42)
  41. self.failUnlessEqual(grc(res), ref42 + 1)
  42. del res
  43. self.failUnlessEqual(grc(42), ref42)
  44. def test_PyObj_FromPtr(self):
  45. s = "abc def ghi jkl"
  46. ref = grc(s)
  47. # id(python-object) is the address
  48. pyobj = PyObj_FromPtr(id(s))
  49. self.failUnless(s is pyobj)
  50. self.failUnlessEqual(grc(s), ref + 1)
  51. del pyobj
  52. self.failUnlessEqual(grc(s), ref)
  53. def test_PyOS_snprintf(self):
  54. PyOS_snprintf = pythonapi.PyOS_snprintf
  55. PyOS_snprintf.argtypes = POINTER(c_char), c_size_t, c_char_p
  56. buf = c_buffer(256)
  57. PyOS_snprintf(buf, sizeof(buf), "Hello from %s", "ctypes")
  58. self.failUnlessEqual(buf.value, "Hello from ctypes")
  59. PyOS_snprintf(buf, sizeof(buf), "Hello from %s", "ctypes", 1, 2, 3)
  60. self.failUnlessEqual(buf.value, "Hello from ctypes")
  61. # not enough arguments
  62. self.failUnlessRaises(TypeError, PyOS_snprintf, buf)
  63. def test_pyobject_repr(self):
  64. self.failUnlessEqual(repr(py_object()), "py_object(<NULL>)")
  65. self.failUnlessEqual(repr(py_object(42)), "py_object(42)")
  66. self.failUnlessEqual(repr(py_object(object)), "py_object(%r)" % object)
  67. if __name__ == "__main__":
  68. unittest.main()