471,334 Members | 1,372 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,334 software developers and data experts.

stdout/err and C extentions

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

Jun 12 '07 #1
4 1276
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 Bauck Hamar

Jun 12 '07 #2
hg
Robert Bauck Hamar wrote:
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 Bauck Hamar

Robert, thanks,

I understand that sys.stdout and stdout of an extention are two different
entities ... correct ?
hg
Jun 12 '07 #3
hg wrote:
Robert Bauck Hamar wrote:
>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.

--
Robert Bauck Hamar

Jun 12 '07 #4
hg
Robert Bauck Hamar wrote:
hg wrote:
>Robert Bauck Hamar wrote:
>>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.

--
Robert Bauck Hamar

Thanks Robert.

hg

Jun 12 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Tsai Li Ming | last post: by
4 posts views Thread by Leon | last post: by
2 posts views Thread by EMW | last post: by
2 posts views Thread by jt | last post: by
9 posts views Thread by Santtu Nyrhinen | last post: by
5 posts views Thread by =?gb2312?B?yMvR1MLkyNXKx8zs0cSjrM37vKvM7NHEsru8+7z | last post: by
5 posts views Thread by Joakim Hove | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.