[Python-3000] Pre-peps on raise and except changes

Guido van Rossum guido at python.org
Sun Feb 11 05:08:26 CET 2007


Somehow it seems that exceptions keep getting permission to violate
the rules... (E.g. the insistence on a fixed base class is also
considered unpythonic in other contexts.) Maybe it's because they're
"exceptions" ? :-)

Anyway, I believe there's a use case for re-raising an existing
exception with an added traceback. After all the __traceback__
attribute is mutable. Returning the mutated object is acceptable here
because the *dominant* use case is creating and raising an exception
in one go:

  raise FooException(<args>).with_traceback(<tb>)

--Guido

On 2/10/07, Nick Coghlan <ncoghlan at gmail.com> wrote:
> Guido van Rossum wrote:
> > Why don't you want it to mutate the instance?
>
> The recent repeat of the API discussion about list.sort() &
> list.reversed() (mutate instance & return None) vs sorted() and
> reversed() (return new instance).
>
> I'm trying to see why mutating & returning self would be OK here, when
> it's not OK for a list to do the same thing.
>
> An alternate constructor as a class method ducks the question entirely.
>
> Cheers,
> Nick.
>
> >
> > On 2/10/07, Nick Coghlan <ncoghlan at gmail.com> wrote:
> >> Collin Winter wrote:
> >> > I think so. I've already got language ready for the section on using
> >> > BaseException.with_traceback() in the 2->3 raise translations, and
> >> > I'll work up additional language for the transition plan sometime this
> >> > weekend.
> >>
> >> If with_traceback() is an instance method, does it mutate the existing
> >> exception or create a new one?
> >>
> >> To avoid any confusion, perhaps it should instead be a class method
> >> equivalent to the following:
> >>
> >>    @classmethod
> >>    def with_traceback(*args, **kwds):
> >>       cls = args[0]
> >>       tb = args[1]
> >>       args = args[2:]
> >>       exc = cls(*args, **kwds)
> >>       exc.__traceback__ = tb
> >>       return exc
> >>
> >> Usage would look like:
> >>
> >>    raise E.with_traceback(T, V)
> >>
> >>
> >> Cheers,
> >> Nick.
> >>
> >> --
> >> Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
> >> ---------------------------------------------------------------
> >>              http://www.boredomandlaziness.org
> >>
> >
> >
>
>
> --
> Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
> ---------------------------------------------------------------
>              http://www.boredomandlaziness.org
>


-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-3000 mailing list