Skip to content

Exception handling with generators #243

Closed
@pfalcon

Description

@pfalcon

See #242 for background of why generators are important.

In vm.c, there's a comment:

                // re-raise exception to higher level
                // TODO what to do if this is a generator??
                nlr_jump(nlr.ret_val);

And indeed, it's rather unclear how to do exception handling with generators.

First of all, we have 2 directions of exception flow:

  1. Exception occurring in generator should be propagated back to caller. Fortunately, throwing exception out of generator terminates it, just the same as returning value from it. (Don't have formal reference handy, but here's: http://stackoverflow.com/questions/11366064/handle-an-exception-thrown-in-a-generator , and it makes perfect sense: normal exceptions are not restartable, so why exceptions in generators would be?)
  2. Exceptions can be injected into generator using generator.throw(). Within generator, they are raised at current yield point.

Then with Py3.3 and yield from, we apparently have not just 2 contexts of "main" and "generator", but stack of generator contexts. Intuitively, these context are higher-order ones than try/except contexts. Within generator, there can be number of enclosing try blocks, and only when exception escapes from outermost block, we have generator context switch, and need to marshal exception.

Metadata

Metadata

Assignees

No one assigned

    Labels

    rfcRequest for Comment

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy