[Python-checkins] r80556 - python/trunk/Lib/ctypes/test/test_errno.py

victor.stinner python-checkins at python.org
Wed Apr 28 00:01:24 CEST 2010


Author: victor.stinner
Date: Wed Apr 28 00:01:24 2010
New Revision: 80556

Log:
Issue #7449, part 5: split Test.test_open() of ctypes/test/test_errno.py

 * Split Test.test_open() in 2 functions: test_open() and test_thread_open()
 * Skip test_open() and test_thread_open() if we are unable to find the C
   library
 * Skip test_thread_open() if thread support is disabled
 * Use unittest.skipUnless(os.name == "nt", ...) on test_GetLastError()


Modified:
   python/trunk/Lib/ctypes/test/test_errno.py

Modified: python/trunk/Lib/ctypes/test/test_errno.py
==============================================================================
--- python/trunk/Lib/ctypes/test/test_errno.py	(original)
+++ python/trunk/Lib/ctypes/test/test_errno.py	Wed Apr 28 00:01:24 2010
@@ -1,76 +1,84 @@
 import unittest, os, errno
 from ctypes import *
 from ctypes.util import find_library
-import threading
+from test import test_support
+try:
+    import threading
+except ImportError:
+    threading = None
+
+libc_name = find_library("c")
 
 class Test(unittest.TestCase):
+    @unittest.skipUnless(libc_name, 'Unable to find the C library')
     def test_open(self):
-        libc_name = find_library("c")
-        if libc_name is not None:
-            libc = CDLL(libc_name, use_errno=True)
+        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.assertEqual(libc_open("", 0), -1)
+        self.assertEqual(get_errno(), errno.ENOENT)
+
+        self.assertEqual(set_errno(32), errno.ENOENT)
+        self.assertEqual(get_errno(), 32)
+
+    @unittest.skipUnless(libc_name, 'Unable to find the C library')
+    @unittest.skipUnless(threading, 'This test requires threading.')
+    def test_open_thread(self):
+        self.assertEqual(set_errno(32), errno.ENOENT)
+
+        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.assertEqual(libc_open("", 0), -1)
-            self.assertEqual(get_errno(), errno.ENOENT)
-
-            self.assertEqual(set_errno(32), errno.ENOENT)
-            self.assertEqual(get_errno(), 32)
+            self.assertEqual(get_errno(), 0)
 
+        t = threading.Thread(target=_worker)
+        t.start()
+        t.join()
 
-            def _worker():
-                set_errno(0)
+        self.assertEqual(get_errno(), 32)
+        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.assertEqual(libc_open("", 0), -1)
-                self.assertEqual(get_errno(), 0)
+    @unittest.skipUnless(os.name == "nt", 'Test specific to Windows')
+    def test_GetLastError(self):
+        dll = WinDLL("kernel32", use_last_error=True)
+        GetModuleHandle = dll.GetModuleHandleA
+        GetModuleHandle.argtypes = [c_wchar_p]
 
-            t = threading.Thread(target=_worker)
-            t.start()
-            t.join()
+        self.assertEqual(0, GetModuleHandle("foo"))
+        self.assertEqual(get_last_error(), 126)
 
-            self.assertEqual(get_errno(), 32)
-            set_errno(0)
+        self.assertEqual(set_last_error(32), 126)
+        self.assertEqual(get_last_error(), 32)
 
-    if os.name == "nt":
+        def _worker():
+            set_last_error(0)
 
-        def test_GetLastError(self):
-            dll = WinDLL("kernel32", use_last_error=True)
-            GetModuleHandle = dll.GetModuleHandleA
+            dll = WinDLL("kernel32", use_last_error=False)
+            GetModuleHandle = dll.GetModuleHandleW
             GetModuleHandle.argtypes = [c_wchar_p]
+            GetModuleHandle("bar")
 
-            self.assertEqual(0, GetModuleHandle("foo"))
-            self.assertEqual(get_last_error(), 126)
-
-            self.assertEqual(set_last_error(32), 126)
-            self.assertEqual(get_last_error(), 32)
-
-            def _worker():
-                set_last_error(0)
+            self.assertEqual(get_last_error(), 0)
 
-                dll = WinDLL("kernel32", use_last_error=False)
-                GetModuleHandle = dll.GetModuleHandleW
-                GetModuleHandle.argtypes = [c_wchar_p]
-                GetModuleHandle("bar")
+        t = threading.Thread(target=_worker)
+        t.start()
+        t.join()
 
-                self.assertEqual(get_last_error(), 0)
+        self.assertEqual(get_last_error(), 32)
 
-            t = threading.Thread(target=_worker)
-            t.start()
-            t.join()
-
-            self.assertEqual(get_last_error(), 32)
-
-            set_last_error(0)
+        set_last_error(0)
 
 if __name__ == "__main__":
     unittest.main()


More information about the Python-checkins mailing list