UnicodeDecode error while printing line (#539258)

Alexander Belchenko bialix at ukr.net
Tue Mar 16 08:27:29 GMT 2010


Alexander Belchenko пишет:
> Parth Malwankar пишет:
>> Hello,
>>
>> I am seeing a UnicodeDecodeError[1] in bzr-grep while
>> writing to terminal.
>>
>>    File "/home/parthm/.bazaar/plugins/grep/__init__.py", line 144, in run
>>      verbose, self.outf)
>>    File "/home/parthm/.bazaar/plugins/grep/grep.py", line 107, in
>> workingtree_grep
>>      include, exclude, verbose, outf, path_prefix)
>>    File "/home/parthm/.bazaar/plugins/grep/grep.py", line 146, in 
>> dir_grep
>>      print_revno, include, exclude, verbose, outf, path_prefix)
>>    File "/home/parthm/.bazaar/plugins/grep/grep.py", line 240, in 
>> _file_grep
>>      outf.write(pfmt % (line,))
>>  UnicodeDecodeError: 'ascii' codec can't decode byte 0xa4 in position
>> 7: ordinal not in range(128)
>>
>> What is the suggested way to fix this? outf above is the instance 
>> attribute
>> for the grep command derived from the Command class.
> 
>  From Command class help:
> 
>     encoding_type
>         Command objects will get a 'outf' attribute, which has been
>         setup to properly handle encoding of unicode strings.
>         encoding_type determines what will happen when characters cannot
>         be encoded
>             strict - abort if we cannot decode
>             replace - put in a bogus character (typically '?')
>             exact - do not encode sys.stdout
> 
>             NOTE: by default on Windows, sys.stdout is opened as a text
>             stream, therefore LF line-endings are converted to CRLF.
>             When a command uses encoding_type = 'exact', then
>             sys.stdout is forced to be a binary stream, and line-endings
>             will not mangled.
> 
> To write lines of the versioned files you need to use
> encoding_type = 'exact'
> 
> But for writing [possible] unicode text to the terminal you need to use 
> 'replace' in most situations.
> 
> So without looking at the code in your plugin I'd say you need 'exact'.
> 
> But if you need to use also unicode output then you need both 'exact' 
> and 'replace'. This can be done although slightly non-trivial.
> 
> 2 Martin Pool: I think UI class should provide both encoded and raw 
> output stream. I'm not sure is it already done, so excuse me if this is 
> already exists.

OK, IIUC this is now done via 
UIFactory.make_output_stream(encoding_type='exact')




More information about the bazaar mailing list