Author: Ronan Lamy <ronan.l...@gmail.com>
Branch: cleanup-test_lib_pypy
Changeset: r95451:b73f30700869
Date: 2018-12-11 20:26 +0000
http://bitbucket.org/pypy/pypy/changeset/b73f30700869/

Log:    Clean up test using pytest's builtin fixtures

diff --git a/pypy/module/test_lib_pypy/ctypes_tests/test_callback_traceback.py 
b/pypy/module/test_lib_pypy/ctypes_tests/test_callback_traceback.py
--- a/pypy/module/test_lib_pypy/ctypes_tests/test_callback_traceback.py
+++ b/pypy/module/test_lib_pypy/ctypes_tests/test_callback_traceback.py
@@ -1,50 +1,35 @@
 # derived from test_random_things.py
+import pytest
+
 from ctypes import *
-import sys
 
-class TestCallbackTraceback:
-    # When an exception is raised in a ctypes callback function, the C
-    # code prints a traceback.
+_rawffi = pytest.importorskip('_rawffi')
+
+#
+# This test makes sure the exception types *and* the exception
+# value is printed correctly.
+
+@pytest.mark.skipif("sys.flags.inspect")
+def test_SystemExit(monkeypatch, capsys):
+    """
+    When an exception is raised in a ctypes callback function, the C
+    code prints a traceback. When SystemExit is raised, the interpreter
+    normally exits immediately.
+    """
+    def callback_func(arg):
+        raise SystemExit(42)
+    def custom_exit(value):
+        raise Exception("<<<exit(%r)>>>" % (value,))
+    monkeypatch.setattr(_rawffi, 'exit', custom_exit)
+    cb = CFUNCTYPE(c_int, c_int)(callback_func)
+    cb2 = cast(cast(cb, c_void_p), CFUNCTYPE(c_int, c_int))
+    out, err = capsys.readouterr()
+    assert not err
+    cb2(0)
+    out, err = capsys.readouterr()
+    assert err.splitlines()[-1] == "Exception: <<<exit(42)>>>"
     #
-    # This test makes sure the exception types *and* the exception
-    # value is printed correctly.
-    #
-    # Changed in 0.9.3: No longer is '(in callback)' prepended to the
-    # error message - instead a additional frame for the C code is
-    # created, then a full traceback printed.  When SystemExit is
-    # raised in a callback function, the interpreter exits.
-
-    def capture_stderr(self, func, *args, **kw):
-        # helper - call function 'func', and return the captured stderr
-        import StringIO
-        old_stderr = sys.stderr
-        logger = sys.stderr = StringIO.StringIO()
-        try:
-            func(*args, **kw)
-        finally:
-            sys.stderr = old_stderr
-        return logger.getvalue()
-
-    def test_SystemExit(self):
-        import _rawffi
-        if sys.flags.inspect:
-            skip("requires sys.flags.inspect == 0")
-        def callback_func(arg):
-            raise SystemExit(42)
-        def custom_exit(value):
-            raise Exception("<<<exit(%r)>>>" % (value,))
-        original_exit = _rawffi.exit
-        try:
-            _rawffi.exit = custom_exit
-            #
-            cb = CFUNCTYPE(c_int, c_int)(callback_func)
-            cb2 = cast(cast(cb, c_void_p), CFUNCTYPE(c_int, c_int))
-            out = self.capture_stderr(cb2, 0)
-            assert out.splitlines()[-1] == "Exception: <<<exit(42)>>>"
-            #
-            cb = CFUNCTYPE(c_int, c_int)(callback_func)
-            out = self.capture_stderr(cb, 0)
-            assert out.splitlines()[-1] == "Exception: <<<exit(42)>>>"
-            #
-        finally:
-            _rawffi.exit = original_exit
+    cb = CFUNCTYPE(c_int, c_int)(callback_func)
+    cb(0)
+    out, err = capsys.readouterr()
+    assert err.splitlines()[-1] == "Exception: <<<exit(42)>>>"
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to