def test_FILE_stored_in_stdout():
if not sys.platform.startswith('linux'):
py.test.skip("likely, we cannot assign to stdout")
ffi = FFI()
ffi.cdef("int printf(const char *, ...); FILE *setstdout(FILE *);")
lib = ffi.verify("""
#include <stdio.h>
FILE *setstdout(FILE *f) {
FILE *result = stdout;
stdout = f;
return result;
}
""")
import os
fdr, fdw = os.pipe()
fw1 = os.fdopen(fdw, 'wb', 256)
old_stdout = lib.setstdout(fw1)
try:
#
fw1.write(b"X")
r = lib.printf(b"hello, %d!\n", ffi.cast("int", 42))
fw1.close()
assert r == len("hello, 42!\n")
#
finally:
lib.setstdout(old_stdout)
#
result = os.read(fdr, 256)
os.close(fdr)
# the 'X' might remain in the user-level buffer of 'fw1' and
# end up showing up after the 'hello, 42!\n'
assert result == b"Xhello, 42!\n" or result == b"hello, 42!\nX"
评论列表
文章目录