ยปCore Development>Code coverage>Lib/ctypes/test/test_errno.py

Python code coverage for Lib/ctypes/test/test_errno.py

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