|
| 1 | + |
| 2 | +# FastDup Software, (C) copyright 2022 Dr. Amir Alush and Dr. Danny Bickson. |
| 3 | +# This software is free for non-commercial and academic usage under the Creative Common Attribution-NonCommercial-NoDerivatives |
| 4 | +# 4.0 International license. Please reach out to [email protected] for licensing options. |
| 5 | + |
| 6 | +# workaround taken from https://stackoverflow.com/questions/35745541/how-to-get-printed-output-from-ctypes-c-functions-into-jupyter-ipython-notebook |
| 7 | +# to view C++ output in jupyter notebook |
| 8 | + |
| 9 | +import io |
| 10 | +import os |
| 11 | +import sys |
| 12 | +import tempfile |
| 13 | +from contextlib import contextmanager |
| 14 | +import ctypes |
| 15 | +from ctypes import util |
| 16 | +import platform |
| 17 | +# use_errno parameter is optional, because I'm not checking errno anyway. |
| 18 | +libc = ctypes.CDLL(util.find_library('c'), use_errno=True) |
| 19 | + |
| 20 | +class FILE(ctypes.Structure): |
| 21 | + pass |
| 22 | + |
| 23 | +FILE_p = ctypes.POINTER(FILE) |
| 24 | + |
| 25 | +# Alternatively, we can just use: |
| 26 | +# FILE_p = ctypes.c_void_p |
| 27 | + |
| 28 | +# These variables, defined inside the C library, are readonly. |
| 29 | +if platform.system() == "Darwin": |
| 30 | + cstdin = FILE_p.in_dll(libc, '__stdinp') |
| 31 | + cstdout = FILE_p.in_dll(libc, '__stdoutp') |
| 32 | + cstderr = FILE_p.in_dll(libc, '__stderrp') |
| 33 | +else: |
| 34 | + cstdin = FILE_p.in_dll(libc, 'stdin') |
| 35 | + cstdout = FILE_p.in_dll(libc, 'stdout') |
| 36 | + cstderr = FILE_p.in_dll(libc, 'stderr') |
| 37 | + |
| 38 | +# C function to disable buffering. |
| 39 | +csetbuf = libc.setbuf |
| 40 | +csetbuf.argtypes = (FILE_p, ctypes.c_char_p) |
| 41 | +csetbuf.restype = None |
| 42 | + |
| 43 | +# C function to flush the C library buffer. |
| 44 | +cfflush = libc.fflush |
| 45 | +cfflush.argtypes = (FILE_p,) |
| 46 | +cfflush.restype = ctypes.c_int |
| 47 | + |
| 48 | +@contextmanager |
| 49 | +def capture_c_stdout(encoding='utf8'): |
| 50 | + # Flushing, it's a good practice. |
| 51 | + sys.stdout.flush() |
| 52 | + sys.stderr.flush() |
| 53 | + cfflush(cstdout) |
| 54 | + cfflush(cstderr) |
| 55 | + |
| 56 | + # We need to use a actual file because we need the file descriptor number. |
| 57 | + with tempfile.TemporaryFile(buffering=0) as temp: |
| 58 | + # Saving a copy of the original stdout. |
| 59 | + with tempfile.TemporaryFile(buffering=0) as temp2: |
| 60 | + prev_sys_stdout = sys.stdout |
| 61 | + prev_sys_stderr = sys.stderr |
| 62 | + prev_stdout_fd = os.dup(1) |
| 63 | + prev_stderr_fd = os.dup(2) |
| 64 | + os.close(1) |
| 65 | + os.close(2) |
| 66 | + |
| 67 | + # Duplicating the temporary file fd into the stdout fd. |
| 68 | + # In other words, replacing the stdout. |
| 69 | + os.dup2(temp.fileno(), 1) |
| 70 | + os.dup2(temp2.fileno(), 2) |
| 71 | + |
| 72 | + # Replacing sys.stdout for Python code. |
| 73 | + # |
| 74 | + # IPython Notebook version of sys.stdout is actually an |
| 75 | + # in-memory OutStream, so it does not have a file descriptor. |
| 76 | + # We need to replace sys.stdout so that interleaved Python |
| 77 | + # and C output gets captured in the correct order. |
| 78 | + # |
| 79 | + # We enable line_buffering to force a flush after each line. |
| 80 | + # And write_through to force all data to be passed through the |
| 81 | + # wrapper directly into the binary temporary file. |
| 82 | + temp_wrapper = io.TextIOWrapper( |
| 83 | + temp, encoding=encoding, line_buffering=True, write_through=True) |
| 84 | + sys.stdout = temp_wrapper |
| 85 | + temp_wrapper2 = io.TextIOWrapper(temp2, encoding=encoding, line_buffering=True, write_through=True) |
| 86 | + sys.stderr = temp_wrapper2 |
| 87 | + |
| 88 | + # Disabling buffering of C stdout. |
| 89 | + csetbuf(cstdout, None) |
| 90 | + csetbuf(cstderr, None) |
| 91 | + |
| 92 | + yield |
| 93 | + |
| 94 | + # Must flush to clear the C library buffer. |
| 95 | + cfflush(cstdout) |
| 96 | + cfflush(cstderr) |
| 97 | + |
| 98 | + # Restoring stdout. |
| 99 | + os.dup2(prev_stdout_fd, 1) |
| 100 | + os.dup2(prev_stderr_fd, 2) |
| 101 | + os.close(prev_stdout_fd) |
| 102 | + os.close(prev_stderr_fd) |
| 103 | + sys.stdout = prev_sys_stdout |
| 104 | + sys.stderr = prev_sys_stderr |
| 105 | + |
| 106 | + # Printing the captured output. |
| 107 | + temp_wrapper.seek(0) |
| 108 | + print(temp_wrapper.read(), end='') |
| 109 | + temp_wrapper2.seek(0) |
| 110 | + print(temp_wrapper2.read(), end='') |
0 commit comments