Skip to content

gh-126028: Add more tests for msvcrt module #126029

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

Open
wants to merge 17 commits into
base: main
Choose a base branch
from

Conversation

aisk
Copy link
Member

@aisk aisk commented Oct 27, 2024

aisk and others added 2 commits October 27, 2024 19:19
Co-authored-by: Tomas R. <tomas.roun8@gmail.com>
aisk and others added 3 commits October 28, 2024 14:47
Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

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

LGTM now, I will wait for MS expert to also approve :)

@sobolevn sobolevn requested a review from a team October 28, 2024 08:26
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
Member 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
Member 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.

@aisk
Copy link
Member Author

aisk commented Jul 17, 2025

Updated: I misunderstand the SetErrorMode API, it's mode can be restored after the test. Now this PR is ready for review.

cc @serhiy-storchaka

Bellow comment is wrong: Hi, I found that the [`SetErrorMode`](https://learn.microsoft.com/en-us/windows/win32/api/errhandlingapi/nf-errhandlingapi-seterrormode) actually sets the value as a bit flag to the current state, and `SetErrorMode(0)` sets it to 0.

I have some code like this:

import msvcrt
import unittest


class TestOther(unittest.TestCase):

   def test_SetErrorMode(self):
       origin = msvcrt.GetErrorMode()
       print(f"origin mode: {origin}")
       msvcrt.SetErrorMode(0)
       current_default = msvcrt.GetErrorMode()
       print(f"current default: {current_default}")
       for v in (msvcrt.SEM_FAILCRITICALERRORS, msvcrt.SEM_NOGPFAULTERRORBOX,
                 msvcrt.SEM_NOALIGNMENTFAULTEXCEPT, msvcrt.SEM_NOOPENFILEERRORBOX):
           if origin & v:
               print("set v:", v)
               msvcrt.SetErrorMode(v)
       self.assertEqual(origin, msvcrt.GetErrorMode())


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

It reads the original mode, and sets the current mode to 0, and compares the 4 available bit flags to the original mode, and if it's true, then sets it.

There is the running result:

python .\a.py
origin mode: 32771  (0x1 + 0x2 + 0x8000)
current default: 0
set v: 1 (0x1)
set v: 2 (0x2)
set v: 32768 (0x8000)
F
======================================================================
FAIL: test_SetErrorMode (__main__.TestOther)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\xxxxx\Source\cpython\a.py", line 18, in test_SetErrorMode
    self.assertEqual(origin, msvcrt.GetErrorMode())
AssertionError: 32771 != 32768

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (failures=1)

But the final result is not the same as the original mode. There is a difference of "3", so I guess the bit flags "0x1" and "0x2" don't have any effect.
This leads to us not being able to restore the mode to the original value after we set it in the test case. I have no idea why this happens, maybe there are some limitations in the Windows OS. So should we keep this test?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting merge skip news tests Tests in the Lib/test dir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy