Re: Help on exceptions (was: Convertion of Unicode to ASCII NIGHTMARE)

2006-04-10 Thread Roger Binns

"Serge Orlov" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED]
> It can be like this. Notice special url, it is pointing to a
> non-existing :) tutorial about why concatenating byte strings with
> unicode strings can produce UnicodeDecodeError

An alternate is to give an error code that people can use to
look up (and users can report).  It also has the advantage
of being language neutral.  You can see this kind of approach
on IBM systems (mainframe, AIX etc) and Oracle.

Roger 


-- 
http://mail.python.org/mailman/listinfo/python-list


Help on exceptions (was: Convertion of Unicode to ASCII NIGHTMARE)

2006-04-10 Thread Serge Orlov
ChaosKCW wrote:

> Ok I get it now. Sorry for the slowness. I have to say as a lover of
> python for its simplicity and clarity, the charatcer set thing has been
> harder than I would have liked to figure out.

I think there is a room for improvement here. In my opinion the message
is too confusing for newbies. It would be easier for them if there is a
mini tutorial available about what's going on with links to other more
broad tutorials (like unicode tutorial). Instead of generic error
--
UnicodeDecodeError: 'ascii' codec can't decode byte 0xa5 in position 0:
ordinal not in range(128)
--

It can be like this. Notice special url, it is pointing to a
non-existing :) tutorial about why concatenating byte strings with
unicode strings can produce UnicodeDecodeError
--
UnicodeDecodeError: 'ascii' codec can't decode byte 0xa5 in position 0:
ordinal
not in range(128)
For additional information about this exception see:
  http://docs.python.org/2.4/exceptions/concat+UnicodeDecodeError+str
--

Here is the sample code how it can be done:

---
extended_help = {
("concat", UnicodeDecodeError, str, unicode):
"http://docs.python.org/2.4/exceptions/concat+UnicodeDecodeError+str";,
("concat", UnicodeDecodeError, unicode, str):
"http://docs.python.org/2.4/exceptions/concat+UnicodeDecodeError+str";
}

def get_more_help(error, key):
if not extended_help.has_key(key):
return
error.reason += "\nFor additional information about this exception
see:\n  "
error.reason += extended_help[key]


def concat(s1,s2):
try:
return s1 + s2
except Exception, e:
key = "concat", e.__class__, type(s1), type(s2)
get_more_help(e, key)
raise

concat(chr(0xA5),unichr(0x5432))

-- 
http://mail.python.org/mailman/listinfo/python-list