473,404 Members | 2,137 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,404 software developers and data experts.

redirect output from embedded C module

hi all.. I'm writing an application in python2.3 under linux debian
which accepts new "plug-ins" in the form of c-written python extension
modules.
My problem is that I'd like to catch the stdout/stderr from one of
this modules
and redirect it into a tkinter text widget, in real time. Let's say
the module's
name is plugin and that plugin.doSomething() prints out with a C
printf()
the string "it's a mess".

Redirecting sys.stdout before calling plugin.doSomething() doesn't
work, I guess
because the plugin code has a different stdout file then python. So
the second solution is to force every plugin-writer to embed in every
plugin the C lines:

if ((fd = open("outfile", O_WRONLY | O_CREAT | O_TRUNC, 0600)) == -1)
{
perror("open outfile");
exit(2);
}
dup2(fd, fileno(stdout));

and use as "outfile" the real stout. Then redirect python stdout to
the text widget.
But how do I access python stdout from C?
or even better how do I access the text widget directly from C?
or, since this solution looks quite crappy, how do I do it in another
way?

thanks to all
legba
Jul 18 '05 #1
3 3271
legba wrote:
hi all.. I'm writing an application in python2.3 under linux debian
which accepts new "plug-ins" in the form of c-written python extension
modules.
My problem is that I'd like to catch the stdout/stderr from one of
this modules
and redirect it into a tkinter text widget, in real time. Let's say
the module's
name is plugin and that plugin.doSomething() prints out with a C
printf()
the string "it's a mess".

Redirecting sys.stdout before calling plugin.doSomething() doesn't
work, I guess
because the plugin code has a different stdout file then python. So
the second solution is to force every plugin-writer to embed in every
plugin the C lines:

if ((fd = open("outfile", O_WRONLY | O_CREAT | O_TRUNC, 0600)) == -1)
{
perror("open outfile");
exit(2);
}
dup2(fd, fileno(stdout));

and use as "outfile" the real stout. Then redirect python stdout to
the text widget.
But how do I access python stdout from C?
Use the API functions PySys_WriteStdout(format, ...)
and PySys_WriteStderr(format, ...)

HTH,

Vincent Wehren
or even better how do I access the text widget directly from C?
or, since this solution looks quite crappy, how do I do it in another
way?

thanks to all
legba

Jul 18 '05 #2
>
Use the API functions PySys_WriteStdout(format, ...)
and PySys_WriteStderr(format, ...)


the main problem here is that I do not control all the code I'm using
as plugins.
I'd like people to mix existent code with my program and I can't ask
them to convert every printf with PySys_WriteStdout...

thanks,
legba.
Jul 18 '05 #3
15 Jun 2004 03:29:42 -0700, na comp.lang.python, legba napisał(a):
hi all.. I'm writing an application in python2.3 under linux debian
which accepts new "plug-ins" in the form of c-written python extension
modules.
My problem is that I'd like to catch the stdout/stderr from one of
this modules
and redirect it into a tkinter text widget, in real time. Let's say
the module's
name is plugin and that plugin.doSomething() prints out with a C
printf()
the string "it's a mess".

in initialization set C/python code
---------------

m = initmojeokna((TForm*)Owner);
c=PyRun_SimpleString("import mojeokna\n");
PyObject* mod= PyImport_ImportModule("mojeokna");
c=PyRun_SimpleString("import sys\n"
"sys.stdout=mojeokna.openwin()\n"
"sys.stderr=sys.stdout\n");

---------------------
where [mojeokna = polish mywindows] .openwin() is contructor of standard
Python object with 'write' method.

Work well in Pytnon 2.0/2.1 in Junly 2002.
Im not sure, but 2.3 can have one or two places where it writes to
'hardwired' stdout, not wirtual sys.stdout.

if ((fd = open("outfile", O_WRONLY | O_CREAT | O_TRUNC, 0600)) == -1)
dup2(fd, fileno(stdout));


Will not work.
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: newgene | last post by:
Hi, group, I guess it is a simple question for you, but it puzzles me for a while. I use a wxTextCtrl in my application for output result and any exception msg. I know I can add new msg onto...
7
by: Donna Hawkins | last post by:
I want to use javascript to redirect to a URL which has been passed as a variable (in php). I have searched but cannot find any solution. I think this code is a basic redirect: <script...
9
by: MNQ | last post by:
Hi All I want to use my parallel port of my PC to control some external devices. I am writing a program in ANSI C using the PacificC compiler. What I need to know is how to access the parallel...
8
by: Andreas Klemt | last post by:
Hello, I get this error Message "cannot redirect after http headers have been sent" when I do this response.redirect ("home.aspx") How can I find out with vb.net if already a http header has...
1
by: abcd | last post by:
I have a program which is written in C and interfaced with python via Swig. However, the function I call prints stuff out to the console. I would like to capture what it is printing out. I...
6
by: Uday | last post by:
Hi everyone, I have a ASP page that triggers a db-side stored procedure. At the end of the procedure, it spits out a log file, that this ASP page reads and displays for the users. But the...
1
by: Harel | last post by:
I'm using httplib.HTTPConnection to perform http connections and during the beta period of the application I wanted to log its verbose output . However, raising the debuglevel just outputs it to...
4
by: Chris8Boyd | last post by:
I am embedding Python in a MSVC++ (2005) application. The application creates some environment and then launches a Python script that will call some functions exported from the MSVC++ application....
18
by: Paul Lautman | last post by:
JRough wrote: What do you mean by "redirect the output to Excel"??? Excel isn't a location, it's a spreadsheet program that some (but not all users) will have on their machine. BTW, Location:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.