PageRenderTime 13ms CodeModel.GetById 2ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 1ms

/Lib/ctypes/test/test_errno.py

http://unladen-swallow.googlecode.com/
Python | 76 lines | 73 code | 3 blank | 0 comment | 3 complexity | 6db0aac0ddb46a19d708777dfef8be43 MD5 | raw file
 1import unittest, os, errno
 2from ctypes import *
 3from ctypes.util import find_library
 4import threading
 5
 6class Test(unittest.TestCase):
 7    def test_open(self):
 8        libc_name = find_library("c")
 9        if libc_name is not None:
10            libc = CDLL(libc_name, use_errno=True)
11            if os.name == "nt":
12                libc_open = libc._open
13            else:
14                libc_open = libc.open
15
16            libc_open.argtypes = c_char_p, c_int
17
18            self.failUnlessEqual(libc_open("", 0), -1)
19            self.failUnlessEqual(get_errno(), errno.ENOENT)
20
21            self.failUnlessEqual(set_errno(32), errno.ENOENT)
22            self.failUnlessEqual(get_errno(), 32)
23
24
25            def _worker():
26                set_errno(0)
27
28                libc = CDLL(libc_name, use_errno=False)
29                if os.name == "nt":
30                    libc_open = libc._open
31                else:
32                    libc_open = libc.open
33                libc_open.argtypes = c_char_p, c_int
34                self.failUnlessEqual(libc_open("", 0), -1)
35                self.failUnlessEqual(get_errno(), 0)
36
37            t = threading.Thread(target=_worker)
38            t.start()
39            t.join()
40
41            self.failUnlessEqual(get_errno(), 32)
42            set_errno(0)
43
44    if os.name == "nt":
45
46        def test_GetLastError(self):
47            dll = WinDLL("kernel32", use_last_error=True)
48            GetModuleHandle = dll.GetModuleHandleA
49            GetModuleHandle.argtypes = [c_wchar_p]
50
51            self.failUnlessEqual(0, GetModuleHandle("foo"))
52            self.failUnlessEqual(get_last_error(), 126)
53
54            self.failUnlessEqual(set_last_error(32), 126)
55            self.failUnlessEqual(get_last_error(), 32)
56
57            def _worker():
58                set_last_error(0)
59
60                dll = WinDLL("kernel32", use_last_error=False)
61                GetModuleHandle = dll.GetModuleHandleW
62                GetModuleHandle.argtypes = [c_wchar_p]
63                GetModuleHandle("bar")
64
65                self.failUnlessEqual(get_last_error(), 0)
66
67            t = threading.Thread(target=_worker)
68            t.start()
69            t.join()
70
71            self.failUnlessEqual(get_last_error(), 32)
72
73            set_last_error(0)
74
75if __name__ == "__main__":
76    unittest.main()