Skip to content
Open
46 changes: 45 additions & 1 deletion Lib/test/test_msvcrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import unittest
from textwrap import dedent

from test.support import os_helper, requires_resource
from test.support import os_helper, requires_resource, Py_DEBUG
from test.support.os_helper import TESTFN, TESTFN_ASCII

if sys.platform != "win32":
Expand Down Expand Up @@ -115,6 +115,50 @@ def test_heap_min(self):
except OSError:
pass

def test_GetErrorMode(self):
msvcrt.GetErrorMode()

def test_SetErrorMode(self):
old = msvcrt.GetErrorMode()
self.addCleanup(msvcrt.SetErrorMode, old)

returned = msvcrt.SetErrorMode(0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe test it also with non-zero value, and also with out-of-range values -1 and 2**32? If this is possible (if the test does not crash or hangs).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function SetErrorMode have weird behaviors and it behaves different in the test case or in a standalone test script, it sometimes returns different mode compare to the one set. I'm still try to figure it out and will update it tomorrow, thank you for the review!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Figured out the case of SetErrorMode and found that it's actually not set the value to current, but set the bit flags on it. Currently made the test workable, but still have some concerns about it.

Added some details as a separate comment blow.

self.assertIs(type(returned), int)
self.assertEqual(old, returned)

@unittest.skipUnless(Py_DEBUG, "only available under debug build")
def test_set_error_mode(self):
old = msvcrt.set_error_mode(msvcrt.REPORT_ERRMODE)
self.addCleanup(msvcrt.set_error_mode, old)

returned = msvcrt.set_error_mode(msvcrt.OUT_TO_STDERR)
self.assertIs(type(returned), int)
self.assertNotEqual(returned, -1)
self.assertEqual(old, returned)

@unittest.skipUnless(Py_DEBUG, "only available under debug build")
def test_CrtSetReportMode(self):
old = msvcrt.CrtSetReportMode(msvcrt.CRT_WARN,
msvcrt.CRTDBG_REPORT_MODE)
self.addCleanup(msvcrt.CrtSetReportMode, msvcrt.CRT_WARN, old)

returned = msvcrt.CrtSetReportMode(msvcrt.CRT_WARN,
msvcrt.CRTDBG_MODE_DEBUG)
self.assertIs(type(returned), int)
self.assertNotEqual(returned, -1)
self.assertEqual(old, returned)

@unittest.skipUnless(Py_DEBUG, "only available under debug build")
def test_CrtSetReportFile(self):
old = msvcrt.CrtSetReportFile(msvcrt.CRT_WARN,
msvcrt.CRTDBG_REPORT_FILE)
self.addCleanup(msvcrt.CrtSetReportFile, msvcrt.CRT_WARN, old)

returned = msvcrt.CrtSetReportFile(msvcrt.CRT_WARN,
msvcrt.CRTDBG_FILE_STDOUT)
self.assertIs(type(returned), int)
self.assertEqual(old, returned)


if __name__ == "__main__":
unittest.main()
Loading