Python: Uitzondering in de afzonderlijke module werkt fout

Ik heb een nieuwe uitzonderingsklasse gemaakt en ik geef hem graag de weergave van fouten zoals in OSError klasse. Dit is wat ik wil:

>>> raise(MyError(1, 'info'))
MyError: [Errno 1] predefined text: info

Wat moet ik doen? Kan ik dit doen als ik ervaar van de basisklasse Uitzondering? Dit is wat ik heb geprobeerd (voorbeeld van module voor werk met gnulib):

class GNULibError(Exception):
  '''Exception handler for GNULib classes.'''

  def __init__(self, errno, errinfo=None):
    '''Each error has following parameters:
    errno: code of error; used to catch error type
      1: destination directory does not exist: 
      2: configure file does not exist: 
      3: selected module does not exist: 
      4:  is expected to contain gl_M4_BASE([m4base])
      5: missing sourcebase argument
      6: missing docbase argument
      7: missing testsbase argument
      8: missing libname argument
    errinfo: additional info'''
    self.errno = errno; self.errinfo = errinfo
    self.args = (self.errno, self.errinfo)

  def __str__(self):
    errors = \
    [ # Begin list of errors
      "destination directory does not exist: %s" % self.errinfo,
      "configure file does not exist: %s" % self.errinfo,
      "selected module does not exist: %s" % self.errinfo,
      "%s is expected to contain gl_M4_BASE([%s])" % \
        (os.path.join(self.errinfo, 'gnulib-comp.m4'), self.errinfo),
      "missing sourcebase argument; cache file doesn't contain it,"
        +" so you might have to set this argument",
      "missing docbase argument; you might have to create GNULibImport" \
        +" instance with mode 0 and docbase argument",
      "missing testsbase argument; cache file doesn't contain it,"
        +" so you might have to set this argument"
      "missing libname argument; cache file doesn't contain it,"
        +" so you might have to set this argument",
      "dependencies and testflag 'default' cannot be used together",
    ] # Complete list of errors
    if not PYTHON3:
      self.message = (b'[Errno %d] %s' % \
        (self.errno, errors[self.errno -1].encode(ENCS['default'])))
    else: # if PYTHON3
      self.message = ('[Errno %d] %s' % \
        (self.errno, errors[self.errno -1]))
    return(self.message)

Het werkt fout en retourneert alleen de foutnaam voor Python 2 en lege tekenreeks voor Python 3. Hoe krijg ik dergelijk gedrag als ik wil? Bedankt!

0

1 antwoord

U moet __repr__-methode implementeren in plaats van __str__

http://docs.python.org/reference/datamodel.html#object.__repr__

Dat zal werken:

 class GNULibError(Exception):
  '''Exception handler for GNULib classes.'''

  def __init__(self, errno, errinfo=None):
    '''Each error has following parameters:
    errno: code of error; used to catch error type
      1: destination directory does not exist: 
      2: configure file does not exist: 
      3: selected module does not exist: 
      4:  is expected to contain gl_M4_BASE([m4base])
      5: missing sourcebase argument
      6: missing docbase argument
      7: missing testsbase argument
      8: missing libname argument
    errinfo: additional info'''
    self.errno = errno; self.errinfo = errinfo
    self.args = (self.errno, self.errinfo)

  def __repr__(self):
    errors = \
    [ # Begin list of errors
      "destination directory does not exist: %s" % self.errinfo,
      "configure file does not exist: %s" % self.errinfo,
      "selected module does not exist: %s" % self.errinfo,
      "%s is expected to contain gl_M4_BASE([%s])" % \
        (os.path.join(self.errinfo, 'gnulib-comp.m4'), self.errinfo),
      "missing sourcebase argument; cache file doesn't contain it,"
        +" so you might have to set this argument",
      "missing docbase argument; you might have to create GNULibImport" \
        +" instance with mode 0 and docbase argument",
      "missing testsbase argument; cache file doesn't contain it,"
        +" so you might have to set this argument"
      "missing libname argument; cache file doesn't contain it,"
        +" so you might have to set this argument",
      "dependencies and testflag 'default' cannot be used together",
    ] # Complete list of errors
    if not PYTHON3:
      self.message = (b'[Errno %d] %s' % \
        (self.errno, errors[self.errno -1].encode(ENCS['default'])))
    else: # if PYTHON3
      self.message = ('[Errno %d] %s' % \
        (self.errno, errors[self.errno -1]))
    return(self.message)
1
toegevoegd