-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
gh-108996: add tests for msvcrt #109004
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
gh-108996: add tests for msvcrt #109004
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8845629
gh-108996: add tests for msvcrt
aisk df210bc
Merge branch 'main' into test-msvcrt
aisk 7ded8fc
Update Lib/test/test_msvcrt.py
aisk 2f00ed4
Update Lib/test/test_msvcrt.py
aisk b591de0
Update Lib/test/test_msvcrt.py
aisk ab0d25b
Update Lib/test/test_msvcrt.py
aisk 2dd611c
Update Lib/test/test_msvcrt.py
aisk 394ef2d
Update Lib/test/test_msvcrt.py
aisk 3bb378e
fix tests
aisk 2872257
Fix lint error
aisk 45ff90e
Merge branch 'main' into test-msvcrt
aisk 3a7bec2
Merge branch 'main' into test-msvcrt
aisk dff3acc
Update Lib/test/test_msvcrt.py
aisk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
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; | ||
|
||
from _testconsole import write_input | ||
|
||
|
||
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) | ||
|
||
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: | ||
self.addCleanup(os_helper.unlink, TESTFN) | ||
|
||
msvcrt.get_osfhandle(f.fileno()) | ||
|
||
|
||
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): | ||
self.assertEqual(msvcrt.kbhit(), 0) | ||
|
||
def test_getch(self): | ||
msvcrt.ungetch(b'c') | ||
self.assertEqual(msvcrt.getch(), b'c') | ||
|
||
def test_getwch(self): | ||
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): | ||
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') | ||
|
||
def test_putwch(self): | ||
msvcrt.putwch(c) | ||
|
||
|
||
class TestOther(unittest.TestCase): | ||
def test_heap_min(self): | ||
try: | ||
msvcrt.heapmin() | ||
except OSError: | ||
pass | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Tests/2023-09-06-22-06-22.gh-issue-108996.IBhR3U.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add tests for ``msvcrt``. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.