Path: csiph.com!feeder.erje.net!2.eu.feeder.erje.net!newsfeed0.kamp.net!newsfeed.kamp.net!fu-berlin.de!uni-berlin.de!not-for-mail From: Oscar Benjamin Newsgroups: comp.lang.python Subject: Re: yield in try/finally case Date: Thu, 3 Mar 2016 12:13:36 +0000 Lines: 57 Message-ID: References: <84965b86-819b-4924-bca9-e82eed040606@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de NUsXyG1mOr1ybcfl1mK7OAtufhlm8GHeGt66o9DtTMFw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'context': 0.05; 'finally:': 0.05; 'clause': 0.07; 'cc:addr:python-list': 0.09; 'block.': 0.09; 'called.': 0.09; 'restriction': 0.09; 'python': 0.10; 'def': 0.13; '2016': 0.16; 'cc:name:python list': 0.16; 'closed:': 0.16; 'construct.': 0.16; 'executed;': 0.16; 'meanwhile,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:case': 0.16; 'subject:yield': 0.16; 'wrote:': 0.16; 'later': 0.16; 'case.': 0.18; 'try:': 0.18; 'version.': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'pass': 0.22; 'header :In-Reply-To:1': 0.24; 'message-id:@mail.gmail.com': 0.27; 'yield': 0.27; 'skip:u 20': 0.28; 'url:peps': 0.29; 'subject:/': 0.30; 'code': 0.30; 'implement': 0.32; 'statement': 0.32; 'problem': 0.33; 'url:python': 0.33; 'common': 0.33; 'date.': 0.33; 'file': 0.34; 'received:google.com': 0.35; 'url:dev': 0.35; 'quite': 0.35; 'but': 0.36; 'too': 0.36; 'there': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'method': 0.37; 'itself': 0.38; 'received:209': 0.38; 'anything': 0.38; 'means': 0.39; 'still': 0.40; 'some': 0.40; 'ever': 0.60; 'information': 0.63; 'march': 0.64; 'difficulty': 0.66; 'guaranteed': 0.67; 'finally': 0.70; 'construct': 0.84; 'oscar': 0.84; 'subject:try': 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 :cc:content-transfer-encoding; bh=h/jRYaJBWWW4Ic/Sa+RtXAqIK37mNW+iVK+28iSNa4o=; b=iTOW20WvhXJYZbV3T56hZJte2gqwHeK7nbhDxgr4/OFDI3cVnsBJKfxzqQNHzNen5D V8894KYwI6RYCjnfDJ3JN2V1bFghTz08kjnXDK/5egsYaZAqLVZTHXdL5eWZem7SNBwM 31nRMDSlw0353h+nuyGQWf/9j4xWWC5Wqz3xQncA3DoOA9OBn7qBlNlnvLgSr1uKfF/e lXwrPBK9dVKWx9uFPaNTC/Tsj8fo90LdDIv3/esE+LXylE+6pR6ItA3qX63VVVCLOqjh zl910a7Pb9PSjmJK5evaut8UjTNiuoc0cNLFdlYefxaa9iLfjWgMMm2U/3pRQNdhc25k 1bmA== 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:cc:content-transfer-encoding; bh=h/jRYaJBWWW4Ic/Sa+RtXAqIK37mNW+iVK+28iSNa4o=; b=QxqU0O0DIzlyqTLi3GLEyOMRciCuQAKGvEcEnVEeufSFlYfBa7NW5fMNrhOyoGDU8U 5luuBRfKMuazUFEjWuKVrFfeAaQsVZwd7RrFcIZszGH+R6XV7Qd/UGbsQSMed142nlBm 4e5FEdk3ELhtaPs/5CUI9pgbb3gUkofUmlcutbjT5f3jKEHslu2o+d8P0PyC+lPxaabl jKi+cUaSUwsFURuYywHMIkazabLOD9jEtRE2j0G/ZMi5EGhEXcNmFy+KpOulJvLRh9iJ E5e/R1FO6/zgcU9dTg+JqrW/sSF+ahcjemVgIH2LoXo03/4YojS9klAJdjhss4F7IOEY 6w+A== X-Gm-Message-State: AD7BkJKax1A1TVcZlgT3gLW6vkwwlmiUHkedN7pom1nailsfAMeCY9JBpz/aBs9vkL3v0iulBTIncguyuvCVcQ== X-Received: by 10.25.19.151 with SMTP id 23mr1002887lft.126.1457007236418; Thu, 03 Mar 2016 04:13:56 -0800 (PST) In-Reply-To: <84965b86-819b-4924-bca9-e82eed040606@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:103960 On 3 March 2016 at 11:52, =E5=88=98=E7=90=A6=E5=B8=86 w= rote: > > "A yield statement is not allowed in the try clause of a try/finally cons= truct. The difficulty is that there's no guarantee the generator will ever= be resumed, hence no guarantee that the finally block will ever get execut= ed; that's too much a violation of finally's purpose to bear." from https:/= /www.python.org/dev/peps/pep-0255/ > > But, meanwhile, the code showed on that page use yield in a try/finally c= ase. > It really puzzles me. Is there anything wrong? I think what it means is that you can put a yield in the finally block but not the try block so: # Not allowed def f(): try: yield 1 finally: pass # Allowed def f(): try: pass finally: yield 1 However that information is out of date. The restriction was removed in some later Python version. Actually the construct is quite common when using generator functions to implement context managers: @contextlib.contextmanager def replace_stdin(newstdin): oldstdin =3D sys.stdin try: sys.stdin =3D newstdin yield finally: sys.stdin =3D oldstdin Although the restriction was removed the problem itself still remains. There's no guarantee that a finally block will execute if there is a yield in the try block. The same happens if you use a context manager around a yield statement: the __exit__ method is not guaranteed to be called. One implication of this is that in the following code it is not guaranteed that the file will be closed: def upperfile(filename): with open(filename) as fin: for line in fin: yield line.upper() -- Oscar