[issue10080] Py_Initialize crashes when stdout has been redirected with freopen

2013-07-05 Thread Christian Heimes

Christian Heimes added the comment:

I can't reproduce the issue with Python 3.2 and dup2 on Linux.

$ gcc -o py_main py_main_dup2.c  -I/usr/include/python3.2mu -lpython3.2mu -lm 
-pthread -ldl -lutil
$ ./py_main

log.txt contains 'foo' and the debug message, too. Is this still an issue for 
you?

--
nosy: +christian.heimes
resolution:  - works for me
status: open - languishing
versions: +Python 3.3, Python 3.4 -Python 3.1

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue10080
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue10080] Py_Initialize crashes when stdout has been redirected with freopen

2010-10-13 Thread Amaury Forgeot d'Arc

Amaury Forgeot d'Arc amaur...@gmail.com added the comment:

It looks very similar to issue6501.
Can you check if setting the environment variable
PYTHONIOENCODING=cp1252
solves the problem?

--
nosy: +amaury.forgeotdarc

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue10080
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue10080] Py_Initialize crashes when stdout has been redirected with freopen

2010-10-13 Thread Antoine Pitrou

Antoine Pitrou pit...@free.fr added the comment:

I don't think freopen() is the right solution, since it might open another 
(different) file descriptor under the hood; but Python always uses file 
descriptor 1 when creating sys.stderr.

I would suggest instead something such as (untested):

int tmpfd;
tmpfd = open(myfile.txt, O_CREAT | O_WRONLY);
dup2(tmpfd, 1);

--
nosy: +pitrou

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue10080
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue10080] Py_Initialize crashes when stdout has been redirected with freopen

2010-10-13 Thread Mitchell Stokes

Mitchell Stokes moguri...@gmail.com added the comment:

Setting PYTHONIOENCODING=cp1252 does not help.

Also, dup2 is not available on Windows (at least not from what I've read). 
From my understanding, freopen() is supposed to be the portable way to 
redirect stdout.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue10080
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue10080] Py_Initialize crashes when stdout has been redirected with freopen

2010-10-13 Thread Antoine Pitrou

Antoine Pitrou pit...@free.fr added the comment:

 Also, dup2 is not available on Windows (at least not from what I've
 read).

MSDN says dup2 is deprecated but you can use _dup2 instead:
http://msdn.microsoft.com/en-us/library/8syseb29%28v=VS.80%29.aspx

 From my understanding, freopen() is supposed to be the portable way to
 redirect stdout.

I'm not sure, but as I said Python does not use C's stdout. Instead,
it writes directly (using write()) to the file descriptor number 1. So,
if freopen() creates a different file descriptor for the C stdout,
Python's stdout will not be redirected where you want it to be.

I'm not saying that's what happens in your program, though. You could
call _fileno(stdout) after the reopen() call to check if it's still 1 or
not.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue10080
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue10080] Py_Initialize crashes when stdout has been redirected with freopen

2010-10-13 Thread Mitchell Stokes

Mitchell Stokes moguri...@gmail.com added the comment:

I used _dup2() and I still got a crash. Also, if I use _fileno(stdout) after 
using freopen(), I get 1.

I'm uploading two more example programs.

py_main_fileno_check.c prints the fileno to the text file
py_main_dup2.c uses _dup2() to redirect stdout instead of freopen

--
Added file: http://bugs.python.org/file19219/py_main_fileno_check.c

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue10080
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue10080] Py_Initialize crashes when stdout has been redirected with freopen

2010-10-13 Thread Mitchell Stokes

Changes by Mitchell Stokes moguri...@gmail.com:


Added file: http://bugs.python.org/file19220/py_main_dup2.c

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue10080
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue10080] Py_Initialize crashes when stdout has been redirected with freopen

2010-10-13 Thread Antoine Pitrou

Antoine Pitrou pit...@free.fr added the comment:

Thanks for the information. I'm afraid I can't be of any more help, since I'm 
not a Windows developer. I hope someone else can chime in.

--
nosy: +brian.curtin, tim.golden

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue10080
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue10080] Py_Initialize crashes when stdout has been redirected with freopen

2010-10-12 Thread Mitchell Stokes

New submission from Mitchell Stokes moguri...@gmail.com:

I have Python embedded in a C/C++ program, and I'm trying to redirect stdout to 
a text file. Since I'm trying to get rid of the console, I want C and Python 
stdout going to the text file (so, not sys.stdout = open('log.txt', 'w')). To 
this end, I used freopen() like so:

freopen(log.txt, w, stdout);

to redirect stdout in C. However, when using Python 3.1, I get the following 
error:

Fatal Python error: Py_Initialize: can't initialize sys standard streams
OSError: [Errno 9] Bad file descriptor

followed by a crash (Runtime Error). I think this is somewhat related to this 
bug: http://bugs.python.org/issue1415

I've attached a sample program that illustrates the problem. I compiled it with:

gcc -o py_main.exe py_main.c -IC:\Python31\include -LC:\Python31\libs -lpython31

I had someone test the sample program on Linux, and it worked fine there, so I 
think it's a Windows issue.

--
components: Windows
files: py_main.c
messages: 118502
nosy: Mitchell.Stokes
priority: normal
severity: normal
status: open
title: Py_Initialize crashes when stdout has been redirected with freopen
type: crash
versions: Python 3.1
Added file: http://bugs.python.org/file19212/py_main.c

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue10080
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com