From 88456294dbf3a37d95ae4cc17348386609fe9754 Mon Sep 17 00:00:00 2001 From: AN Long Date: Wed, 6 Sep 2023 22:06:51 +0800 Subject: [PATCH 01/10] gh-108996: add tests for msvcrt --- Lib/test/test_msvcrt.py | 85 +++++++++++++++++++ ...-09-06-22-06-22.gh-issue-108996.IBhR3U.rst | 1 + 2 files changed, 86 insertions(+) create mode 100644 Lib/test/test_msvcrt.py create mode 100644 Misc/NEWS.d/next/Tests/2023-09-06-22-06-22.gh-issue-108996.IBhR3U.rst diff --git a/Lib/test/test_msvcrt.py b/Lib/test/test_msvcrt.py new file mode 100644 index 00000000000000..1d7e0ad8f59a60 --- /dev/null +++ b/Lib/test/test_msvcrt.py @@ -0,0 +1,85 @@ +import os +import sys +import unittest + +from test.support import os_helper +from test.support.os_helper import TESTFN, TESTFN_ASCII + +if sys.platform != "win32": + raise unittest.SkipTest("windows related tests") + +import _winapi +import msvcrt + + +class TestFileOperations(unittest.TestCase): + def test_locking(self): + with open(TESTFN, "w") as f: + self.addCleanup(os_helper.unlink, TESTFN) + + msvcrt.locking(f.fileno(), msvcrt.LK_LOCK, 1) + self.assertRaises(OSError, msvcrt.locking, f.fileno(), msvcrt.LK_NBLCK, 1) + + def test_unlockfile(self): + with open(TESTFN, "w") as f: + self.addCleanup(os_helper.unlink, TESTFN) + + msvcrt.locking(f.fileno(), msvcrt.LK_LOCK, 1) + msvcrt.locking(f.fileno(), msvcrt.LK_UNLCK, 1) + msvcrt.locking(f.fileno(), msvcrt.LK_LOCK, 1) + + def test_setmode(self): + with open(TESTFN, "w") as f: + self.addCleanup(os_helper.unlink, TESTFN) + + msvcrt.setmode(f.fileno(), os.O_BINARY) + msvcrt.setmode(f.fileno(), os.O_TEXT) + + def test_open_osfhandle(self): + h = _winapi.CreateFile(TESTFN_ASCII, _winapi.GENERIC_WRITE, 0, 0, 1, 128, 0) + self.addCleanup(os_helper.unlink, TESTFN_ASCII) + + fd = msvcrt.open_osfhandle(h, os.O_RDONLY) + os.close(fd) + + def test_get_osfhandle(self): + with open(TESTFN, "w") as f: + self.addCleanup(os_helper.unlink, TESTFN) + + msvcrt.get_osfhandle(f.fileno()) + + +class TestConsoleIO(unittest.TestCase): + def test_kbhit(self): + msvcrt.kbhit() + + def test_getch(self): + msvcrt.ungetch(b'c') + self.assertEqual(msvcrt.getch(), b'c') + + def test_getwch(self): + msvcrt.ungetwch('c') + self.assertEqual(msvcrt.getwch(), 'c') + + def test_getche(self): + msvcrt.ungetch(b'c') + self.assertEqual(msvcrt.getche(), b'c') + + def test_getwche(self): + msvcrt.ungetwch('c') + self.assertEqual(msvcrt.getwche(), 'c') + + def test_putch(self): + msvcrt.putch(b'c') + + def test_putwch(self): + msvcrt.putwch('字') + + +class TestOther(unittest.TestCase): + def test_heap_min(self): + msvcrt.heapmin() + + +if __name__ == "__main__": + unittest.main() diff --git a/Misc/NEWS.d/next/Tests/2023-09-06-22-06-22.gh-issue-108996.IBhR3U.rst b/Misc/NEWS.d/next/Tests/2023-09-06-22-06-22.gh-issue-108996.IBhR3U.rst new file mode 100644 index 00000000000000..887f8b74bcfa30 --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2023-09-06-22-06-22.gh-issue-108996.IBhR3U.rst @@ -0,0 +1 @@ +Add tests for ``msvcrt``. From 7ded8fcaf9dd573c5d01f3943cbf39c6fe883fbc Mon Sep 17 00:00:00 2001 From: AN Long Date: Thu, 7 Sep 2023 16:28:54 +0800 Subject: [PATCH 02/10] Update Lib/test/test_msvcrt.py Co-authored-by: Terry Jan Reedy --- Lib/test/test_msvcrt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_msvcrt.py b/Lib/test/test_msvcrt.py index 1d7e0ad8f59a60..b8ee1725d49175 100644 --- a/Lib/test/test_msvcrt.py +++ b/Lib/test/test_msvcrt.py @@ -51,7 +51,7 @@ def test_get_osfhandle(self): class TestConsoleIO(unittest.TestCase): def test_kbhit(self): - msvcrt.kbhit() + self.assertEqual(msvcrt.kbhit(), 0) def test_getch(self): msvcrt.ungetch(b'c') From 2f00ed4c3913a8c3622a5c7e914116023ba75fc0 Mon Sep 17 00:00:00 2001 From: AN Long Date: Thu, 7 Sep 2023 16:35:35 +0800 Subject: [PATCH 03/10] Update Lib/test/test_msvcrt.py Co-authored-by: Terry Jan Reedy --- Lib/test/test_msvcrt.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Lib/test/test_msvcrt.py b/Lib/test/test_msvcrt.py index b8ee1725d49175..d1a514b805de81 100644 --- a/Lib/test/test_msvcrt.py +++ b/Lib/test/test_msvcrt.py @@ -49,6 +49,8 @@ def test_get_osfhandle(self): msvcrt.get_osfhandle(f.fileno()) +c = '\u5b57' # unicode CJK char (meaning 'character') for 'wide-char' tests + class TestConsoleIO(unittest.TestCase): def test_kbhit(self): self.assertEqual(msvcrt.kbhit(), 0) From b591de0803d34ee12bf5ee0a627b6e3dd4eabef4 Mon Sep 17 00:00:00 2001 From: AN Long Date: Thu, 7 Sep 2023 16:35:46 +0800 Subject: [PATCH 04/10] Update Lib/test/test_msvcrt.py Co-authored-by: Terry Jan Reedy --- Lib/test/test_msvcrt.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_msvcrt.py b/Lib/test/test_msvcrt.py index d1a514b805de81..d0c66cc7fd9682 100644 --- a/Lib/test/test_msvcrt.py +++ b/Lib/test/test_msvcrt.py @@ -68,8 +68,8 @@ def test_getche(self): self.assertEqual(msvcrt.getche(), b'c') def test_getwche(self): - msvcrt.ungetwch('c') - self.assertEqual(msvcrt.getwche(), 'c') + msvcrt.ungetwch(c) + self.assertEqual(msvcrt.getwche(), c) def test_putch(self): msvcrt.putch(b'c') From ab0d25b4cb4924d62d7fae6d1026d3e8bb1ff41d Mon Sep 17 00:00:00 2001 From: AN Long Date: Thu, 7 Sep 2023 16:35:58 +0800 Subject: [PATCH 05/10] Update Lib/test/test_msvcrt.py Co-authored-by: Terry Jan Reedy --- Lib/test/test_msvcrt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_msvcrt.py b/Lib/test/test_msvcrt.py index d0c66cc7fd9682..4993a6fc240365 100644 --- a/Lib/test/test_msvcrt.py +++ b/Lib/test/test_msvcrt.py @@ -75,7 +75,7 @@ def test_putch(self): msvcrt.putch(b'c') def test_putwch(self): - msvcrt.putwch('字') + msvcrt.putwch(c) class TestOther(unittest.TestCase): From 2dd611cfa5683f1b1269e916719216b1f1a0f936 Mon Sep 17 00:00:00 2001 From: AN Long Date: Thu, 7 Sep 2023 16:36:08 +0800 Subject: [PATCH 06/10] Update Lib/test/test_msvcrt.py Co-authored-by: Terry Jan Reedy --- Lib/test/test_msvcrt.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_msvcrt.py b/Lib/test/test_msvcrt.py index 4993a6fc240365..40580011d52e27 100644 --- a/Lib/test/test_msvcrt.py +++ b/Lib/test/test_msvcrt.py @@ -60,8 +60,8 @@ def test_getch(self): self.assertEqual(msvcrt.getch(), b'c') def test_getwch(self): - msvcrt.ungetwch('c') - self.assertEqual(msvcrt.getwch(), 'c') + msvcrt.ungetwch(c) + self.assertEqual(msvcrt.getwch(), c) def test_getche(self): msvcrt.ungetch(b'c') From 394ef2d288813357d4917588f824339b83c53ee0 Mon Sep 17 00:00:00 2001 From: AN Long Date: Thu, 7 Sep 2023 16:36:34 +0800 Subject: [PATCH 07/10] Update Lib/test/test_msvcrt.py Co-authored-by: Terry Jan Reedy --- Lib/test/test_msvcrt.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_msvcrt.py b/Lib/test/test_msvcrt.py index 40580011d52e27..6c93d1c41b29b7 100644 --- a/Lib/test/test_msvcrt.py +++ b/Lib/test/test_msvcrt.py @@ -80,7 +80,10 @@ def test_putwch(self): class TestOther(unittest.TestCase): def test_heap_min(self): - msvcrt.heapmin() + try: + msvcrt.heapmin() + except OSError: + pass if __name__ == "__main__": From 3bb378ec0171408f5c39000937aae486b808dc6a Mon Sep 17 00:00:00 2001 From: AN Long Date: Thu, 7 Sep 2023 17:44:53 +0800 Subject: [PATCH 08/10] fix tests --- Lib/test/test_msvcrt.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_msvcrt.py b/Lib/test/test_msvcrt.py index 6c93d1c41b29b7..dffb1e75d4f4db 100644 --- a/Lib/test/test_msvcrt.py +++ b/Lib/test/test_msvcrt.py @@ -9,7 +9,9 @@ raise unittest.SkipTest("windows related tests") import _winapi -import msvcrt +import msvcrt; + +from _testconsole import write_input class TestFileOperations(unittest.TestCase): @@ -50,6 +52,8 @@ def test_get_osfhandle(self): c = '\u5b57' # unicode CJK char (meaning 'character') for 'wide-char' tests +c_encoded = b'\x57\x5b' # utf-16-le (which windows internally used) encoded char for this CJK char + class TestConsoleIO(unittest.TestCase): def test_kbhit(self): @@ -60,16 +64,28 @@ def test_getch(self): self.assertEqual(msvcrt.getch(), b'c') def test_getwch(self): - msvcrt.ungetwch(c) - self.assertEqual(msvcrt.getwch(), c) + stdin = open('CONIN$', 'r') + old_stdin = sys.stdin + try: + sys.stdin = stdin + write_input(stdin.buffer.raw, c_encoded) + self.assertEqual(msvcrt.getwch(), c) + finally: + sys.stdin = old_stdin def test_getche(self): msvcrt.ungetch(b'c') self.assertEqual(msvcrt.getche(), b'c') def test_getwche(self): - msvcrt.ungetwch(c) - self.assertEqual(msvcrt.getwche(), c) + stdin = open('CONIN$', 'r') + old_stdin = sys.stdin + try: + sys.stdin = stdin + write_input(stdin.buffer.raw, c_encoded) + self.assertEqual(msvcrt.getwche(), c) + finally: + sys.stdin = old_stdin def test_putch(self): msvcrt.putch(b'c') From 2872257bdcc197073910655f57c2ba7f32540f80 Mon Sep 17 00:00:00 2001 From: AN Long Date: Thu, 7 Sep 2023 19:49:16 +0800 Subject: [PATCH 09/10] Fix lint error --- Lib/test/test_msvcrt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_msvcrt.py b/Lib/test/test_msvcrt.py index dffb1e75d4f4db..81c6b05623fc45 100644 --- a/Lib/test/test_msvcrt.py +++ b/Lib/test/test_msvcrt.py @@ -51,7 +51,7 @@ def test_get_osfhandle(self): msvcrt.get_osfhandle(f.fileno()) -c = '\u5b57' # unicode CJK char (meaning 'character') for 'wide-char' tests +c = '\u5b57' # unicode CJK char (meaning 'character') for 'wide-char' tests c_encoded = b'\x57\x5b' # utf-16-le (which windows internally used) encoded char for this CJK char From dff3acce48b7a7e1d66921fdc7da0f99686ef075 Mon Sep 17 00:00:00 2001 From: AN Long Date: Fri, 8 Sep 2023 20:28:13 +0800 Subject: [PATCH 10/10] Update Lib/test/test_msvcrt.py Co-authored-by: Steve Dower --- Lib/test/test_msvcrt.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_msvcrt.py b/Lib/test/test_msvcrt.py index 81c6b05623fc45..adf4e26b98ac55 100644 --- a/Lib/test/test_msvcrt.py +++ b/Lib/test/test_msvcrt.py @@ -41,8 +41,13 @@ def test_open_osfhandle(self): h = _winapi.CreateFile(TESTFN_ASCII, _winapi.GENERIC_WRITE, 0, 0, 1, 128, 0) self.addCleanup(os_helper.unlink, TESTFN_ASCII) - fd = msvcrt.open_osfhandle(h, os.O_RDONLY) - os.close(fd) + try: + fd = msvcrt.open_osfhandle(h, os.O_RDONLY) + h = None + os.close(fd) + finally: + if h: + _winapi.CloseHandle(h) def test_get_osfhandle(self): with open(TESTFN, "w") as f: