Is there a cross-platform way to reliably find the stdout file descriptor with ctypes?
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Droplet of life
--
Chapters
00:00 Is There A Cross-Platform Way To Reliably Find The Stdout File Descriptor With Ctypes?
01:50 Accepted Answer Score 5
02:43 Answer 2 Score 4
03:18 Thank you
--
Full question
https://stackoverflow.com/questions/9119...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #macos #posix #ctypes #darwin
#avk47
ACCEPTED ANSWER
Score 5
As so often when it comes to C, if you want compatibility, you'll have to go and look in the relevant standard. Since you mention windows, I guess you're not actually wanting the POSIX standard, but rather the C one.
C99 section 7,19,1 defines stdout to be a macro, and thus not a variable. That means there's no way you can rely on finding it using dlsym (which I assume in_dll uses). The actual expression could just as well be a function call or a fixed address. Perhaps not very likely, but it is possible...
As said in the comments, the fileno function is in turn defined by POSIX, not by C. C has no concept of file descriptors. I think you're better off assuming POSIX and just checking for the value 1, which it specifies.
ANSWER 2
Score 4
If you're simply interested in making things work, rather than strict standards adherence (like me), you can find the "real" name of stdout by writing a simple C snippet:
echo -e '#include <stdio.h>\nFILE* mystdout = stdout;' > test.c
cpp test.c | tail
Gives you the output:
FILE* mystdout = __stdoutp;
This means that you also need to try ctypes.c_void_p.in_dll(libc, '__stdoutp') to cover the case of darwin.