Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 25a4045

Browse files
committed
#11732: add a new suppress_crash_popup() context manager to test.support.
1 parent 884f058 commit 25a4045

5 files changed

Lines changed: 40 additions & 4 deletions

File tree

Doc/library/test.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,13 @@ The :mod:`test.support` module defines the following functions:
405405
A decorator for running tests that require support for symbolic links.
406406

407407

408+
.. function:: suppress_crash_popup()
409+
410+
A context manager that disables Windows Error Reporting dialogs using
411+
`SetErrorMode <http://msdn.microsoft.com/en-us/library/windows/desktop/ms680621%28v=vs.85%29.aspx>`_.
412+
On other platforms it's a no-op.
413+
414+
408415
.. decorator:: anticipate_failure(condition)
409416

410417
A decorator to conditionally mark tests with

Lib/test/support.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
"TestHandler", "Matcher", "can_symlink", "skip_unless_symlink",
7272
"skip_unless_xattr", "import_fresh_module", "requires_zlib",
7373
"PIPE_MAX_SIZE", "failfast", "anticipate_failure", "run_with_tz",
74-
"requires_bz2", "requires_lzma"
74+
"requires_bz2", "requires_lzma", "suppress_crash_popup",
7575
]
7676

7777
class Error(Exception):
@@ -1905,6 +1905,28 @@ def skip_unless_xattr(test):
19051905
msg = "no non-broken extended attribute support"
19061906
return test if ok else unittest.skip(msg)(test)
19071907

1908+
1909+
if sys.platform.startswith('win'):
1910+
@contextlib.contextmanager
1911+
def suppress_crash_popup():
1912+
"""Disable Windows Error Reporting dialogs using SetErrorMode."""
1913+
# see http://msdn.microsoft.com/en-us/library/windows/desktop/ms680621%28v=vs.85%29.aspx
1914+
import ctypes
1915+
k32 = ctypes.windll.kernel32
1916+
old_error_mode = k32.GetErrorMode()
1917+
SEM_NOGPFAULTERRORBOX = 0x02
1918+
k32.SetErrorMode(old_error_mode | SEM_NOGPFAULTERRORBOX)
1919+
try:
1920+
yield
1921+
finally:
1922+
k32.SetErrorMode(old_error_mode)
1923+
else:
1924+
# this is a no-op for other platforms
1925+
@contextlib.contextmanager
1926+
def suppress_crash_popup():
1927+
yield
1928+
1929+
19081930
def patch(test_instance, object_to_patch, attr_name, new_value):
19091931
"""Override 'object_to_patch'.'attr_name' with 'new_value'.
19101932

Lib/test/test_capi.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ def test_instancemethod(self):
4444

4545
@unittest.skipUnless(threading, 'Threading required for this test.')
4646
def test_no_FatalError_infinite_loop(self):
47-
p = subprocess.Popen([sys.executable, "-c",
47+
with support.suppress_crash_popup():
48+
p = subprocess.Popen([sys.executable, "-c",
4849
'import _testcapi;'
4950
'_testcapi.crash_no_current_thread()'],
5051
stdout=subprocess.PIPE,

Lib/test/test_faulthandler.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ def check_fatal_error(self, code, line_number, name_regex,
101101
header=re.escape(header))
102102
if other_regex:
103103
regex += '|' + other_regex
104-
output, exitcode = self.get_output(code, filename)
104+
with support.suppress_crash_popup():
105+
output, exitcode = self.get_output(code, filename)
105106
output = '\n'.join(output)
106107
self.assertRegex(output, regex)
107108
self.assertNotEqual(exitcode, 0)
@@ -229,7 +230,8 @@ def test_disable(self):
229230
faulthandler._read_null()
230231
""".strip()
231232
not_expected = 'Fatal Python error'
232-
stderr, exitcode = self.get_output(code)
233+
with support.suppress_crash_popup():
234+
stderr, exitcode = self.get_output(code)
233235
stder = '\n'.join(stderr)
234236
self.assertTrue(not_expected not in stderr,
235237
"%r is present in %r" % (not_expected, stderr))

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,10 @@ Library
640640
Tests
641641
-----
642642

643+
- Issue #11732: add a new suppress_crash_popup() context manager to test.support
644+
that disables crash popups on Windows and use it in test_faulthandler and
645+
test_ctypes.
646+
643647
- Issue #13898: test_ssl no longer prints a spurious stack trace on Ubuntu.
644648

645649
- Issue #17249: convert a test in test_capi to use unittest and reap threads.

0 commit comments

Comments
 (0)