FrontPage 

Fuego wiki

Login or create account

Issue 0083 in 'raw' format

; Summary: unicode exception handling on Jenkins error
; Owner: Tim
; Reporter: Laszlo Sitzer
; Status: closed
; Priority: medium
; Version: 1.5
; Subsystem: ftc
; Tags: python, unicode, exception

= Description =
If Jenkins returns an error, you get an exception.  While printing the
exception, then you can get a second exception because of the use of 
str(e), which throws a UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position xxxx: ordinal not in range(128)

Here is the exception:
{{{#!YellowBox
# ftc add-nodes -b bbb
Traceback (most recent call last):
  File "/usr/local/bin/ftc", line 5625, in <module>
    main()
  File "/usr/local/bin/ftc", line 5553, in main
    sys.exit(str(e) + '\n' + command_help['add-nodes'][1])
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in
position 4199: ordinal not in range(128)
}}}

The problem is that the exception comes from the jenkins module, which
uses str() on the text returned from the web server, which apparently
has a unicode '\xa0' character in it.  (Unicode a0 is a non-breaking space)

I'd like to print out the text returned from the server, but I
don't know how to query the exception for it, without the server
trying to convert the text using the str() function.

If the default encoding for python is 'utf-8', instead of ascii, then 
things work better.  I was able to work around this by doing:

{{{#YellowBox
import sys
reload(sys)
sys.setdefaultencoding('utf8')
}}}

but that is apparently an unnapproved way of handling this.  The reload
is required because the attribute 'setdefaultencoding' is used during 
sys import, but then deleted before the module becomes active.

----
Another possible solution is putting this at the top of the file:

{{{#YellowBox
#coding: utf-8
}}}

OK - that doesn't work.

----
Another method to do this would be to make a safe_str_and_exit()
which should only be called on an error path.

This would do the reload(sys) and sys.setdefaultencoding('utf8'),
but only at error time, so that ftc doesn't have to suffer the overhead
of these operations in the non-error case.


= Notes =

Fixed with commit 600c12ff - using the safe_str_and_exit routine

; backlink: [[Fuego Issues List]]










TBWiki engine 1.8.3 by Tim Bird