[IMP] safe_eval: allow INPLACE* operator opcodes as introduced in PEP-203
[odoo/odoo.git] / openerp / tools / func.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 #    Copyright (C) 2010 OpenERP s.a. (<http://openerp.com>).
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU Affero General Public License as
10 #    published by the Free Software Foundation, either version 3 of the
11 #    License, or (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU Affero General Public License for more details.
17 #
18 #    You should have received a copy of the GNU Affero General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 __all__ = ['partial', 'wraps', 'update_wrapper', 'synchronized']
24
25 try:
26     from functools import partial, wraps, update_wrapper
27 except ImportError:
28     # The functools module doesn't exist in python < 2.5
29     # Code taken from python 2.5
30     # http://svn.python.org/view/python/tags/r254/Lib/functools.py?view=markup
31
32     def partial(fun, *args, **kwargs):
33         """ Partial implementation
34         
35             See: http://svn.python.org/view/python/tags/r254/Lib/functools.py
36         """
37         def _partial(*args2, **kwargs2):
38             return fun(*(args+args2), **dict(kwargs, **kwargs2))
39         return _partial
40
41     ### --- code from python 2.5
42
43     # update_wrapper() and wraps() are tools to help write
44     # wrapper functions that can handle naive introspection
45
46     WRAPPER_ASSIGNMENTS = ('__module__', '__name__', '__doc__')
47     WRAPPER_UPDATES = ('__dict__',)
48     def update_wrapper(wrapper,
49                        wrapped,
50                        assigned = WRAPPER_ASSIGNMENTS,
51                        updated = WRAPPER_UPDATES):
52         """Update a wrapper function to look like the wrapped function
53
54            wrapper is the function to be updated
55            wrapped is the original function
56            assigned is a tuple naming the attributes assigned directly
57            from the wrapped function to the wrapper function (defaults to
58            functools.WRAPPER_ASSIGNMENTS)
59            updated is a tuple naming the attributes off the wrapper that
60            are updated with the corresponding attribute from the wrapped
61            function (defaults to functools.WRAPPER_UPDATES)
62         """
63         for attr in assigned:
64             setattr(wrapper, attr, getattr(wrapped, attr))
65         for attr in updated:
66             getattr(wrapper, attr).update(getattr(wrapped, attr, {}))
67         # Return the wrapper so this can be used as a decorator via partial()
68         return wrapper
69
70     def wraps(wrapped,
71               assigned = WRAPPER_ASSIGNMENTS,
72               updated = WRAPPER_UPDATES):
73         """Decorator factory to apply update_wrapper() to a wrapper function
74
75            Returns a decorator that invokes update_wrapper() with the decorated
76            function as the wrapper argument and the arguments to wraps() as the
77            remaining arguments. Default arguments are as for update_wrapper().
78            This is a convenience function to simplify applying partial() to
79            update_wrapper().
80         """
81         return partial(update_wrapper, wrapped=wrapped,
82                        assigned=assigned, updated=updated)
83
84
85
86 def synchronized(lock_attr='_lock'):
87     def decorator(func):
88         @wraps(func)
89         def wrapper(self, *args, **kwargs):
90             lock = getattr(self, lock_attr)
91             try:
92                 lock.acquire()
93                 return func(self, *args, **kwargs)
94             finally:
95                 lock.release()
96         return wrapper
97     return decorator
98
99
100
101 from inspect import getsourcefile
102
103 def frame_codeinfo(fframe, back=0):
104     """ Return a (filename, line) pair for a previous frame .
105         @return (filename, lineno) where lineno is either int or string==''
106     """
107     
108     try:
109         if not fframe:
110             return ("<unknown>", '')
111         for i in range(back):
112             fframe = fframe.f_back
113         try:
114             fname = getsourcefile(fframe)
115         except TypeError:
116             fname = '<builtin>'
117         lineno = fframe.f_lineno or ''
118         return (fname, lineno)
119     except Exception:
120         return ("<unknown>", '')
121
122 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: