From c55a316cff6723a22dbe57b2e9d0d25b5387226d Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Mon, 23 Feb 2015 07:56:13 -0800 Subject: [PATCH] Issue 23314: SuppressCrashReports now disables CRT assertions SuppressCrashReports should be used in test subprocesses that test invalid conditions. --- Lib/test/support/__init__.py | 27 +++++++++++++++++++++++++++ Lib/test/tf_inherit_check.py | 32 +++++++++++++++++--------------- 2 files changed, 44 insertions(+), 15 deletions(-) diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index 205c47c0c1..7bfa12c2d0 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -2151,6 +2151,7 @@ class SuppressCrashReport: disable the creation of coredump file. """ old_value = None + old_modes = None def __enter__(self): """On Windows, disable Windows Error Reporting dialogs using @@ -2168,6 +2169,26 @@ class SuppressCrashReport: SEM_NOGPFAULTERRORBOX = 0x02 self.old_value = self._k32.SetErrorMode(SEM_NOGPFAULTERRORBOX) self._k32.SetErrorMode(self.old_value | SEM_NOGPFAULTERRORBOX) + + # Suppress assert dialogs in debug builds + # (see http://bugs.python.org/issue23314) + try: + import msvcrt + msvcrt.CrtSetReportMode + except (AttributeError, ImportError): + # no msvcrt or a release build + pass + else: + self.old_modes = {} + for report_type in [msvcrt.CRT_WARN, + msvcrt.CRT_ERROR, + msvcrt.CRT_ASSERT]: + old_mode = msvcrt.CrtSetReportMode(report_type, + msvcrt.CRTDBG_MODE_FILE) + old_file = msvcrt.CrtSetReportFile(report_type, + msvcrt.CRTDBG_FILE_STDERR) + self.old_modes[report_type] = old_mode, old_file + else: if resource is not None: try: @@ -2199,6 +2220,12 @@ class SuppressCrashReport: if sys.platform.startswith('win'): self._k32.SetErrorMode(self.old_value) + + if self.old_modes: + import msvcrt + for report_type, (old_mode, old_file) in self.old_modes.items(): + msvcrt.CrtSetReportMode(report_type, old_mode) + msvcrt.CrtSetReportFile(report_type, old_file) else: if resource is not None: try: diff --git a/Lib/test/tf_inherit_check.py b/Lib/test/tf_inherit_check.py index afe50d2325..138f25a858 100644 --- a/Lib/test/tf_inherit_check.py +++ b/Lib/test/tf_inherit_check.py @@ -4,22 +4,24 @@ import sys import os +from test.support import SuppressCrashReport -verbose = (sys.argv[1] == 'v') -try: - fd = int(sys.argv[2]) - +with SuppressCrashReport(): + verbose = (sys.argv[1] == 'v') try: - os.write(fd, b"blat") - except OSError: - # Success -- could not write to fd. - sys.exit(0) - else: + fd = int(sys.argv[2]) + + try: + os.write(fd, b"blat") + except OSError: + # Success -- could not write to fd. + sys.exit(0) + else: + if verbose: + sys.stderr.write("fd %d is open in child" % fd) + sys.exit(1) + + except Exception: if verbose: - sys.stderr.write("fd %d is open in child" % fd) + raise sys.exit(1) - -except Exception: - if verbose: - raise - sys.exit(1) -- 2.40.0