Commit f00d0932 authored by Georg Brandl's avatar Georg Brandl

Bug #1597824: return the registered function from atexit.register()

to facilitate usage as a decorator.
parent bc9f501e
...@@ -44,6 +44,10 @@ If an exception is raised during execution of the exit handlers, a ...@@ -44,6 +44,10 @@ If an exception is raised during execution of the exit handlers, a
traceback is printed (unless \exception{SystemExit} is raised) and the traceback is printed (unless \exception{SystemExit} is raised) and the
exception information is saved. After all exit handlers have had a exception information is saved. After all exit handlers have had a
chance to run the last exception to be raised is re-raised. chance to run the last exception to be raised is re-raised.
\versionchanged[This function now returns \var{func} which makes it
possible to use it as a decorator without binding the
original name to \code{None}]{2.6}
\end{funcdesc} \end{funcdesc}
...@@ -92,3 +96,15 @@ atexit.register(goodbye, 'Donny', 'nice') ...@@ -92,3 +96,15 @@ atexit.register(goodbye, 'Donny', 'nice')
# or: # or:
atexit.register(goodbye, adjective='nice', name='Donny') atexit.register(goodbye, adjective='nice', name='Donny')
\end{verbatim} \end{verbatim}
Usage as a decorator:
\begin{verbatim}
import atexit
@atexit.register
def goodbye():
print "You are now leaving the Python sector."
\end{verbatim}
This obviously only works with functions that don't take arguments.
...@@ -40,8 +40,11 @@ def register(func, *targs, **kargs): ...@@ -40,8 +40,11 @@ def register(func, *targs, **kargs):
func - function to be called at exit func - function to be called at exit
targs - optional arguments to pass to func targs - optional arguments to pass to func
kargs - optional keyword arguments to pass to func kargs - optional keyword arguments to pass to func
func is returned to facilitate usage as a decorator.
""" """
_exithandlers.append((func, targs, kargs)) _exithandlers.append((func, targs, kargs))
return func
if hasattr(sys, "exitfunc"): if hasattr(sys, "exitfunc"):
# Assume it's another registered exit function - append it to our list # Assume it's another registered exit function - append it to our list
......
...@@ -98,6 +98,9 @@ Core and builtins ...@@ -98,6 +98,9 @@ Core and builtins
Library Library
------- -------
- Bug #1597824: return the registered function from atexit.register()
to facilitate usage as a decorator.
- Patch #1360200: Use unmangled_version RPM spec field to deal with - Patch #1360200: Use unmangled_version RPM spec field to deal with
file name mangling. file name mangling.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment