[Python-checkins] r87406 - in python/branches/release27-maint: Lib/unittest/case.py Lib/unittest/test/test_case.py Misc/NEWS

michael.foord python-checkins at python.org
Sun Dec 19 15:53:19 CET 2010


Author: michael.foord
Date: Sun Dec 19 15:53:19 2010
New Revision: 87406

Log:
Issue 10611. SystemExit should not cause a unittest test run to exit.

Modified:
   python/branches/release27-maint/Lib/unittest/case.py
   python/branches/release27-maint/Lib/unittest/test/test_case.py
   python/branches/release27-maint/Misc/NEWS

Modified: python/branches/release27-maint/Lib/unittest/case.py
==============================================================================
--- python/branches/release27-maint/Lib/unittest/case.py	(original)
+++ python/branches/release27-maint/Lib/unittest/case.py	Sun Dec 19 15:53:19 2010
@@ -311,11 +311,15 @@
                 self.setUp()
             except SkipTest as e:
                 self._addSkip(result, str(e))
-            except Exception:
+            except KeyboardInterrupt:
+                raise
+            except:
                 result.addError(self, sys.exc_info())
             else:
                 try:
                     testMethod()
+                except KeyboardInterrupt:
+                    raise
                 except self.failureException:
                     result.addFailure(self, sys.exc_info())
                 except _ExpectedFailure as e:
@@ -336,14 +340,16 @@
                         result.addFailure(self, sys.exc_info())
                 except SkipTest as e:
                     self._addSkip(result, str(e))
-                except Exception:
+                except:
                     result.addError(self, sys.exc_info())
                 else:
                     success = True
 
                 try:
                     self.tearDown()
-                except Exception:
+                except KeyboardInterrupt:
+                    raise
+                except:
                     result.addError(self, sys.exc_info())
                     success = False
 
@@ -367,7 +373,9 @@
             function, args, kwargs = self._cleanups.pop(-1)
             try:
                 function(*args, **kwargs)
-            except Exception:
+            except KeyboardInterrupt:
+                raise
+            except:
                 ok = False
                 result.addError(self, sys.exc_info())
         return ok

Modified: python/branches/release27-maint/Lib/unittest/test/test_case.py
==============================================================================
--- python/branches/release27-maint/Lib/unittest/test/test_case.py	(original)
+++ python/branches/release27-maint/Lib/unittest/test/test_case.py	Sun Dec 19 15:53:19 2010
@@ -999,6 +999,58 @@
         # This shouldn't blow up
         deepcopy(test)
 
+    def testKeyboardInterrupt(self):
+        def _raise(self=None):
+            raise KeyboardInterrupt
+        def nothing(self):
+            pass
+
+        class Test1(unittest.TestCase):
+            test_something = _raise
+
+        class Test2(unittest.TestCase):
+            setUp = _raise
+            test_something = nothing
+
+        class Test3(unittest.TestCase):
+            test_something = nothing
+            tearDown = _raise
+
+        class Test4(unittest.TestCase):
+            def test_something(self):
+                self.addCleanup(_raise)
+
+        for klass in (Test1, Test2, Test3, Test4):
+            with self.assertRaises(KeyboardInterrupt):
+                klass('test_something').run()
+
+    def testSystemExit(self):
+        def _raise(self=None):
+            raise SystemExit
+        def nothing(self):
+            pass
+
+        class Test1(unittest.TestCase):
+            test_something = _raise
+
+        class Test2(unittest.TestCase):
+            setUp = _raise
+            test_something = nothing
+
+        class Test3(unittest.TestCase):
+            test_something = nothing
+            tearDown = _raise
+
+        class Test4(unittest.TestCase):
+            def test_something(self):
+                self.addCleanup(_raise)
+
+        for klass in (Test1, Test2, Test3, Test4):
+            result = unittest.TestResult()
+            klass('test_something').run(result)
+            self.assertEqual(len(result.errors), 1)
+            self.assertEqual(result.testsRun, 1)
+
 
 if __name__ == '__main__':
     unittest.main()

Modified: python/branches/release27-maint/Misc/NEWS
==============================================================================
--- python/branches/release27-maint/Misc/NEWS	(original)
+++ python/branches/release27-maint/Misc/NEWS	Sun Dec 19 15:53:19 2010
@@ -22,6 +22,8 @@
 Library
 -------
 
+- Issue #10611: SystemExit should not cause a unittest test run to exit.
+
 - Issue #6791: Limit header line length (to 65535 bytes) in http.client,
   to avoid denial of services from the other party.
 


More information about the Python-checkins mailing list