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

Jörgen Stenarson jorgen.stenarson@bostream...
Mon Mar 5 12:39:54 CST 2007


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
> 
> 
> ------------------------------------------------------------------------
> 
> Index: console.py
> ===================================================================
> --- console.py	(revision 2126)
> +++ console.py	(working copy)
> @@ -157,6 +157,11 @@
>                   }
>  from ansi import AnsiState,AnsiWriter
>  
> +def ensure_text(text):
> +    if isinstance(text, unicode):
> +        return text.encode("mbcs")
> +    return text
> +
>  class Console(object):
>      '''Console driver for Windows.
>  
> @@ -366,7 +371,7 @@
>              n += len(chunk)
>              log('attr=%s' % attr)
>              self.SetConsoleTextAttribute(self.hout, attr)
> -            self.WriteConsoleA(self.hout, chunk, len(chunk), byref(junk), None)
> +            self.WriteConsoleA(self.hout, ensure_text(chunk), len(chunk), byref(junk), None)
>          return n
>  
>      def write_color(self, text, attr=None):
> @@ -376,7 +381,7 @@
>              log(str(attr))
>              log(str(chunk))
>              self.SetConsoleTextAttribute(self.hout, attr.winattr)
> -            self.WriteConsoleA(self.hout, chunk, len(chunk), byref(junk), None)
> +            self.WriteConsoleA(self.hout, ensure_text(chunk), len(chunk), byref(junk), None)
>          return n
>  
>      def write_plain(self, text, attr=None):
> @@ -386,7 +391,7 @@
>              attr = self.attr
>          n = c_int(0)
>          self.SetConsoleTextAttribute(self.hout, attr)
> -        self.WriteConsoleA(self.hout, text, len(text), byref(n), None)
> +        self.WriteConsoleA(self.hout, ensure_text(text), len(text), byref(n), None)
>          return len(text)
>          
>      if os.environ.has_key("EMACS"):
> 

I think two issues more must be considered here.

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?

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.

/Jörgen






More information about the IPython-user mailing list