-# -*- encoding: utf-8 -*-
+# -*- coding: utf-8 -*-
##############################################################################
-#
+#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
+# Copyright (C) 2010 OpenERP s.a. (<http://openerp.com>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
-# along with this program. If not, see <http://www.gnu.org/licenses/>.
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
-__all__ = ['partial', 'wraps', 'update_wrapper']
+__all__ = ['partial', 'wraps', 'update_wrapper', 'synchronized']
try:
from functools import partial, wraps, update_wrapper
except ImportError:
# The functools module doesn't exist in python < 2.5
- # Code taken from python 2.5
+ # Code taken from python 2.5
# http://svn.python.org/view/python/tags/r254/Lib/functools.py?view=markup
def partial(fun, *args, **kwargs):
return partial(update_wrapper, wrapped=wrapped,
assigned=assigned, updated=updated)
+
+
+def synchronized(lock_attr='_lock'):
+ def decorator(func):
+ @wraps(func)
+ def wrapper(self, *args, **kwargs):
+ lock = getattr(self, lock_attr)
+ try:
+ lock.acquire()
+ return func(self, *args, **kwargs)
+ finally:
+ lock.release()
+ return wrapper
+ return decorator
+
+
+
+from inspect import getsourcefile
+
+def frame_codeinfo(fframe, back=0):
+ """ Return a (filename, line) pair for a previous frame .
+ @return (filename, lineno) where lineno is either int or string==''
+ """
+
+ try:
+ if not fframe:
+ return ("<unknown>", '')
+ for i in range(back):
+ fframe = fframe.f_back
+ try:
+ fname = getsourcefile(fframe)
+ except TypeError:
+ fname = '<builtin>'
+ lineno = fframe.f_lineno or ''
+ return (fname, lineno)
+ except Exception:
+ return ("<unknown>", '')