summaryrefslogtreecommitdiffstats
path: root/Lib/ctypes/test
diff options
context:
space:
mode:
authorThomas Heller <theller@ctypes.org>2008-06-04 18:59:03 (GMT)
committerThomas Heller <theller@ctypes.org>2008-06-04 18:59:03 (GMT)
commite70c3378c039cae30cd9ae559c4bdeb923254c43 (patch)
tree5977a09a2cd5bbe4aa3ff6bb52b4b554c179097a /Lib/ctypes/test
parenta2b34b87a94871999c78f343773ccf0f8c545932 (diff)
downloadcpython-e70c3378c039cae30cd9ae559c4bdeb923254c43.zip
cpython-e70c3378c039cae30cd9ae559c4bdeb923254c43.tar.gz
cpython-e70c3378c039cae30cd9ae559c4bdeb923254c43.tar.bz2
Issue #1798: Add ctypes calling convention that allows safe access to
errno (and LastError, on Windows). ctypes maintains a module-global, but thread-local, variable that contains an error number; called 'ctypes_errno' for this discussion. This variable is a private copy of the systems 'errno' value; the copy is swapped with the 'errno' variable on several occasions. Foreign functions created with CDLL(..., use_errno=True), when called, swap the values just before the actual function call, and swapped again immediately afterwards. The 'use_errno' parameter defaults to False, in this case 'ctypes_errno' is not touched. The values are also swapped immeditately before and after ctypes callback functions are called, if the callbacks are constructed using the new optional use_errno parameter set to True: CFUNCTYPE(..., use_errno=TRUE) or WINFUNCTYPE(..., use_errno=True). Two new ctypes functions are provided to access the 'ctypes_errno' value from Python: - ctypes.set_errno(value) sets ctypes_errno to 'value', the previous ctypes_errno value is returned. - ctypes.get_errno() returns the current ctypes_errno value. --- On Windows, the same scheme is implemented for the error value which is managed by the GetLastError() and SetLastError() windows api calls. The ctypes functions are 'ctypes.set_last_error(value)' and 'ctypes.get_last_error()', the CDLL and WinDLL optional parameter is named 'use_last_error', defaults to False. --- On Windows, TlsSetValue and TlsGetValue calls are used to provide thread local storage for the variables; ctypes compiled with __GNUC__ uses __thread variables.
Diffstat (limited to 'Lib/ctypes/test')
-rw-r--r--Lib/ctypes/test/test_errno.py76
1 files changed, 76 insertions, 0 deletions
diff --git a/Lib/ctypes/test/test_errno.py b/Lib/ctypes/test/test_errno.py
new file mode 100644
index 0000000..b80656b
--- /dev/null
+++ b/Lib/ctypes/test/test_errno.py
@@ -0,0 +1,76 @@
+import unittest, os, errno
+from ctypes import *
+from ctypes.util import find_library
+import threading
+
+class Test(unittest.TestCase):
+ def test_open(self):
+ libc_name = find_library("c")
+ if libc_name is not None:
+ libc = CDLL(libc_name, use_errno=True)
+ if os.name == "nt":
+ libc_open = libc._open
+ else:
+ libc_open = libc.open
+
+ libc_open.argtypes = c_char_p, c_int
+
+ self.failUnlessEqual(libc_open("", 0), -1)
+ self.failUnlessEqual(get_errno(), errno.ENOENT)
+
+ self.failUnlessEqual(set_errno(32), errno.ENOENT)
+ self.failUnlessEqual(get_errno(), 32)
+
+
+ def _worker():
+ set_errno(0)
+
+ libc = CDLL(libc_name, use_errno=False)
+ if os.name == "nt":
+ libc_open = libc._open
+ else:
+ libc_open = libc.open
+ libc_open.argtypes = c_char_p, c_int
+ self.failUnlessEqual(libc_open("", 0), -1)
+ self.failUnlessEqual(get_errno(), 0)
+
+ t = threading.Thread(target=_worker)
+ t.start()
+ t.join()
+
+ self.failUnlessEqual(get_errno(), 32)
+ set_errno(0)
+
+ if os.name == "nt":
+
+ def test_GetLastError(self):
+ dll = WinDLL("kernel32", use_last_error=True)
+ GetModuleHandle = dll.GetModuleHandleA
+ GetModuleHandle.argtypes = [c_wchar_p]
+
+ self.failUnlessEqual(0, GetModuleHandle("foo"))
+ self.failUnlessEqual(get_last_error(), 126)
+
+ self.failUnlessEqual(set_last_error(32), 126)
+ self.failUnlessEqual(get_last_error(), 32)
+
+ def _worker():
+ set_last_error(0)
+
+ dll = WinDLL("kernel32", use_last_error=False)
+ GetModuleHandle = dll.GetModuleHandleW
+ GetModuleHandle.argtypes = [c_wchar_p]
+ GetModuleHandle("bar")
+
+ self.failUnlessEqual(get_last_error(), 0)
+
+ t = threading.Thread(target=_worker)
+ t.start()
+ t.join()
+
+ self.failUnlessEqual(get_last_error(), 32)
+
+ set_last_error(0)
+
+if __name__ == "__main__":
+ unittest.main()