Eryk Sun added the comment:

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')
    -27590656
    >>> def ValidHandle(value):
    ...     if value == 0:
    ...         raise WinError()
    ...     return value
    ...
    >>> kernel32.GetModuleHandleA.restype = ValidHandle
    >>> kernel32.GetModuleHandleA(b'advapi32')
    -27590656

    >>> hex(kernel32.GetModuleHandleA(b'advapi32') & 2**32-1)
    '0xfe5b0000'
    >>> kernel32.GetModuleHandleA.restype = c_void_p 
    >>> hex(kernel32.GetModuleHandleA(b'advapi32'))          
    '0x7fefe5b0000'

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue23606>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to