home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 December / maximum-cd-2009-12.iso / DiscContents / YoFrankie.msi / CS_runtime.cab / test_python_api.py.6B976ADF_8AE8_434E_B282_A06C7F624D2F < prev    next >
Encoding:
Text File  |  2006-08-17  |  3.0 KB  |  88 lines

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