from __future__ import with_statement from threading import Lock, RLock def func_pass(): lock = Lock() with lock: pass return lock def func_except(): lock = Lock() try: with lock: raise Exception("Hello World") finally: return lock def func_return(): lock = Lock() with lock: return lock print "func_pass lock still held : %s" % func_pass()._is_owned() print "func_except lock still held: %s" % func_except()._is_owned() print "func_return lock still held: %s" % func_return()._is_owned()