0

When calling a wrapped c function as such:

f = io.BytesIO()
with stdout_redirector(f):
    r = _wrapped_c_function(in_file, out_file)

the return code is returned in r, where return codes are given based on the errors raise inside the C function, however if an error occurs in the C function that is NOT caught by the C function r exits as 0 and the Python program silently exits as 0 indicating as if nothing was wrong. However in stdoutt a message such as malloc(): invalid size (unsorted) will get printed.

How can I catch these error messages, as the function call should print nothing if it all works. The issue with using the redirector is that when _wrapped_c_function fails it exits the Python function before anything can get written into the buffer f.

Sam Palmer
  • 1,675
  • 1
  • 25
  • 45
  • Either you are breaking the C function's contract or the C function is buggy. If the former, you should check your arguments before passing them. If the latter, you should not try to fix it on Python's side. – Acorn Jan 31 '20 at 19:07
  • The function is buggy, but I want to raise an error when the case is that the C function is buggy. However currently I can't catch the unknown cases when the C function is buggy as it just silently exits! – Sam Palmer Jan 31 '20 at 19:08
  • Log the function call. If the function fails, you will know because you'll see it get called in the log but no return value or error. – Robert Harvey Jan 31 '20 at 19:56
  • The C function isn't handling the error condition from malloc failing correctly. There's nothing you can do about that without patching the C code. – ngoldbaum Feb 03 '20 at 17:49

0 Answers0