~malept/ubuntu/lucid/python2.6/dev-dependency-fix

« back to all changes in this revision

Viewing changes to Lib/ctypes/test/test_returnfuncptrs.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2009-02-13 12:51:00 UTC
  • Revision ID: james.westby@ubuntu.com-20090213125100-uufgcb9yeqzujpqw
Tags: upstream-2.6.1
ImportĀ upstreamĀ versionĀ 2.6.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import unittest
 
2
from ctypes import *
 
3
 
 
4
import _ctypes_test
 
5
 
 
6
class ReturnFuncPtrTestCase(unittest.TestCase):
 
7
 
 
8
    def test_with_prototype(self):
 
9
        # The _ctypes_test shared lib/dll exports quite some functions for testing.
 
10
        # The get_strchr function returns a *pointer* to the C strchr function.
 
11
        dll = CDLL(_ctypes_test.__file__)
 
12
        get_strchr = dll.get_strchr
 
13
        get_strchr.restype = CFUNCTYPE(c_char_p, c_char_p, c_char)
 
14
        strchr = get_strchr()
 
15
        self.failUnlessEqual(strchr("abcdef", "b"), "bcdef")
 
16
        self.failUnlessEqual(strchr("abcdef", "x"), None)
 
17
        self.assertRaises(ArgumentError, strchr, "abcdef", 3)
 
18
        self.assertRaises(TypeError, strchr, "abcdef")
 
19
 
 
20
    def test_without_prototype(self):
 
21
        dll = CDLL(_ctypes_test.__file__)
 
22
        get_strchr = dll.get_strchr
 
23
        # the default 'c_int' would not work on systems where sizeof(int) != sizeof(void *)
 
24
        get_strchr.restype = c_void_p
 
25
        addr = get_strchr()
 
26
        # _CFuncPtr instances are now callable with an integer argument
 
27
        # which denotes a function address:
 
28
        strchr = CFUNCTYPE(c_char_p, c_char_p, c_char)(addr)
 
29
        self.failUnless(strchr("abcdef", "b"), "bcdef")
 
30
        self.failUnlessEqual(strchr("abcdef", "x"), None)
 
31
        self.assertRaises(ArgumentError, strchr, "abcdef", 3)
 
32
        self.assertRaises(TypeError, strchr, "abcdef")
 
33
 
 
34
if __name__ == "__main__":
 
35
    unittest.main()