Title: with_statement: context __exit__ not called for return
Type: behaviour Severity: normal
Components: None Versions: 2.5.1
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: pjenvey Nosy List: draghuram, pjenvey, terrence
Priority: Keywords: patch

Created on 2008-12-08.08:36:42 by terrence, last changed 2008-12-09.21:57:42 by pjenvey.

File name Uploaded Description Edit Remove terrence, 2008-12-08.08:36:41 A test case that demonstrates the problem.
test_with_return_calls_exit.diff terrence, 2008-12-08.09:25:21 This patch adds a failing test for this bug to the jython regrtest suite.
msg3885 (view) Author: Terrence Cole (terrence) Date: 2008-12-08.08:36:41
A minimal test case:

from __future__ import with_statement
from threading import Lock, RLock

def func_return():
        lock = Lock()
        with lock:
                return lock

if func_return()._is_owned(): print "FAIL!"
else print "OK"
# eof

After annotating __enter__ and __exit__ of RLock with print statements,
it appears that __exit__ is not getting called in this case, but is in
other similar cases (see more detailed attached test case for cases that
pass).  The jython I'm using was compiled from svn on 7 Dec 2008.
msg3886 (view) Author: Terrence Cole (terrence) Date: 2008-12-08.09:25:21
Attached a patch to add a test for this case to the regrtest testsuite.
msg3887 (view) Author: Raghuram Devarakonda (draghuram) Date: 2008-12-08.15:25:22
I have confirmed this behaviour with the following simple script (run on

from __future__ import with_statement

class test:
    def __init__(self):
    def __enter__(self):
        print "in enter"
        return self
    def __exit__(self, exc_type, exc_val, exc_tb):
        print "in exit"
        return False

def f():
    with test():

akash$ jython 
in enter

akash$ jython
Jython 2.5a3+ (trunk:5430M, Oct 16 2008, 12:29:25) 
[Java HotSpot(TM) Client VM (Sun Microsystems Inc.)] on java1.6.0_06
msg3891 (view) Author: Philip Jenvey (pjenvey) Date: 2008-12-08.20:58:41
Looks like our compiler is generating a try: catch: instead of a try: 
finally: for these
msg3894 (view) Author: Philip Jenvey (pjenvey) Date: 2008-12-09.21:57:42
fixed in r5724

note the bytecode magic hasn't changed for this yet, so you'll want to 
remove older $py.class's before trying it

Date User Action Args
2008-12-09 21:57:42pjenveysetstatus: open -> closed
resolution: fixed
messages: + msg3894
components: + None, - Core
2008-12-09 04:31:42pjenveysetassignee: pjenvey
2008-12-08 22:50:47loewissetfiles: - README.diff
2008-12-08 22:50:26loewissetfiles: + README.diff
keywords: + patch
2008-12-08 20:58:42pjenveysetnosy: + pjenvey
messages: + msg3891
2008-12-08 15:25:23draghuramsetnosy: + draghuram
messages: + msg3887
2008-12-08 09:25:22terrencesetfiles: + test_with_return_calls_exit.diff
messages: + msg3886
2008-12-08 08:36:42terrencecreate