]> granicus.if.org Git - python/commitdiff
bpo-30125: Fix faulthandler.disable() on Windows (#1243)
authorVictor Stinner <victor.stinner@gmail.com>
Fri, 21 Apr 2017 21:17:33 +0000 (23:17 +0200)
committerGitHub <noreply@github.com>
Fri, 21 Apr 2017 21:17:33 +0000 (23:17 +0200)
On Windows, faulthandler.disable() now removes the exception handler
installed by faulthandler.enable().

Lib/test/test_faulthandler.py
Modules/faulthandler.c

index bdd8d1a2a6163fd8dfb64e6812327c9677dfad43..01cbae3f200b1aa26586b6ea62bc2ef6ace5ac84 100644 (file)
@@ -755,6 +755,18 @@ class FaultHandlerTests(unittest.TestCase):
                 3,
                 name)
 
+    @unittest.skipUnless(MS_WINDOWS, 'specific to Windows')
+    def test_disable_windows_exc_handler(self):
+        code = dedent("""
+            import faulthandler
+            faulthandler.enable()
+            faulthandler.disable()
+            code = faulthandler._EXCEPTION_ACCESS_VIOLATION
+            faulthandler._raise_exception(code)
+        """)
+        output, exitcode = self.get_output(code)
+        self.assertEqual(output, [])
+        self.assertEqual(exitcode, 0xC0000005)
 
 
 if __name__ == "__main__":
index 2f8b624fd160d9db7a8bae2e45e0c17371d6fa4f..61fc4908b0fe9bf9144de6cc8eb24f1ee6a8847f 100644 (file)
@@ -55,6 +55,9 @@ static struct {
     int fd;
     int all_threads;
     PyInterpreterState *interp;
+#ifdef MS_WINDOWS
+    void *exc_handler;
+#endif
 } fatal_error = {0, NULL, -1, 0};
 
 #ifdef FAULTHANDLER_LATER
@@ -462,7 +465,8 @@ faulthandler_enable(void)
     }
 
 #ifdef MS_WINDOWS
-    AddVectoredExceptionHandler(1, faulthandler_exc_handler);
+    assert(fatal_error.exc_handler == NULL);
+    fatal_error.exc_handler = AddVectoredExceptionHandler(1, faulthandler_exc_handler);
 #endif
     return 0;
 }
@@ -514,7 +518,12 @@ faulthandler_disable(void)
             faulthandler_disable_fatal_handler(handler);
         }
     }
-
+#ifdef MS_WINDOWS
+    if (fatal_error.exc_handler != NULL) {
+        RemoveVectoredExceptionHandler(fatal_error.exc_handler);
+        fatal_error.exc_handler = NULL;
+    }
+#endif
     Py_CLEAR(fatal_error.file);
 }