This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Redirected stdout fires [Errno 9]
Type: Stage:
Components: IO, Windows Versions: Python 2.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: BreamoreBoy, amaury.forgeotdarc, ecir.hana
Priority: normal Keywords:

Created on 2009-11-18 17:15 by ecir.hana, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
std.c ecir.hana, 2009-11-18 17:15 test-case
Messages (3)
msg95433 - (view) Author: Ecir Hana (ecir.hana) Date: 2009-11-18 17:15
I try to log all the output of a program written in Python and C to a
buffer. I create a pipe, redirect stdout to its write-end and then read
its content afterward. However, printing from Python fires "IOError:
[Errno 9] Bad file descriptor". Please see the attached test-case.

It is happening on Windows XP, Python 2.6 and MinGW GCC and I used this
to compile:
gcc -o std.exe std.c -Ic:/dev/include/python2.6 -l python26

PS: It might be that the problem is that Python was compiled with
MSVC2008 and I'm using MinGW but I'm not sure...
msg112890 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2010-08-04 20:49
Can a Windows guru comment on whether this is a Python issue or not?
msg117562 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2010-09-28 23:20
Yes, mingw uses a different C runtime (msvcrt.dll) than Python (msvcr90.dll), so file descriptors are completely different and this causes the error.

I can see two solutions:

- Use the same compiler as python26; I've also heard about a way to force mingw to use msvcr90.dll.

- Avoid to pass mingw file descriptors to the python interpreter.  This requires more work: create a PyTypeObject that mimics a file object, and set this as sys.stdout. It should be enough to implement .write() and .flush(); you can use the C system calls (fwrite, fflush or whatever) there, and the file descriptors are used in a consistent way.
History
Date User Action Args
2022-04-11 14:56:54adminsetgithub: 51595
2010-09-28 23:21:00amaury.forgeotdarcsetstatus: open -> closed

nosy: + amaury.forgeotdarc
messages: + msg117562

resolution: not a bug
2010-08-04 20:49:35BreamoreBoysetnosy: + BreamoreBoy
messages: + msg112890
2009-11-18 17:15:32ecir.hanacreate