[IPython-user] IPython Color help on windows cmd shell

Thomas Heller theller@ctypes....
Wed Mar 7 03:47:50 CST 2007

Jörgen Stenarson schrieb:
> Thomas Heller skrev:
>> Robert Kern schrieb:
>>> Fernando Perez wrote:
>>>> On 3/2/07, Thomas Heller <theller@ctypes.org> wrote:
>>>>> In [1]: class X(object):
>>>>>    ...:     u"the doc string"
>>>>>    ...:
>>>>>    ...:
>>>>> In [2]: x = X()
>>>>> In [3]: x??
>>>>> T y p eX B a s e   C < c l a s s   ' _ _ mS t r i n g    < _ _ m a i n _ _ . X   o b j e cN a m e s pI n t e r a D o c s
>>>>>  t r i n g   [ s o u r c e   f        t h e   d o
>>>> Mmh, very odd.  I don't see that at all under Linux:
>>>> I know so little about windows that I honestly don't have a clue as to
>>>> what the problem could be.  Any suggestions?
>>> Various (GUI terminal, font) combinations, on all platforms, want to render
>>> "Unicode" characters as double-width.
>> Yes, and pyreadline/console.py should convert unicode to byte strings before
>> rendering them.  There are two possibilities to fix this with ctypes:
>> - The first one is to explicitely convert unicode strings.  The attached patch
>>   does that for the WriteConsoleA function.
>> - The second one is to let ctypes do it automatically by setting the .argtypes
>>   attribute on the functions that need to handle text; in this case ctypes
>>   automatically convert unicode/byte strings.
>> Thomas
[Patch deleted]
> I think two issues more must be considered here.

These are valid points, imo.
> What should happen if the encoding does not work? Should offending 
> characters be rendered as ? or should pyreadline raise an exception? 
> What do you think?

In interactive mode, rendering them as ? is probably the best that can be done.

> The second issue is which encoding should be used? I not sure mbcs is 
> the best, as I understand things it is connected to the filesystem 
> encoding and not the encoding used by the console. These are in general 
> not the same. For me (using a swedish windows machine) the default 
> codepage for a console is cp850, though I usually use 1252.

Sure.  Probably one should use sys.stdout.encoding:

Python 2.4.4 (#71, Oct 18 2006, 08:34:43) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.stdout.encoding
>>> sys.stdin.encoding
>>> sys.stderr.encoding

> /Jörgen

Hm, I forgot to mention a third possibility, which would hopefully also
solve all the issues mentioned above:  Why not use WriteConsoleW instead
of WriteConsoleA when a unicode string has to be printed?  This way no
conversion at all would be needed.


More information about the IPython-user mailing list