Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #102694

Re: coroutine, throw, yield, call-stack and exception handling

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Ian Kelly <ian.g.kelly@gmail.com>
Newsgroups comp.lang.python
Subject Re: coroutine, throw, yield, call-stack and exception handling
Date Mon, 8 Feb 2016 14:56:55 -0700
Lines 70
Message-ID <mailman.112.1454968664.2317.python-list@python.org> (permalink)
References <n99m93$lc7$1@dont-email.me>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
X-Trace news.uni-berlin.de qM9aGWFdH3a1GlGXJ6UVCQXffnX6khqTzjEzslK8+S4w==
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'exit': 0.07; 'method,': 0.07; 'appropriate.': 0.09; 'handled.': 0.09; 'handler.': 0.09; 'lost.': 0.09; 'result)': 0.09; 'example:': 0.10; 'thread': 0.10; 'exception': 0.13; 'def': 0.13; '2016': 0.16; 'expression.': 0.16; 'originate': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:exception': 0.16; 'subject:handling': 0.16; 'subject:yield': 0.16; 'surprising': 0.16; 'true:': 0.16; 'wrote:': 0.16; 'passes': 0.18; 'try:': 0.18; 'exceptions': 0.22; 'am,': 0.23; 'code.': 0.23; 'feb': 0.23; 'passing': 0.23; 'this:': 0.23; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; 'example': 0.26; 'separate': 0.27; 'question': 0.27; 'message-id:@mail.gmail.com': 0.27; 'executing': 0.27; 'object,': 0.27; 'yield': 0.27; 'raise': 0.29; 'call.': 0.30; 'returned': 0.32; "he's": 0.33; 'raised': 0.33; 'surely': 0.33; 'values.': 0.33; 'similar': 0.33; 'except': 0.34; 'handle': 0.34; 'received:google.com': 0.35; 'next': 0.35; 'execution': 0.35; 'skip:* 20': 0.35; 'should': 0.36; 'instead': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'method': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'mean': 0.38; 'does': 0.39; 'subject:-': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'called': 0.40; 'some': 0.40; 'forget': 0.60; 'save': 0.60; 'care': 0.60; "you'll": 0.61; 'above,': 0.63; 'safe': 0.63; 'manner': 0.69; '8bit%:21': 0.70; 'to:name:python': 0.84; 'yielded': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; bh=EzxYcsM/ZhMYRWTSwJqfBlON5bVXlZBOjJ79VCJ8z2g=; b=iQRyP/KaMKKB2H2eydyAD/U/lt2AXjXbjRYRmLAvm8JuybX4NfjYd17Bs9gpliOQ5P vzmX5C4UOOo+BcZAYn5EvndLTgcOdHL0IpnuMRKo7m3UPU8fSeQosDadL6j0I3QIbGIq hO8eeLtPQqOBWgeIeAtgN2fVUGrG/++Xv13VOaH2mv5DDgsWN+TcqpOBsNMRBks5VqMp hkbwerAVSaWdFzCVD2b1yhdKsFwWbZq/2cjFwCoazMj+8j2jhTwqX9Bm6EJ+TsdCF0pk qB+ghcC5WtcWPwhxNpUkzQUgp8oSfY2D/wxxdC2T30Zjk7yUYsW1nWJooc2VmNAGBo7q jq8A==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:content-type:content-transfer-encoding; bh=EzxYcsM/ZhMYRWTSwJqfBlON5bVXlZBOjJ79VCJ8z2g=; b=mGJwFpiEB1fXY3sKNbNggaQ8WGaWIORlHCHgl02Lb4tpxj1EKqhAGQnR2cSdvDq/Jx u0DHd8ijmdmQ6BPsuflxiGp1o+6p1qS0gEszV2e1Am30FVzANv9F8RdYRKdIPsl4p5Fc 5Up26uu6KXAewriE20OO1qnFt1kBgQN2tc2zG0zkdmg1zKB2kp901xj3wlf4t5/zSte5 MJsN+qE0dUZ0B2z3nYlM9uCFN5wKkDq/wt0n70Ny2FOcgWI8PSF7Rs3oUz2rwo6qScT3 bRI8A1P9L/TiVrG88OwF+amUEzPpitj+Wo7NJGBBvEZJ9k1AYloSkjuiQN7EnAFl+1wG X+iQ==
X-Gm-Message-State AG10YOS0qXttL85nPmd5KJCETsPvDr5njuVYwOVMBoHDaX/fPAHJqbHPp8CwN74vjCQdF5iQGzMyK0O5yOezVg==
X-Received by 10.50.131.201 with SMTP id oo9mr1134192igb.68.1454968655163; Mon, 08 Feb 2016 13:57:35 -0800 (PST)
In-Reply-To <n99m93$lc7$1@dont-email.me>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21rc2
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:102694

Show key headers only | View raw


On Mon, Feb 8, 2016 at 2:17 AM, Veek. M <vek.m1234@gmail.com> wrote:
> ****************************
> Exceptions can be raised inside a coroutine using the throw(
>
> Exceptions raised in this manner will originate at the currently
> executing yield state-ment in the coroutine.A coroutine can elect to
> catch exceptions and handle them as appropriate. It is not safe to use
> throw() as an asynchronous signal to a coroutine—it should never be
> invoked from a separate execution thread or in a signal handler.
> ****************************
>
> What does Beazley mean by this: 'will originate at the currently
> executing yield state-ment in the coroutine'
>
> If he's throw'ing an exception surely it originates at the throw:
>
> def mycoroutine():
>  while len(n) > 2:
>    n = (yield)
>
>  throw('RuntimeError' "die!")

The "throw" is not called from inside the coroutine. It's a method of
the generator object, and it's used by the calling code. It's similar
to calling the send method, except that instead of passing a value to
be returned by the yield expression, it passes an exception to be
raised inside the coroutine at the yield expression.

Example:

def mycoroutine():
  n = 0
  while True:
    try:
      n = (yield n)
    except SomeException:
      n = 42

coro = mycoroutine()
coro.next()
for i in range(100):
  if i % 6 == 0:
    coro.send(i % 6)
  else:
    coro.throw(SomeException())


> Also this bit:
> ***********************
> If a coroutine returns values, some care is required if exceptions
> raised with throw() are being handled. If you raise an exception in a
> coroutine using throw(), the value passed to the next yield in the
> coroutine will be returned as the result of throw(). If
> you need this value and forget to save it, it will be lost.
> ***********************
>
> def coroutine():
>   while True:
>    line = (yield result)
>
>   throw(FooException)
>
> where is the question of a 'yield'? You'll exit the coroutine straight
> away..

Taking my example from above, after SomeException is caught, the next
value yielded inside the coroutine will be the return value of the
coro.throw() call. This may be surprising if you're only expecting
coro.send() and not coro.throw() to return yielded values.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

coroutine, throw, yield, call-stack and exception handling "Veek. M" <vek.m1234@gmail.com> - 2016-02-08 14:47 +0530
  Re: coroutine, throw, yield, call-stack and exception handling "Veek. M" <vek.m1234@gmail.com> - 2016-02-08 15:04 +0530
  Re: coroutine, throw, yield, call-stack and exception handling Ian Kelly <ian.g.kelly@gmail.com> - 2016-02-08 14:56 -0700
    Re: coroutine, throw, yield, call-stack and exception handling "Veek. M" <vek.m1234@gmail.com> - 2016-02-09 17:25 +0530

csiph-web