Author eryksun
Recipients amaury.forgeotdarc, belopolsky, cgohlke, eryksun, meador.inge, pitrou, python-dev, steve.dower, tim.golden, zach.ware
Date 2016-03-12.22:39:30
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
I occasionally come across code snippets on Stack Overflow, and projects such as win-unicode-console (IIRC), that use ctypes to work with C stdio FILE streams (sometimes for dubious reasons, such as a DLL API that uses FILE streams). Maybe the _ctypes extension module could provide void pointers for the current C stdin, stdout, and stderr -- as well as stdio functions such as fflush, fopen, and freopen. This is already done with _ctypes._memmove_addr and _ctypes._memset_addr. However, getting the current standard stream pointers would need to use a callable or descriptor. 

> it'd be good content for a "Best practices" section 

The tutorial itself is outdated in places and doesn't promote best practices. For example, it assigns a ValidHandle function to windll.kernel32.GetModuleHandleA.restype, which would affect every module that uses windll.kernel32. Also, this ValidHandle example is bogus, as is every example in the tutorial that uses GetModuleHandle without setting restype to a pointer type such as c_void_p. It's truncating 64-bit pointers to 32-bit int values. You just need to try a DLL that loads at a high address:

    >>> kernel32.GetModuleHandleA(b'advapi32')
    >>> def ValidHandle(value):
    ...     if value == 0:
    ...         raise WinError()
    ...     return value
    >>> kernel32.GetModuleHandleA.restype = ValidHandle
    >>> kernel32.GetModuleHandleA(b'advapi32')

    >>> hex(kernel32.GetModuleHandleA(b'advapi32') & 2**32-1)
    >>> kernel32.GetModuleHandleA.restype = c_void_p 
    >>> hex(kernel32.GetModuleHandleA(b'advapi32'))          
Date User Action Args
2016-03-12 22:39:30eryksunsetrecipients: + eryksun, amaury.forgeotdarc, belopolsky, pitrou, tim.golden, cgohlke, meador.inge, python-dev, zach.ware, steve.dower
2016-03-12 22:39:30eryksunsetmessageid: <>
2016-03-12 22:39:30eryksunlinkissue23606 messages
2016-03-12 22:39:30eryksuncreate