473,219 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,219 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 1352
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Tsai Li Ming | last post by:
Dear all, I have a problem with a redirecting stdout and stderr. I am a top level module and has no control over the imported modules that are making system calls such as os.system or popen2.* ....
4
by: Leon | last post by:
"stdout" is file object, it open mode is "w" and it have a close() fuction..... while it run close(), how to reopen it because I want to do rewrite it stdout content update
2
by: Mo | last post by:
Hi all, I hope that someone out there can help me with a problem I have been having. I have just finished creating a website and have added a header to most of the pages on my site. The header...
2
by: EMW | last post by:
Hi, Can someone please post a link for the download of Smart Device Extentions Beta 1? I need this SDE to be able to create programs for the pocket pc with vb.NET. Microsoft doesn't have it...
1
by: A.M-SG | last post by:
Hi, Can I view/modify SOAP message at the client proxy side without using soap extensions? Thank you,
2
by: jt | last post by:
I need to create a DLL Extension. I going to write it in C. Can you point me to any information on how to write DLL extentions? I just don't know how the calling conventions are, etc... ...
9
by: Santtu Nyrhinen | last post by:
Hi, Let say that I have a function like void writeHello() { printf("Hello"); } Now I need to make an automated test fot that function. The test function returns 1 for successful and 0 for...
5
by: =?gb2312?B?yMvR1MLkyNXKx8zs0cSjrM37vKvM7NHEsru8+7z | last post by:
I wanna print the log to both the screen and file, so I simulatered a 'tee' class Tee(file): def __init__(self, name, mode): file.__init__(self, name, mode) self.stdout = sys.stdout...
5
by: Joakim Hove | last post by:
Hello, I have written a program in C; this programs uses an external proprietary library. When calling a certain function in the external library, the particular function writes a message to...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.