1 | import unittest, os, errno |
---|
2 | from ctypes import * |
---|
3 | from ctypes.util import find_library |
---|
4 | from test import test_support |
---|
5 | try: |
---|
6 | import threading |
---|
7 | except ImportError: |
---|
8 | threading = None |
---|
9 | |
---|
10 | class Test(unittest.TestCase): |
---|
11 | def test_open(self): |
---|
12 | libc_name = find_library("c") |
---|
13 | if libc_name is None: |
---|
14 | raise unittest.SkipTest("Unable to find C library") |
---|
15 | libc = CDLL(libc_name, use_errno=True) |
---|
16 | if os.name == "nt": |
---|
17 | libc_open = libc._open |
---|
18 | else: |
---|
19 | libc_open = libc.open |
---|
20 | |
---|
21 | libc_open.argtypes = c_char_p, c_int |
---|
22 | |
---|
23 | self.assertEqual(libc_open("", 0), -1) |
---|
24 | self.assertEqual(get_errno(), errno.ENOENT) |
---|
25 | |
---|
26 | self.assertEqual(set_errno(32), errno.ENOENT) |
---|
27 | self.assertEqual(get_errno(), 32) |
---|
28 | |
---|
29 | if threading: |
---|
30 | def _worker(): |
---|
31 | set_errno(0) |
---|
32 | |
---|
33 | libc = CDLL(libc_name, use_errno=False) |
---|
34 | if os.name == "nt": |
---|
35 | libc_open = libc._open |
---|
36 | else: |
---|
37 | libc_open = libc.open |
---|
38 | libc_open.argtypes = c_char_p, c_int |
---|
39 | self.assertEqual(libc_open("", 0), -1) |
---|
40 | self.assertEqual(get_errno(), 0) |
---|
41 | |
---|
42 | t = threading.Thread(target=_worker) |
---|
43 | t.start() |
---|
44 | t.join() |
---|
45 | |
---|
46 | self.assertEqual(get_errno(), 32) |
---|
47 | set_errno(0) |
---|
48 | |
---|
49 | @unittest.skipUnless(os.name == "nt", 'Test specific to Windows') |
---|
50 | def test_GetLastError(self): |
---|
51 | dll = WinDLL("kernel32", use_last_error=True) |
---|
52 | GetModuleHandle = dll.GetModuleHandleA |
---|
53 | GetModuleHandle.argtypes = [c_wchar_p] |
---|
54 | |
---|
55 | self.assertEqual(0, GetModuleHandle("foo")) |
---|
56 | self.assertEqual(get_last_error(), 126) |
---|
57 | |
---|
58 | self.assertEqual(set_last_error(32), 126) |
---|
59 | self.assertEqual(get_last_error(), 32) |
---|
60 | |
---|
61 | def _worker(): |
---|
62 | set_last_error(0) |
---|
63 | |
---|
64 | dll = WinDLL("kernel32", use_last_error=False) |
---|
65 | GetModuleHandle = dll.GetModuleHandleW |
---|
66 | GetModuleHandle.argtypes = [c_wchar_p] |
---|
67 | GetModuleHandle("bar") |
---|
68 | |
---|
69 | self.assertEqual(get_last_error(), 0) |
---|
70 | |
---|
71 | t = threading.Thread(target=_worker) |
---|
72 | t.start() |
---|
73 | t.join() |
---|
74 | |
---|
75 | self.assertEqual(get_last_error(), 32) |
---|
76 | |
---|
77 | set_last_error(0) |
---|
78 | |
---|
79 | if __name__ == "__main__": |
---|
80 | unittest.main() |
---|