blob: a06e83a4a4d8937d7b03ccdcbaa257561d20290d [file] [log] [blame]
# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import functools
def in_context(context_name):
Call a method in the context of member variable 'context_name.'
You can use this like:
class Foo(object):
def __init__(self):
self._mutex = threading.RLock()
def bar(self):
# Two threads can call safely without
# any other synchronization.
print 'Locked up tight.'
def contextless_bar(self):
with self._mutex:
print 'Locked up tight.'
With the in_context decorator, is equivalent to
self.contextless_bar. You can use this this to declare synchronized
methods in the style of Java. Similar to other locking methods, this
can land you in deadlock in a hurry if you're not aware of what you're
@param context_name string name of the context manager to look up in self.
def wrap(func):
This function will get called with the instance method pulled off
of self. It does not get the self object though, so we wrap yet
another nested function.
@param func Function object that we'll eventually call.
def wrapped_manager(self, *args, **kwargs):
""" Do the actual work of acquiring the context.
We need this layer of indirection so that we can get at self.
We use functools.wraps does some magic so that the function
names and docs are set correctly on the wrapped function.
context = getattr(self, context_name)
with context:
return func(self, *args, **kwargs)
return wrapped_manager
return wrap