user_encoding fix

Alexander Belchenko bialix at ukr.net
Mon Feb 20 10:42:39 GMT 2006


Nir Soffer пишет:
>> Exactly. Using always bzrlib.user_encoding instead of 
>> sys.stdout.encoding if it present will break windows compatibility (as 
>> I explain earlier, russian windows have sys.stdout.encoding == 'cp866' 
>> and bzrlib.user_encoding is 'cp1251').
> 
> 
> So on Windows it should use sys.stdout.encoding, but on Mac OS X it 
> should use user_encoding. What is the  correct check for windows in this 
> case? os.name == nt or sys.platform?

No. You're wrong. On windows (and on other system too, I think) we
should check if sys.stdout have encoding attribute and this attribute
not None and not 'ascii' and then use it. Otherwise, we should use
bzrlib.user_encoding. Because when I run bzr from editor that grab their
output then sys.stdout is PIPE and does not have encoding attribute at all.

So, I think right code should be as following:

if getattr(sys.stdout, "encoding", "ascii") not in (None, "ascii"):
	encoding = sys.stdout.encoding
else:
	encoding = bzrlib.user_encoding

And later use this encoding variable for encoding of unicode strings.

> Since its another os specific issue, maybe move it into osutils?

I don't think that this should be OS-specific.

But if you want to check explicitly for windows, use conditional
expression: sys.platform == "win32"

It's better than os.name == "nt". I think "win32" string more readable
and more informative.

--
Alexander





More information about the bazaar mailing list