From ccdeaa86b4ff6ed0a2f30eb61c7f714acee6f987 Mon Sep 17 00:00:00 2001 From: calvin Date: Tue, 29 Mar 2005 12:35:03 +0000 Subject: [PATCH] added git-svn-id: https://linkchecker.svn.sourceforge.net/svnroot/linkchecker/trunk/linkchecker@2475 e7d03fd6-7b0d-0410-9947-9c21f3af8025 --- linkcheck/decorators.py | 56 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 linkcheck/decorators.py diff --git a/linkcheck/decorators.py b/linkcheck/decorators.py new file mode 100644 index 00000000..16021213 --- /dev/null +++ b/linkcheck/decorators.py @@ -0,0 +1,56 @@ +""" +Simple decorators (usable in Python >= 2.4). +""" +import warnings + +def deprecated (func): + """ + A decorator which can be used to mark functions as deprecated. + It emits a warning when the function is called. + """ + def newFunc (*args, **kwargs): + warnings.warn("Call to deprecated function %s." % func.__name__, + category=DeprecationWarning) + return func(*args, **kwargs) + newFunc.__name__ = func.__name__ + newFunc.__doc__ = func.__doc__ + newFunc.__dict__.update(func.__dict__) + return newFunc + + +def _synchronized (lock, func): + """ + Call function with aqcuired lock. + """ + def newFunc (*args, **kwargs): + lock.acquire(True) # blocking + try: + return func(*args, **kwargs) + finally: + lock.release() + newFunc.__name__ = func.__name__ + newFunc.__doc__ = func.__doc__ + newFunc.__dict__.update(func.__dict__) + return newFunc + + +def synchronized (lock): + """ + A decorator calling a function with aqcuired lock. + """ + def newFunc (func): + return _synchronized(lock, func) + return newFunc + + +if __name__ == '__main__': + import thread + @synchronized(thread.allocate_lock()) + def f (): + """Just me""" + print "i am synchronized:", f, f.__doc__ + f() + @deprecated + def g (): + pass + g()