[New-bugs-announce] [issue3957] [contextlib] Reverse order of locking

David Naylor report at bugs.python.org
Wed Sep 24 17:01:03 CEST 2008


New submission from David Naylor <naylor.b.david at gmail.com>:

Overview:
Add a generator that will revert the order applied to a with 
statement.  

Motivation:
Often with threaded applications one needs to do a certain task 
outside of a lock but while inside a greater block of code protected 
by a lock.  

e.g:
with lock:
  BLOCK1
  lock.release()
  try:
    BLOCK2
  finally:
    lock.acquire()
  BLOCK3

but with an inverter for a with statement it becomes:

with lock:
  BLOCK1
  with invert(lock):
    BLOCK2
  BLOCK3

[Of course there are many other possible uses for this...]

Implementation:
def invert(thing):
  thing.__exit__(None, None, None)
  yield thing
  thing.__enter__()

Issues:
Normal exception handling will not take place (however for locks and 
the like this is not an issue)

----------
components: Library (Lib)
messages: 73715
nosy: DragonSA
severity: normal
status: open
title: [contextlib] Reverse order of locking
type: feature request
versions: Python 2.6, Python 3.0

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue3957>
_______________________________________


More information about the New-bugs-announce mailing list