Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Lib/test/audit-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,9 @@ def hook(event, args):


if __name__ == "__main__":
from test.libregrtest.setup import suppress_msvcrt_asserts
from test.support import suppress_msvcrt_asserts

suppress_msvcrt_asserts(False)
suppress_msvcrt_asserts()

test = sys.argv[1]
globals()[test]()
27 changes: 1 addition & 26 deletions Lib/test/libregrtest/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def setup_tests(ns):
if ns.threshold is not None:
gc.set_threshold(ns.threshold)

suppress_msvcrt_asserts(ns.verbose and ns.verbose >= 2)
support.suppress_msvcrt_asserts(ns.verbose and ns.verbose >= 2)

support.use_resources = ns.use_resources

Expand All @@ -93,31 +93,6 @@ def _test_audit_hook(name, args):
support.LONG_TIMEOUT = min(support.LONG_TIMEOUT, ns.timeout)


def suppress_msvcrt_asserts(verbose):
try:
import msvcrt
except ImportError:
return

msvcrt.SetErrorMode(msvcrt.SEM_FAILCRITICALERRORS|
msvcrt.SEM_NOALIGNMENTFAULTEXCEPT|
msvcrt.SEM_NOGPFAULTERRORBOX|
msvcrt.SEM_NOOPENFILEERRORBOX)
try:
msvcrt.CrtSetReportMode
except AttributeError:
# release build
return

for m in [msvcrt.CRT_WARN, msvcrt.CRT_ERROR, msvcrt.CRT_ASSERT]:
if verbose:
msvcrt.CrtSetReportMode(m, msvcrt.CRTDBG_MODE_FILE)
msvcrt.CrtSetReportFile(m, msvcrt.CRTDBG_FILE_STDERR)
else:
msvcrt.CrtSetReportMode(m, 0)



def replace_stdout():
"""Set stdout encoder error handler to backslashreplace (as stderr error
handler) to avoid UnicodeEncodeError when printing a traceback"""
Expand Down
53 changes: 34 additions & 19 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1899,6 +1899,27 @@ def test__all__(self):
test_case.assertCountEqual(module.__all__, expected)


def suppress_msvcrt_asserts(verbose=False):
try:
import msvcrt
except ImportError:
return

msvcrt.SetErrorMode(msvcrt.SEM_FAILCRITICALERRORS
| msvcrt.SEM_NOALIGNMENTFAULTEXCEPT
| msvcrt.SEM_NOGPFAULTERRORBOX
| msvcrt.SEM_NOOPENFILEERRORBOX)

# CrtSetReportMode() is only available in debug build
if hasattr(msvcrt, 'CrtSetReportMode'):
for m in [msvcrt.CRT_WARN, msvcrt.CRT_ERROR, msvcrt.CRT_ASSERT]:
if verbose:
msvcrt.CrtSetReportMode(m, msvcrt.CRTDBG_MODE_FILE)
msvcrt.CrtSetReportFile(m, msvcrt.CRTDBG_FILE_STDERR)
else:
msvcrt.CrtSetReportMode(m, 0)


class SuppressCrashReport:
"""Try to prevent a crash report from popping up.

Expand All @@ -1910,30 +1931,25 @@ class SuppressCrashReport:

def __enter__(self):
"""On Windows, disable Windows Error Reporting dialogs using
SetErrorMode.
SetErrorMode() and CrtSetReportMode().

On UNIX, try to save the previous core file size limit, then set
soft limit to 0.
"""
if sys.platform.startswith('win'):
# see http://msdn.microsoft.com/en-us/library/windows/desktop/ms680621.aspx
# GetErrorMode is not available on Windows XP and Windows Server 2003,
# but SetErrorMode returns the previous value, so we can use that
import ctypes
self._k32 = ctypes.windll.kernel32
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:
except ImportError:
return

self.old_value = msvcrt.GetErrorMode()

msvcrt.SetErrorMode(self.old_value | msvcrt.SEM_NOGPFAULTERRORBOX)

# bpo-23314: Suppress assert dialogs in debug builds.
# CrtSetReportMode() is only available in debug build.
if hasattr(msvcrt, 'CrtSetReportMode'):
self.old_modes = {}
for report_type in [msvcrt.CRT_WARN,
msvcrt.CRT_ERROR,
Expand Down Expand Up @@ -1985,10 +2001,10 @@ def __exit__(self, *ignore_exc):
return

if sys.platform.startswith('win'):
self._k32.SetErrorMode(self.old_value)
import msvcrt
msvcrt.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)
Expand Down Expand Up @@ -2332,7 +2348,6 @@ def wait_process(pid, *, exitcode, timeout=None):
if timeout is None:
timeout = SHORT_TIMEOUT
t0 = time.monotonic()
deadline = t0 + timeout
sleep = 0.001
max_sleep = 0.1
while True:
Expand Down
6 changes: 5 additions & 1 deletion Lib/test/test_repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ def test_close_stdin(self):
print("before close")
os.close(0)
''')
process = spawn_repl()
prepare_repl = dedent('''
from test.support import suppress_msvcrt_asserts
suppress_msvcrt_asserts()
''')
process = spawn_repl('-c', prepare_repl)
output = process.communicate(user_input)[0]
self.assertEqual(process.returncode, 0)
self.assertIn('before close', output)
Expand Down
20 changes: 19 additions & 1 deletion PC/clinic/msvcrtmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions PC/msvcrtmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,25 @@ msvcrt_set_error_mode_impl(PyObject *module, int mode)
}
#endif /* _DEBUG */

/*[clinic input]
msvcrt.GetErrorMode

Wrapper around GetErrorMode.
[clinic start generated code]*/

static PyObject *
msvcrt_GetErrorMode_impl(PyObject *module)
/*[clinic end generated code: output=3103fc6145913591 input=5a7fb083b6dd71fd]*/
{
unsigned int res;

_Py_BEGIN_SUPPRESS_IPH
res = GetErrorMode();
_Py_END_SUPPRESS_IPH

return PyLong_FromUnsignedLong(res);
}

/*[clinic input]
msvcrt.SetErrorMode

Expand Down Expand Up @@ -520,6 +539,7 @@ static struct PyMethodDef msvcrt_functions[] = {
MSVCRT_GETCHE_METHODDEF
MSVCRT_PUTCH_METHODDEF
MSVCRT_UNGETCH_METHODDEF
MSVCRT_GETERRORMODE_METHODDEF
MSVCRT_SETERRORMODE_METHODDEF
MSVCRT_CRTSETREPORTFILE_METHODDEF
MSVCRT_CRTSETREPORTMODE_METHODDEF
Expand Down