Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7368433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:40:25+00:00 2026-05-29T03:40:25+00:00

I have some code that uses ctypes to try to determine if the file

  • 0

I have some code that uses ctypes to try to determine if the file pointed to by sys.stdout is actually stdout. I know that on any POSIX-compliant system, and even on Windows, it should be safe to assume this is true if sys.stdout.fileno() == 1, so my question is not how to do this in general.

In my code (which is already using ctypes for something unrelated to my question) I carelessly had something like:

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:
    ...

This works perfectly fine on Linux, so I didn’t really think about it much. It looked nicer and more readable than hard-coding 1 as the file descriptor. But I found a few days later that my code wasn’t working on OSX.

It turns outs OSX’s libc doesn’t export any symbol called ‘stdout’. Instead its stdio.h has stdout defined as:

#define stdout __stdoutp

If I change my code to c_void_p.in_dll(libc, '__stdoutp') 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).

I will probably just change my code to use 1, but my question still stands, out of curiosity, if there’s a cross-platform way to get the stdio pointer (and likewise stdin and stderr) without assuming that it’s using the POSIX-compliant descriptor?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T03:40:27+00:00Added an answer on May 29, 2026 at 3:40 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that uses Open XML to open up a .docx file,
I have some code that uses a multi-part form and HTML5 file object to
Hi I have some code that uses block RandomAccessFile file = new RandomAccessFile(some file,
I have some code that uses the shared gateway pattern to implement an inversion
I have some code that uses the Oracle function add_months to increment a Date
I have some Java code that uses curly braces in two ways // Curly
I have some old code that uses qsort to sort an MFC CArray of
We have some code kicking around that uses this old internal Sun package for
I have some c(++) code that uses sprintf to convert a uint_64 to a
I have some old python code that uses the pywin32 extensions. Starting out with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.