[Tutor] sys.getfilesystemencoding()

eryksun eryksun at gmail.com
Fri Dec 21 05:37:49 CET 2012


On Thu, Dec 20, 2012 at 1:01 PM, Albert-Jan Roskam <fomcl at yahoo.com> wrote:
>
>> You shouldn't encode a string argument you've declared as c_wchar_p
>> (i.e. wintypes.LPCWSTR, i.e. type 'Z').
>
> Ok, yes, that was plain stupid of me.

On Windows, CPython 3.3 presents a twist. sizeof(c_wchar) is 2, but
sys.maxunicode is 1114111. In general you'd have to test for this case
and add the number of surrogates (i.e. characters with ord > U+FFFF)
to the length. However, since ctypes.c_wchar_p (i.e. wintypes.LPCWSTR)
creates a null-terminated buffer (16-bit NUL, U+0000), you can set
cchWideChar = -1. Here's what MSDN says:

    If this parameter is -1, the function processes the entire input string,
    including the terminating null character. Therefore, the resulting
    character string has a terminating null character, and the length
    returned by the function includes this character.

Including the null in lpMultiByteStr is fine. The "value" descriptor
of the c_char array stops at the first null byte when creating a
Python string (bytes in 3.x). To get the whole string use the "raw"
descriptor.

>>     import ctypes
>>     from ctypes import wintypes
>
> As per PEP8, the only time I use from x import * is with ctypes. Don't you
> do this because of name clashes with wintypes?

I see no compelling reason to repeatedly type ctypes.wintypes instead
of just wintypes. As far as import * goes, wintypes doesn't override
ctypes. Mostly it defines type aliases and structures.

http://hg.python.org/cpython/file/8803c3d61da2/Lib/ctypes/wintypes.py


More information about the Tutor mailing list