stdout/err and C extentions

H

hg

Hi,

I have the following

********************* C extention - redir.c


#include "Python.h"

PyObject * test_redir_test(PyObject *self) {
fprintf(stdout, "Hello from an extention!\n");
Py_INCREF(Py_None);
return Py_None;
}

static PyMethodDef test_redir_methods[] = {
{"test", (PyCFunction)test_redir_test, METH_NOARGS, "Prints test string
\n"},
{NULL, NULL, 0, NULL}
};

DL_EXPORT(void) inittest_redir(void)
{
Py_InitModule3("test_redir", test_redir_methods, "Provides a test
function.\n");
}

********************* setup.py :

from distutils.core import setup, Extension
import sys
import os


include = []



setup(name="test_redir", version="0.1",
ext_modules=[
Extension(
"test_redir",
["redir.c"],
undef_macros=['RELEASE'],
library_dirs=[],
)
]
)

*********************** python test script: test.py:



import sys


class My_Stdout:
def write(self, p_string):
l_file = open('res.txt','a')
l_file.write(p_string)
l_file.close


sys.stdout = My_Stdout()

print 'toto'
import test_redir



test_redir.test()



**************** Question:


print 'toto' does go to "res.txt" while "Hello from an extention!\n" goes to
the console.




Any clue ?


Thanks,


hg
 
R

Robert Bauck Hamar

hg said:
Hi,

I have the following

********************* C extention - redir.c


#include "Python.h"

PyObject * test_redir_test(PyObject *self) {
fprintf(stdout, "Hello from an extention!\n");
Py_INCREF(Py_None);
return Py_None;
}
[...]
*********************** python test script: test.py:
import sys


class My_Stdout:
def write(self, p_string):
l_file = open('res.txt','a')
l_file.write(p_string)
l_file.close


sys.stdout = My_Stdout()

print 'toto'
import test_redir



test_redir.test()



**************** Question:

print 'toto' does go to "res.txt" while "Hello from an extention!\n" goes
to the console.

Any clue ?

There is no portable way to change the location of stdout during execution
of a program. If you want to print with whatever is sys.stdout from an
extension module, you should call sys.stdout's write method dynamically
from C.
 
H

hg

Robert said:
hg said:
Hi,

I have the following

********************* C extention - redir.c


#include "Python.h"

PyObject * test_redir_test(PyObject *self) {
fprintf(stdout, "Hello from an extention!\n");
Py_INCREF(Py_None);
return Py_None;
}
[...]
*********************** python test script: test.py:
import sys


class My_Stdout:
def write(self, p_string):
l_file = open('res.txt','a')
l_file.write(p_string)
l_file.close


sys.stdout = My_Stdout()

print 'toto'
import test_redir



test_redir.test()



**************** Question:

print 'toto' does go to "res.txt" while "Hello from an extention!\n" goes
to the console.

Any clue ?

There is no portable way to change the location of stdout during execution
of a program. If you want to print with whatever is sys.stdout from an
extension module, you should call sys.stdout's write method dynamically
from C.


Robert, thanks,

I understand that sys.stdout and stdout of an extention are two different
entities ... correct ?


hg
 
R

Robert Bauck Hamar

hg said:
Robert said:
hg said:
Hi,

I have the following

********************* C extention - redir.c


#include "Python.h"

PyObject * test_redir_test(PyObject *self) {
fprintf(stdout, "Hello from an extention!\n");
Py_INCREF(Py_None);
return Py_None;
}
[...]
*********************** python test script: test.py:
import sys


class My_Stdout:
def write(self, p_string):
l_file = open('res.txt','a')
l_file.write(p_string)
l_file.close


sys.stdout = My_Stdout()

print 'toto'
import test_redir



test_redir.test()



**************** Question:

print 'toto' does go to "res.txt" while "Hello from an extention!\n"
goes to the console.

Any clue ?

There is no portable way to change the location of stdout during
execution of a program. If you want to print with whatever is sys.stdout
from an extension module, you should call sys.stdout's write method
dynamically from C.

Robert, thanks,

I understand that sys.stdout and stdout of an extention are two different
entities ... correct ?

Yes. Python's sys.stdout and C's stdout are both objects wrapping the call
os.write(1, "string") in Python and write(1, "string", strlen("string")) in
C, or some other function on platforms using other mechanisms for output.

In that way sys.stdout in Python and stdout in C will be portable to many
more systems.
 
H

hg

Robert said:
hg said:
Robert said:
hg wrote:

Hi,

I have the following

********************* C extention - redir.c


#include "Python.h"

PyObject * test_redir_test(PyObject *self) {
fprintf(stdout, "Hello from an extention!\n");
Py_INCREF(Py_None);
return Py_None;
}

[...]
*********************** python test script: test.py:
import sys


class My_Stdout:
def write(self, p_string):
l_file = open('res.txt','a')
l_file.write(p_string)
l_file.close


sys.stdout = My_Stdout()

print 'toto'
import test_redir



test_redir.test()



**************** Question:

print 'toto' does go to "res.txt" while "Hello from an extention!\n"
goes to the console.

Any clue ?

There is no portable way to change the location of stdout during
execution of a program. If you want to print with whatever is sys.stdout
from an extension module, you should call sys.stdout's write method
dynamically from C.

Robert, thanks,

I understand that sys.stdout and stdout of an extention are two different
entities ... correct ?

Yes. Python's sys.stdout and C's stdout are both objects wrapping the call
os.write(1, "string") in Python and write(1, "string", strlen("string"))
in C, or some other function on platforms using other mechanisms for
output.

In that way sys.stdout in Python and stdout in C will be portable to many
more systems.


Thanks Robert.

hg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top