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

Skip to content

Commit 1f38621

Browse files
committed
#11732: add a new suppress_crash_popup() context manager to test.support that disables crash popups on Windows and use it in test_ctypes.
1 parent f9164e1 commit 1f38621

4 files changed

Lines changed: 41 additions & 6 deletions

File tree

Doc/library/test.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,13 @@ The :mod:`test.support` module defines the following functions:
365365
assert s.getvalue() == "hello\n"
366366

367367

368+
.. function:: suppress_crash_popup()
369+
370+
A context manager that disables Windows Error Reporting dialogs using
371+
`SetErrorMode <http://msdn.microsoft.com/en-us/library/windows/desktop/ms680621%28v=vs.85%29.aspx>`_.
372+
On other platforms it's a no-op.
373+
374+
368375
.. function:: import_module(name, deprecated=False)
369376

370377
This function imports and returns the named module. Unlike a normal

Lib/test/support.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"reap_children", "cpython_only", "check_impl_detail", "get_attribute",
5757
"swap_item", "swap_attr", "requires_IEEE_754",
5858
"TestHandler", "Matcher", "can_symlink", "skip_unless_symlink",
59-
"import_fresh_module", "failfast", "run_with_tz"
59+
"import_fresh_module", "failfast", "run_with_tz", "suppress_crash_popup",
6060
]
6161

6262
class Error(Exception):
@@ -1775,6 +1775,30 @@ def skip_unless_symlink(test):
17751775
msg = "Requires functional symlink implementation"
17761776
return test if ok else unittest.skip(msg)(test)
17771777

1778+
1779+
if sys.platform.startswith('win'):
1780+
@contextlib.contextmanager
1781+
def suppress_crash_popup():
1782+
"""Disable Windows Error Reporting dialogs using SetErrorMode."""
1783+
# see http://msdn.microsoft.com/en-us/library/windows/desktop/ms680621%28v=vs.85%29.aspx
1784+
# GetErrorMode is not available on Windows XP and Windows Server 2003,
1785+
# but SetErrorMode returns the previous value, so we can use that
1786+
import ctypes
1787+
k32 = ctypes.windll.kernel32
1788+
SEM_NOGPFAULTERRORBOX = 0x02
1789+
old_error_mode = k32.SetErrorMode(SEM_NOGPFAULTERRORBOX)
1790+
k32.SetErrorMode(old_error_mode | SEM_NOGPFAULTERRORBOX)
1791+
try:
1792+
yield
1793+
finally:
1794+
k32.SetErrorMode(old_error_mode)
1795+
else:
1796+
# this is a no-op for other platforms
1797+
@contextlib.contextmanager
1798+
def suppress_crash_popup():
1799+
yield
1800+
1801+
17781802
def patch(test_instance, object_to_patch, attr_name, new_value):
17791803
"""Override 'object_to_patch'.'attr_name' with 'new_value'.
17801804

Lib/test/test_capi.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,12 @@ def test_instancemethod(self):
4646

4747
@unittest.skipUnless(threading, 'Threading required for this test.')
4848
def test_no_FatalError_infinite_loop(self):
49-
p = subprocess.Popen([sys.executable, "-c",
50-
'import _testcapi;'
51-
'_testcapi.crash_no_current_thread()'],
52-
stdout=subprocess.PIPE,
53-
stderr=subprocess.PIPE)
49+
with support.suppress_crash_popup():
50+
p = subprocess.Popen([sys.executable, "-c",
51+
'import _testcapi;'
52+
'_testcapi.crash_no_current_thread()'],
53+
stdout=subprocess.PIPE,
54+
stderr=subprocess.PIPE)
5455
(out, err) = p.communicate()
5556
self.assertEqual(out, b'')
5657
# This used to cause an infinite loop.

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,9 @@ Extension Modules
950950
Tests
951951
-----
952952

953+
- Issue #11732: add a new suppress_crash_popup() context manager to test.support
954+
that disables crash popups on Windows and use it in test_ctypes.
955+
953956
- Issue #13898: test_ssl no longer prints a spurious stack trace on Ubuntu.
954957

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

0 commit comments

Comments
 (0)