Conversion between Python and C integers

In my TODO list, I have an item for a few months. On Windows, the subprocess module seems to hang forever if a negative timeout is passed, whereas it should behave as sleep(0): non-blocking call. For example, if you pass -1e9 second, it’s rounded to -1 ms which becomes 0xFFFF_FFFF and WaitForSingleObject() treats this value as INFINITE (wait forever).

The WaitForSingleObject() function is wrapped in Modules/_winapi.c which uses Argument Clinic with milliseconds: DWORD. And DWORD is defined in Argument Clinic as: create_converter('DWORD', 'k') # F_DWORD is always "k" (which is much shorter).

In short, PyArg_Parse() with k format (C unsigned long) converts silently negative numbers to positive numbers (wrap) which can lead to such surprising behavior. Is it a bug? A feature? I let you decide :slight_smile:

See also Rationale for non-overflow checking format codes in PyArg_Parse* discussion.

2 Likes