Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a cross-platform way to reliably find the stdout file descriptor with ctypes?
    primarykey
    data
    text
    <p>I have some code that uses ctypes to try to determine if the file pointed to by <code>sys.stdout</code> is <em>actually</em> <code>stdout</code>. I know that on any POSIX-compliant system, and even on Windows, it should be safe to assume this is true if <code>sys.stdout.fileno() == 1</code>, so my question is not how to do this in general.</p> <p>In my code (which is already using ctypes for something unrelated to my question) I carelessly had something like:</p> <pre><code>libc = ctypes.CDLL(ctypes.util.find_library('c')) real_stdout = libc.fileno(ctypes.c_void_p.in_dll(libc, 'stdout')) if sys.stdout.fileno() == real_stdout: ... </code></pre> <p>This works perfectly fine on Linux, so I didn't really think about it much. It looked nicer and more readable than hard-coding <code>1</code> as the file descriptor. But I found a few days later that my code wasn't working on OSX.</p> <p>It turns outs OSX's libc doesn't export any symbol called 'stdout'. Instead its stdio.h has stdout defined as:</p> <pre><code>#define stdout __stdoutp </code></pre> <p>If I change my code to <code>c_void_p.in_dll(libc, '__stdoutp')</code> my code works as expected, but of course that's OSX-only. Windows, it turns out, has a similar issue (at least if using MSVC).</p> <p>I will probably just change my code to use <code>1</code>, but my question still stands, out of curiosity, if there's a cross-platform way to get the <code>stdio</code> pointer (and likewise <code>stdin</code> and <code>stderr</code>) without <em>assuming</em> that it's using the POSIX-compliant descriptor?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload