Skip to content

gh-90872: Use strict unsigned long conversion for DWORD values - #32081

Open
jkloth wants to merge 1 commit into
python:mainfrom
jkloth:issue46716_dword
Open

gh-90872: Use strict unsigned long conversion for DWORD values#32081
jkloth wants to merge 1 commit into
python:mainfrom
jkloth:issue46716_dword

Conversation

@jkloth

@jkloth jkloth commented Mar 23, 2022

Copy link
Copy Markdown
Contributor

The second part of the fix for negative timeouts.

This PR should not be backported for the reasons I state here: #32079 (comment)
To further emphasize this, see the additional change required in this PR for use of -1 to denote INFINITE.

https://bugs.python.org/issue46716

@eryksun

eryksun commented Mar 24, 2022

Copy link
Copy Markdown
Contributor

Maybe we should start to gradually add test cases for _winapi in "Lib/test/test_os.py", even if it's only an internal module. For example:

@unittest.skipUnless(sys.platform == "win32", "Win32 specific tests")
class Win32APITests(unittest.TestCase):

    def test_dword_convert_negative(self):
        with self.assertRaises(ValueError):
            for h in _winapi.CreatePipe(None, -1):
                _winapi.CloseHandle(h)

    def test_dword_convert_overflow(self):
        with self.assertRaises(OverflowError):
            for h in _winapi.CreatePipe(None, 1 << 32):
                _winapi.CloseHandle(h)

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The DWORD converter is also used in winreg.c and overlapped.c.

Comment thread Modules/_winapi.c
Comment on lines +212 to +213
class DWORD_converter(unsigned_long_converter):
type = 'DWORD'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You can use DWORD(bitwise=True) instead of DWORD in places where you want to keep the old behavior.

Or you can define non-strict by default completely compatible converter:

class DWORD_converter(unsigned_long_converter):
    type = 'DWORD'

    def converter_init(self, *, bitwise: bool = True) -> None:
        super().converter_init(bitwise=bitwise)

and use DWORD(bitwise=False) if you need a strict conversion.

@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 10, 2026
@serhiy-storchaka

Copy link
Copy Markdown
Member

winreg.c and overlapped.c were converted to the strict DWORD converter in the meantime (GH-94513 and GH-94516), so _winapi is now the only module which uses the bitwise one.

But the strict converter also rejects negative values, and this contradicts gh-132629: out of range values for B, H, I, k and K are now deprecated, while small negative values are still accepted intentionally, because passing -1 instead of 0xffffffff is convenient. The change in win_utils.py in this PR is an example -- WaitForSingleObject(handle, -1) works now and would start failing.

So the question is whether _winapi should reject negative timeouts, or keep k and only get the deprecation warning for values which are out of range for both signed and unsigned long.

@github-actions github-actions Bot removed the stale Stale PR or inactive for long period of time. label Aug 12, 2026
@serhiy-storchaka serhiy-storchaka changed the title bpo-46716: Use strict unsigned long conversion for DWORD values gh-90872: Use strict unsigned long conversion for DWORD values Aug 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants