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