Instances of BaseException and family don't provide __module__? - Python

This is a discussion on Instances of BaseException and family don't provide __module__? - Python ; I have started working on a new project using ZSI and perhaps one can argue this is a bug in ZSI, but I found it odd. The ZSI dispatcher needs to catch all exceptions and pass that over to the ...

+ Reply to Thread
Results 1 to 2 of 2

Instances of BaseException and family don't provide __module__?

  1. Default Instances of BaseException and family don't provide __module__?

    I have started working on a new project using ZSI and perhaps one can
    argue this is a bug in ZSI, but I found it odd. The ZSI dispatcher
    needs to catch all exceptions and pass that over to the client; in
    doing so, it passes along the name of the exception that occurred so
    that the client can know more than just "it failed." Anyways, I am
    getting off the point, the mechanics of this code in ZSI goes more or
    less like this:

    >>> try:
    >>> doSomething()
    >>> except Exception, ex:
    >>> sendFault(':'.join([ex.__module__, ex.__class__.__name__]))


    This works just fine for user-defined exceptions like:

    >>> class UserException(Exception):
    >>> pass


    >>> uex = UserException()
    >>> print ':'.join([uex.__module__, uex.__class__.__name__]) # __main__.UserException


    But falls on its face with built-in exceptions:

    >>> ex = Exception()
    >>> print ':'.join([ex.__module__, ex.__class__.__name__]) # AttributeError!


    , because the built-in exception instances don't have the __module__
    attribute (as of 2.5). The only way this works in 2.4 as well as 2.5
    is to do:

    >>> print ':'.join([ex.__class__.__module__, ex.__class__.__name__]) # exceptions:Exception
    >>> print ':'.join([uex.__class__.__module__, uex.__class__.__name__]) # __main__:NewException


    But this is a bit obscure and I don't understand why user-defined
    exception instances should have a different set of attributes than the
    built-in exception instances. As well as this is a change from 2.4-
    >2.5 that breaks existing code for no apparent reason. This smells

    like it was an overlooked mistake and not a feature. I am tempted to
    open a bug against python for it, but I didn't know if someone could
    give a rational reason why that attribute is missing?

    Thanks,
    -Scott


  2. Default Re: Instances of BaseException and family don't provide __module__?

    On Fri, 02 Nov 2007 19:36:05 -0700, Scott Dial wrote:

    > I have started working on a new project using ZSI and perhaps one can
    > argue this is a bug in ZSI, but I found it odd. The ZSI dispatcher needs
    > to catch all exceptions and pass that over to the client; in doing so,
    > it passes along the name of the exception that occurred so that the
    > client can know more than just "it failed."


    I have no opinion on whether the problem you report is a bug in Python or
    ZSI or not, but I'd suggest that passing along the name of the exception
    is not the right way to go about it.

    The right way is to pass along the exception itself:

    try:
    doSomething()
    except Exception, ex:
    sendFault(ex)
    report_error_and_continue()

    (Always assuming you want to continue.)

    That gives the caller access to as much or as little of the exception as
    it needs.


    --
    Steven.

+ Reply to Thread

Similar Threads

  1. Re: GeneratorExit should derive from BaseException, not Exception
    By Application Development in forum Python
    Replies: 0
    Last Post: 08-21-2007, 09:09 AM
  2. Re: GeneratorExit should derive from BaseException, not Exception
    By Application Development in forum Python
    Replies: 0
    Last Post: 08-21-2007, 02:41 AM
  3. Re: GeneratorExit should derive from BaseException, not Exception
    By Application Development in forum Python
    Replies: 0
    Last Post: 08-21-2007, 02:01 AM
  4. Re: GeneratorExit should derive from BaseException, not Exception
    By Application Development in forum Python
    Replies: 0
    Last Post: 08-20-2007, 10:37 PM
  5. GeneratorExit should derive from BaseException, not Exception
    By Application Development in forum Python
    Replies: 0
    Last Post: 08-20-2007, 05:00 PM