mirror of
https://github.com/Hopiu/linkchecker.git
synced 2026-03-25 18:30:23 +00:00
git-svn-id: https://linkchecker.svn.sourceforge.net/svnroot/linkchecker/trunk/linkchecker@2624 e7d03fd6-7b0d-0410-9947-9c21f3af8025
75 lines
1.9 KiB
Python
75 lines
1.9 KiB
Python
"""
|
|
Simple decorators (usable in Python >= 2.4).
|
|
"""
|
|
import warnings
|
|
import signal
|
|
import os
|
|
|
|
|
|
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 signal_handler (signal_number):
|
|
"""
|
|
A decorator to set the specified function as handler for a signal.
|
|
This function is the 'outer' decorator, called with only the
|
|
(non-function) arguments.
|
|
|
|
From http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/410666
|
|
"""
|
|
# create the 'real' decorator which takes only a function as an argument
|
|
def newfunc (function):
|
|
if os.name == 'posix':
|
|
signal.signal(signal_number, function)
|
|
return function
|
|
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 (function):
|
|
return _synchronized(lock, function)
|
|
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()
|