[New-bugs-announce] [issue13213] generator.throw() behavior

Petri Lehtinen report at bugs.python.org
Tue Oct 18 20:16:43 CEST 2011

New submission from Petri Lehtinen <petri at digip.org>:

The documentation of generator.throw() gives this signature:

    generator.throw(type[, value[, traceback]])

Looking at the code, it accepts the following arguments:

  g.throw(ExcType)  --> raise ExcType()

  g.throw(ExcType, None)  --> raise ExcType()

  g.throw(ExcType, None, tb)  --> raise ExcType().with_traceback(tb)

  g.throw(ExcType, instace_of_ExcType)  --> raise instance_of_ExcType

  g.throw(ExcType, instace_of_ExcType, tb)  --> raise instance_of_ExcType.with_traceback(tb)

  g.throw(ExcType, other_value)  --> raise ExcType(other_value)

  g.throw(ExcType, other_value, tb)  --> raise ExcType(other_value).with_traceback(tb)

Up to this point, I think everything is in line with the documentation. But it also accepts the following (now that issue 13188 is fixed):

  g.throw(exc_instance) --> raise exc_instance (preserving the traceback of exc_instance)

  g.throw(exc_instance, None) --> raise exc_instance (preserving the traceback of exc_instance)

  g.throw(exc_instance, None, tb) --> raise exc_instance.with_traceback(tb)

It does not accept these, throwing a TypeError:

  g.throw(exc_instance, some_value_other_than_None)
  g.throw(exc_instance, some_value_other_than_None, tb)
  g.throw(exc_instance, tb)

The documentation is really unclear on throwing existing exception instances with (exc_instance) or (exc_instance, None), and that calling with (type, value) or (type, value, tb) will create a new exception if not isinstance(value, type).

assignee: docs at python
components: Documentation, Interpreter Core
messages: 145848
nosy: docs at python, ezio.melotti, ncoghlan, petri.lehtinen, pitrou
priority: normal
severity: normal
status: open
title: generator.throw() behavior
type: behavior
versions: Python 2.7, Python 3.2, Python 3.3

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list