diff options
author | Armin Rigo <arigo@tunes.org> | 2015-11-18 12:28:44 +0100 |
---|---|---|
committer | Armin Rigo <arigo@tunes.org> | 2015-11-18 12:28:44 +0100 |
commit | d995bf5eda3cd8dae5c86206ee44f4ccd77d70bb (patch) | |
tree | 14a0060a9d11803d4e629f4aa86bd668bd94faba /testing/cffi0/test_function.py | |
parent | 1b266852f4dc07efdca37f2fe495cc42b60c1a31 (diff) | |
download | cffi-d995bf5eda3cd8dae5c86206ee44f4ccd77d70bb.tar.gz |
Write one error message directly to stderr instead of sys.stderr. This
lets us avoid taking the GIL, which might crash in case the Python
interpreter is not initialized at all.
Diffstat (limited to 'testing/cffi0/test_function.py')
-rw-r--r-- | testing/cffi0/test_function.py | 32 |
1 files changed, 5 insertions, 27 deletions
diff --git a/testing/cffi0/test_function.py b/testing/cffi0/test_function.py index b4c98e7..8fdcc86 100644 --- a/testing/cffi0/test_function.py +++ b/testing/cffi0/test_function.py @@ -4,6 +4,7 @@ import math, os, sys import ctypes.util from cffi.backend_ctypes import CTypesBackend from testing.udir import udir +from testing.support import FdWriteCapture try: from StringIO import StringIO @@ -11,29 +12,6 @@ except ImportError: from io import StringIO -class FdWriteCapture(object): - """xxx limited to capture at most 512 bytes of output, according - to the Posix manual.""" - - def __init__(self, capture_fd): - self.capture_fd = capture_fd - - def __enter__(self): - self.read_fd, self.write_fd = os.pipe() - self.copy_fd = os.dup(self.capture_fd) - os.dup2(self.write_fd, self.capture_fd) - return self - - def __exit__(self, *args): - os.dup2(self.copy_fd, self.capture_fd) - os.close(self.copy_fd) - os.close(self.write_fd) - self._value = os.read(self.read_fd, 512) - os.close(self.read_fd) - - def getvalue(self): - return self._value - lib_m = 'm' if sys.platform == 'win32': #there is a small chance this fails on Mingw via environ $CC @@ -135,7 +113,7 @@ class TestFunction(object): """) ffi.C = ffi.dlopen(None) ffi.C.fputs # fetch before capturing, for easier debugging - with FdWriteCapture(2) as fd: + with FdWriteCapture() as fd: ffi.C.fputs(b"hello\n", ffi.C.stderr) ffi.C.fputs(b" world\n", ffi.C.stderr) res = fd.getvalue() @@ -151,7 +129,7 @@ class TestFunction(object): """) ffi.C = ffi.dlopen(None) ffi.C.fputs # fetch before capturing, for easier debugging - with FdWriteCapture(2) as fd: + with FdWriteCapture() as fd: ffi.C.fputs(b"hello\n", ffi.C.stderr) ffi.C.fputs(b" world\n", ffi.C.stderr) res = fd.getvalue() @@ -166,7 +144,7 @@ class TestFunction(object): void *stderr; """) ffi.C = ffi.dlopen(None) - with FdWriteCapture(2) as fd: + with FdWriteCapture() as fd: ffi.C.fprintf(ffi.C.stderr, b"hello with no arguments\n") ffi.C.fprintf(ffi.C.stderr, b"hello, %s!\n", ffi.new("char[]", b"world")) @@ -228,7 +206,7 @@ class TestFunction(object): fptr = ffi.cast("int(*)(const char *txt, void *)", ffi.C.fputs) assert fptr == ffi.C.fputs assert repr(fptr).startswith("<cdata 'int(*)(char *, void *)' 0x") - with FdWriteCapture(2) as fd: + with FdWriteCapture() as fd: fptr(b"world\n", ffi.C.stderr) res = fd.getvalue() assert res == b'world\n' |