473,378 Members | 1,417 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,378 software developers and data experts.

Redirecting STDOUT using C calls.

Greetings,
I'm trying to redirect python's stdout to another location. The
reason for this is that I'm embedding python in an application. Now,
originally my code was developed for Linux and that did not require
redirection due to the fact that every X11 application can have an
STDOUT associated with it. I then proceeded to take interest in making
my code portable and looked at compiling it on windows, however Win32
GUI applications are not given an STDOUT by default. One has to first
AllocConsole() and then reassign the handles using magic tokens
"CON(IN|OUT)$" such as freopen("CONOUT$", "w", stdout). I like to keep
code clean and would like to stick to the C API as much as possible.
I dived into the source and figured out two ways of doing it:

------------------------------------------------
#include <Python.h>
#include <iostream>

// Demonstrates redirection after initialisation.
int main() {
Py_Initialize();
FILE* afile = fopen("zig.txt", "w+");
PySys_SetObject("stdout", PyFile_FromFile(afile, "zig.txt",
"wb", fclose));
// I would be using boost::python::exec, but let's stick to basics.
PyRun_SimpleString("print(\"pytest1\")");
Py_Finalize();
fclose(afile);
return 0;
}
------------------------------------------------
#include <Python.h>
#include <cstdio>
#include <iostream>

// Demonstrates redirection before initialisation.
int main() {
FILE* afile = fopen("zig.txt", "w+");
// All of this program's output to file.
stdout = afile;
Py_Initialize();
PyRun_SimpleString("print(\"pytest2\")");
Py_Finalize();
fclose(afile);
return 0;
}
------------------------------------------------

For exemplary purposes, i've used files as the target for
redirection, however the actual target in windows would be the FILE*
gained from opening the "CONOUT$" virtual file. Both of these examples
compile and work fine on linux, however I get an illegal access
exception (a SIGSEGV equivalent i think) on Windows (XP). Why? The
illegal access happens on the line that executed the python print()
function. What is the cause for this and how can i fix it?
It may also help to mention that the following *Python* code works
in Windows:
------------------------------------------------
import sys
sys.stdout = open("CONOUT$", "w", 0)
print("foo")
------------------------------------------------
So if this works, there must be a difference between how the inbuilt
open() opens and assigns the pointers than how the C API does it. Any ideas?

Thank you.
Dec 30 '07 #1
0 1797

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

Similar topics

2
by: Birch | last post by:
I have a python script that uses the print function throughout, and as well uses calls to os.system() to spawn DOS commandline executables. My issue is that I redirect all of the output from this...
1
by: Michael McGarry | last post by:
Hi, How do I redirect stdin, stdout and stderr to a window? I am using Qt for GUI. Thanks, Michael
3
by: Laszlo Zsolt Nagy | last post by:
Hello, I have this code: s = smtplib.SMTP() s.set_debuglevel(1) s.connect(host=smtp_host) s.set_debuglevel(0) log("Connected, sending e-mail") sys.stdout.flush()
15
by: Matt Burland | last post by:
I'm having a problem with redirecting the StandardOutput of a process that I use to run a DOS program in my application. The problem is that I when I start my process (which I do in a separate...
2
by: Jacek | last post by:
Hello! My application has to use external native library writing to stdout and stdin. My goal is to redirect output within running process (no chance to do that in child process - Process class...
9
by: Fuzzyman | last post by:
Hello, I'm trying to redirect standard out in a single namespace. I can replace sys.stdout with a custom object - but that affects all namespaces. There will be code running simultaneously...
8
by: Morpheus | last post by:
I am trying to test a function that outputs text to the standard output, presumably using a cout call. I do not have access to the source, but need to test the output. Is this possible? I can...
1
by: pp4175 | last post by:
Hello Everyone, I am currently working on writing a GUI wrapper for a Menu driven DOS Program. What I was looking on doing is redirecting stdout/stdin to a stream and read/write similar to a...
6
by: drhilbert | last post by:
Dear all, in these days, we have found a problem we can't solve even after long long googling, so we are here asking your precious help. In our program instead of using cout to print messages...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.