]> granicus.if.org Git - python/commitdiff
Bug #1597824: return the registered function from atexit.register()
authorGeorg Brandl <georg@python.org>
Thu, 16 Nov 2006 16:50:59 +0000 (16:50 +0000)
committerGeorg Brandl <georg@python.org>
Thu, 16 Nov 2006 16:50:59 +0000 (16:50 +0000)
to facilitate usage as a decorator.

Doc/lib/libatexit.tex
Lib/atexit.py
Misc/NEWS

index 33dc7ddf8cdb7daaecf8087334fe8c22223eb79f..9798b5750744b1e80abd87b13b899b9f6d82b6ab 100644 (file)
@@ -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
 exception information is saved.  After all exit handlers have had a
 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}
 
 
@@ -92,3 +96,15 @@ atexit.register(goodbye, 'Donny', 'nice')
 # or:
 atexit.register(goodbye, adjective='nice', name='Donny')
 \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.
index c9f4cc677528a7822f71860f34c16d1ceeb10974..93fddf7f99a4473f971474745a74330059150010 100644 (file)
@@ -40,8 +40,11 @@ def register(func, *targs, **kargs):
     func - function to be called at exit
     targs - optional 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))
+    return func
 
 if hasattr(sys, "exitfunc"):
     # Assume it's another registered exit function - append it to our list
index 2d4e31d3d462faf77cb023c329920fc54919226c..32cdb1f688d6d0b9393f9b74fefe00a46446fb15 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -98,6 +98,9 @@ Core and builtins
 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
   file name mangling.