diff options
-rw-r--r-- | c/test_c.py | 2 | ||||
-rw-r--r-- | testing/cffi0/test_function.py | 10 | ||||
-rw-r--r-- | testing/support.py | 5 |
3 files changed, 10 insertions, 7 deletions
diff --git a/c/test_c.py b/c/test_c.py index fb97044..6c2e5a7 100644 --- a/c/test_c.py +++ b/c/test_c.py @@ -1366,6 +1366,8 @@ def test_callback_exception(): try: linecache.getline = lambda *args: 'LINE' # hack: speed up PyPy tests sys.stderr = cStringIO.StringIO() + if hasattr(sys, '__unraisablehook__'): # work around pytest + sys.unraisablehook = sys.__unraisablehook__ # on recent CPythons assert f(100) == 300 assert sys.stderr.getvalue() == '' assert f(10000) == -42 diff --git a/testing/cffi0/test_function.py b/testing/cffi0/test_function.py index 4f2c864..b4bb23d 100644 --- a/testing/cffi0/test_function.py +++ b/testing/cffi0/test_function.py @@ -5,7 +5,7 @@ import math, os, sys import ctypes.util from cffi.backend_ctypes import CTypesBackend from testing.udir import udir -from testing.support import FdWriteCapture +from testing.support import FdWriteCapture, StdErrCapture from .backend_tests import needs_dlopen_none try: @@ -227,13 +227,9 @@ class TestFunction(object): def cb(): return returnvalue fptr = ffi.callback("void(*)(void)", cb) - old_stderr = sys.stderr - try: - sys.stderr = StringIO() + with StdErrCapture() as f: returned = fptr() - printed = sys.stderr.getvalue() - finally: - sys.stderr = old_stderr + printed = f.getvalue() assert returned is None if returnvalue is None: assert printed == '' diff --git a/testing/support.py b/testing/support.py index de8131c..6339a94 100644 --- a/testing/support.py +++ b/testing/support.py @@ -33,9 +33,14 @@ class StdErrCapture(object): from io import StringIO self.old_stderr = sys.stderr sys.stderr = f = StringIO() + if hasattr(sys, '__unraisablehook__'): # work around pytest + self.old_unraisablebook = sys.unraisablehook # on recent CPythons + sys.unraisablehook = sys.__unraisablehook__ return f def __exit__(self, *args): sys.stderr = self.old_stderr + if hasattr(self, 'old_unraisablebook'): + sys.unraisablehook = self.old_unraisablebook class FdWriteCapture(object): |