error messages containing unicode

Jim jhefferon at smcvt.edu
Mon Jan 29 21:01:56 EST 2007


Hello,

I'm trying to write exception-handling code that is OK in the 
presence
of unicode error messages.  I seem to have gotten all mixed up and 
I'd
appreciate any un-mixing that anyone can give me.

I'm used to writing code like this.

  class myException(Exception):
      pass

  fn='README'
  try:
      f=open(fn,'r')
  except Exception, err:
      mesg='unable to open file'+fn+': '+str(err)
      raise myException, mesg

But what if fn is non-ascii?  The following code raises the
dreaded (to me) UnicodeEncodeError.

  class myException(Exception):
      pass

  def fcnCall():
      fn=u'a\N{LATIN SMALL LETTER O WITH DIAERESIS}k'
      try:
          # was: f=open(fn,'r')
          2/0  #  just substitute something that will raise an 
exception
      except Exception, err:
          mesg='unable to open file '+fn+': '+str(err)
          raise myException, mesg

  try:
      fcnCall()
  except Exception, err:
      print 'trouble calling fcnCall: '+str(err)

Maybe my trouble is the "str()", which is supposed to return a 
regular
string?  (BTW, unicode() makes no difference, and help(BaseException)
didn't give me any inspirations.)  So I looked for an end-around past 
the
str() call.

As I understand lib/module-exceptions.html, "For class
exceptions, [err] receives the exception instance. If the exception
class is derived from the standard root class BaseException, the
associated value is present as the exception instance's args
attribute.", I should be able to get the string out of err.args. Sure
enough, putting the above text into test.py and changing str(err)
to repr(err.args) yields this.

  $ python test.py
  trouble calling fcnCall: (u'unable to open file a\xf6k: integer 
division or modulo by zero',)

so that changing the above repr(err.args) to err.args[0] gives the
desired result.

  $ python test.py
  trouble calling fcnCall: unable to open file aök: integer division 
or modulo by zero

(In case this doesn't show up as I intended on your screen, I see an
o with a diaeresis in the filename.)

But the documentation "This may be a string or a tuple containing 
several
items of information (e.g., an error code and a string explaining the
code)." gives me no reason to believe that all exceptions have the 
desired
unicode string as the 0-th element of the tuple.  I confess that I'm 
unable
to confidently read exceptions.c .

No doubt I've missed something (I googled around the net and on this 
group
but I didn't have any luck).  I'd be grateful if someone could show 
me
striaght.

Thanks,
Jim




More information about the Python-list mailing list