home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_376 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  7.2 KB  |  226 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import unittest
  5. import sys
  6.  
  7. class SimpleTypesTestCase(unittest.TestCase):
  8.     
  9.     def setUp(self):
  10.         import ctypes
  11.         
  12.         try:
  13.             set_conversion_mode = set_conversion_mode
  14.             import _ctypes
  15.         except ImportError:
  16.             pass
  17.  
  18.         self.prev_conv_mode = set_conversion_mode('ascii', 'strict')
  19.  
  20.     
  21.     def tearDown(self):
  22.         
  23.         try:
  24.             set_conversion_mode = set_conversion_mode
  25.             import _ctypes
  26.         except ImportError:
  27.             pass
  28.  
  29.         set_conversion_mode(*self.prev_conv_mode)
  30.  
  31.     
  32.     def test_subclasses(self):
  33.         c_void_p = c_void_p
  34.         c_char_p = c_char_p
  35.         import ctypes
  36.         
  37.         class CVOIDP(c_void_p):
  38.             
  39.             def from_param(cls, value):
  40.                 return value * 2
  41.  
  42.             from_param = classmethod(from_param)
  43.  
  44.         
  45.         class CCHARP(c_char_p):
  46.             
  47.             def from_param(cls, value):
  48.                 return value * 4
  49.  
  50.             from_param = classmethod(from_param)
  51.  
  52.         self.failUnlessEqual(CVOIDP.from_param('abc'), 'abcabc')
  53.         self.failUnlessEqual(CCHARP.from_param('abc'), 'abcabcabcabc')
  54.         
  55.         try:
  56.             c_wchar_p = c_wchar_p
  57.             import ctypes
  58.         except ImportError:
  59.             return None
  60.  
  61.         
  62.         class CWCHARP(c_wchar_p):
  63.             
  64.             def from_param(cls, value):
  65.                 return value * 3
  66.  
  67.             from_param = classmethod(from_param)
  68.  
  69.         self.failUnlessEqual(CWCHARP.from_param('abc'), 'abcabcabc')
  70.  
  71.     
  72.     def test_cstrings(self):
  73.         c_char_p = c_char_p
  74.         byref = byref
  75.         import ctypes
  76.         s = '123'
  77.         self.failUnless(c_char_p.from_param(s)._obj is s)
  78.         self.failUnlessEqual(c_char_p.from_param(u'123')._obj, '123')
  79.         self.assertRaises(UnicodeEncodeError, c_char_p.from_param, u'123├┐')
  80.         self.assertRaises(TypeError, c_char_p.from_param, 42)
  81.         a = c_char_p('123')
  82.         self.failUnless(c_char_p.from_param(a) is a)
  83.  
  84.     
  85.     def test_cw_strings(self):
  86.         byref = byref
  87.         import ctypes
  88.         
  89.         try:
  90.             c_wchar_p = c_wchar_p
  91.             import ctypes
  92.         except ImportError:
  93.             return None
  94.  
  95.         s = u'123'
  96.         if sys.platform == 'win32':
  97.             self.failUnless(c_wchar_p.from_param(s)._obj is s)
  98.             self.assertRaises(TypeError, c_wchar_p.from_param, 42)
  99.             self.failUnlessEqual(c_wchar_p.from_param('123')._obj, u'123')
  100.         
  101.         self.assertRaises(UnicodeDecodeError, c_wchar_p.from_param, '123\xff')
  102.         pa = c_wchar_p.from_param(c_wchar_p(u'123'))
  103.         self.failUnlessEqual(type(pa), c_wchar_p)
  104.  
  105.     
  106.     def test_int_pointers(self):
  107.         c_short = c_short
  108.         c_uint = c_uint
  109.         c_int = c_int
  110.         c_long = c_long
  111.         POINTER = POINTER
  112.         pointer = pointer
  113.         import ctypes
  114.         LPINT = POINTER(c_int)
  115.         x = LPINT.from_param(pointer(c_int(42)))
  116.         self.failUnlessEqual(x.contents.value, 42)
  117.         self.failUnlessEqual(LPINT(c_int(42)).contents.value, 42)
  118.         self.assertEqual(LPINT.from_param(None), None)
  119.         if c_int != c_long:
  120.             self.assertRaises(TypeError, LPINT.from_param, pointer(c_long(42)))
  121.         
  122.         self.assertRaises(TypeError, LPINT.from_param, pointer(c_uint(42)))
  123.         self.assertRaises(TypeError, LPINT.from_param, pointer(c_short(42)))
  124.  
  125.     
  126.     def test_byref_pointer(self):
  127.         c_short = c_short
  128.         c_uint = c_uint
  129.         c_int = c_int
  130.         c_long = c_long
  131.         pointer = pointer
  132.         POINTER = POINTER
  133.         byref = byref
  134.         import ctypes
  135.         LPINT = POINTER(c_int)
  136.         LPINT.from_param(byref(c_int(42)))
  137.         self.assertRaises(TypeError, LPINT.from_param, byref(c_short(22)))
  138.         if c_int != c_long:
  139.             self.assertRaises(TypeError, LPINT.from_param, byref(c_long(22)))
  140.         
  141.         self.assertRaises(TypeError, LPINT.from_param, byref(c_uint(22)))
  142.  
  143.     
  144.     def test_byref_pointerpointer(self):
  145.         c_short = c_short
  146.         c_uint = c_uint
  147.         c_int = c_int
  148.         c_long = c_long
  149.         pointer = pointer
  150.         POINTER = POINTER
  151.         byref = byref
  152.         import ctypes
  153.         LPLPINT = POINTER(POINTER(c_int))
  154.         LPLPINT.from_param(byref(pointer(c_int(42))))
  155.         self.assertRaises(TypeError, LPLPINT.from_param, byref(pointer(c_short(22))))
  156.         if c_int != c_long:
  157.             self.assertRaises(TypeError, LPLPINT.from_param, byref(pointer(c_long(22))))
  158.         
  159.         self.assertRaises(TypeError, LPLPINT.from_param, byref(pointer(c_uint(22))))
  160.  
  161.     
  162.     def test_array_pointers(self):
  163.         c_short = c_short
  164.         c_uint = c_uint
  165.         c_int = c_int
  166.         c_long = c_long
  167.         POINTER = POINTER
  168.         import ctypes
  169.         INTARRAY = c_int * 3
  170.         ia = INTARRAY()
  171.         self.failUnlessEqual(len(ia), 3)
  172.         []([ ia[i] for i in range(3) ], [
  173.             0,
  174.             0,
  175.             0])
  176.         LPINT = POINTER(c_int)
  177.         LPINT.from_param(c_int * 3())
  178.         self.assertRaises(TypeError, LPINT.from_param, c_short * 3)
  179.         self.assertRaises(TypeError, LPINT.from_param, c_long * 3)
  180.         self.assertRaises(TypeError, LPINT.from_param, c_uint * 3)
  181.  
  182.     
  183.     def test_noctypes_argtype(self):
  184.         import _ctypes_test
  185.         CDLL = CDLL
  186.         c_void_p = c_void_p
  187.         ArgumentError = ArgumentError
  188.         import ctypes
  189.         func = CDLL(_ctypes_test.__file__)._testfunc_p_p
  190.         func.restype = c_void_p
  191.         self.assertRaises(TypeError, setattr, func, 'argtypes', (object,))
  192.         
  193.         class Adapter(object):
  194.             
  195.             def from_param(cls, obj):
  196.                 pass
  197.  
  198.  
  199.         func.argtypes = (Adapter(),)
  200.         self.failUnlessEqual(func(None), None)
  201.         self.failUnlessEqual(func(object()), None)
  202.         
  203.         class Adapter(object):
  204.             
  205.             def from_param(cls, obj):
  206.                 return obj
  207.  
  208.  
  209.         func.argtypes = (Adapter(),)
  210.         self.assertRaises(ArgumentError, func, object())
  211.         self.failUnlessEqual(func(c_void_p(42)), 42)
  212.         
  213.         class Adapter(object):
  214.             
  215.             def from_param(cls, obj):
  216.                 raise ValueError(obj)
  217.  
  218.  
  219.         func.argtypes = (Adapter(),)
  220.         self.assertRaises(ArgumentError, func, 99)
  221.  
  222.  
  223. if __name__ == '__main__':
  224.     unittest.main()
  225.  
  226.