Author: Philip Jenvey <[email protected]>
Branch: py3k
Changeset: r58796:966a8838af36
Date: 2012-11-08 12:11 -0800
http://bitbucket.org/pypy/pypy/changeset/966a8838af36/

Log:    fix atexit's iteration order, missing import

diff --git a/pypy/module/atexit/app_atexit.py b/pypy/module/atexit/app_atexit.py
--- a/pypy/module/atexit/app_atexit.py
+++ b/pypy/module/atexit/app_atexit.py
@@ -8,18 +8,18 @@
     kwargs - optional keyword arguments to pass to func
 
     func is returned to facilitate usage as a decorator."""
-    
+
     if not callable(func):
         raise TypeError("func must be callable")
 
     atexit_callbacks.append((func, args, kwargs))
     return func
-    
+
 def run_exitfuncs():
     "Run all registered exit functions."
     # Maintain the last exception
     last_exc, last_tb = None, None
-    for (func, args, kwargs) in atexit_callbacks:
+    for (func, args, kwargs) in reversed(atexit_callbacks):
         if func is None:
             # unregistered slot
             continue
@@ -27,6 +27,7 @@
             func(*args, **kwargs)
         except BaseException as e:
             if not isinstance(e, SystemExit):
+                import sys
                 import traceback
                 last_type, last_exc, last_tb = sys.exc_info()
                 traceback.print_exception(last_type, last_exc, last_tb)
diff --git a/pypy/module/atexit/test/test_atexit.py 
b/pypy/module/atexit/test/test_atexit.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/atexit/test/test_atexit.py
@@ -0,0 +1,20 @@
+class AppTestAtexit:
+
+    def test_args(self):
+        import atexit
+        import io
+        import sys
+        stdout, stderr = sys.stdout, sys.stderr
+        try:
+            sys.stdout = sys.stderr = capture = io.StringIO()
+            def h1():
+                print("h1")
+            def h2():
+                print("h2")
+            atexit.register(h1)
+            atexit.register(h2)
+            atexit._run_exitfuncs()
+            assert capture.getvalue() == 'h2\nh1\n'
+        finally:
+            sys.stdout = stdout
+            sys.stderr = stderr
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to