git-svn-id: https://linkchecker.svn.sourceforge.net/svnroot/linkchecker/trunk/linkchecker@2475 e7d03fd6-7b0d-0410-9947-9c21f3af8025
This commit is contained in:
calvin 2005-03-29 12:35:03 +00:00
parent b064e2b897
commit ccdeaa86b4

56
linkcheck/decorators.py Normal file
View file

@ -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()