Hi,
My application has python embedded into it. I noticed that when I run any
python code the output is buffered and doesn't get flushed until my
application exits. To fix this I simply flush sys.stdout and sys.stderr
every once in while by using the following code:
//Get handle to python stdout file and flush it
PyObject *pyStdout = PySys_GetObject ("stdout");
if(pyStdout && PyFile_Check(py Stdout)) {
PyObject *result = PyObject_CallMe thod(pyStdout," flush",NULL);
Py_XDECREF(resu lt);
}
//Get handle to python stderr file and flush it
PyObject *pyStderr = PySys_GetObject ("stderr");
if(pyStderr && PyFile_Check(py Stderr)) {
PyObject *result = PyObject_CallMe thod(pyStderr," flush",NULL);
Py_XDECREF(resu lt);
}
This works for stdout but not for stderr. In order to flush stderr I have to
do:
PyRun_SimpleStr ing("sys.stderr .flush()");
Does anybody know why my first method doesn't work with stderr? I checked
and PyObject_CallMe thod is being called for both objects and no errors are
occuring. The solution I have is acceptable, but I would still like to know
why my first method didn't work.
BTW, I'm running python 2.3 on Windows XP
Thanks,
Farshid 6 4440
"Farshid Lashkari" <la********@SPA Mworldviz.com> wrote in message news:<VoRjd.33$ iY3.7@trnddc01> ... Hi,
My application has python embedded into it. I noticed that when I run any python code the output is buffered and doesn't get flushed until my application exits. To fix this I simply flush sys.stdout and sys.stderr every once in while by using the following code:
//Get handle to python stdout file and flush it PyObject *pyStdout = PySys_GetObject ("stdout"); if(pyStdout && PyFile_Check(py Stdout)) { PyObject *result = PyObject_CallMe thod(pyStdout," flush",NULL); Py_XDECREF(resu lt); }
//Get handle to python stderr file and flush it PyObject *pyStderr = PySys_GetObject ("stderr"); if(pyStderr && PyFile_Check(py Stderr)) { PyObject *result = PyObject_CallMe thod(pyStderr," flush",NULL); Py_XDECREF(resu lt); }
This works for stdout but not for stderr. In order to flush stderr I have to do:
PyRun_SimpleStr ing("sys.stderr .flush()");
Does anybody know why my first method doesn't work with stderr? I checked and PyObject_CallMe thod is being called for both objects and no errors are occuring. The solution I have is acceptable, but I would still like to know why my first method didn't work.
BTW, I'm running python 2.3 on Windows XP
Thanks,
Farshid
I'm sorry to notice that you haven't received any responses back to
your questions even though you have found a workaround. I've noticed
this trend quite a lot in the python community and having learned
python first and now C(in order to understand python better) I'm
frankly disspapointed. I believe python and C are a wonderful marriage
but I suppose because many ppl come to python as their intro language
they info on extending and embedding is limited. I myself haven't
started using the python api but will soon, I hope I for one will be
someone who will be able to provide an answer next time.
Good luck.
BTW: from a C perspective. I don't understand your problem though.
stdout and stderr are the normal output device for your system -
usally your screen. I don't see why you should have to call one via
python sys module.
Perhaps because one doesn't normally flush(stderr) explicity but
flush(stdout).
interested to know as well.
I don't understand it from a C perspective either. I poked around the source
code for python and found where it is flushing the file. It is simply
calling fflush on the FILE handle. Also, I'm somewhat bewildered as to why
running the string "sys.stderr.flu sh()" is different than directly calling
the flush method of the stderr object. Oh well, I guess some things are
better left unknown.
"caroundw5h " <ca********@yah oo.com> wrote in message
news:aa******** *************** ***@posting.goo gle.com... "Farshid Lashkari" <la********@SPA Mworldviz.com> wrote in message
news:<VoRjd.33$ iY3.7@trnddc01> ... Hi,
My application has python embedded into it. I noticed that when I run
any python code the output is buffered and doesn't get flushed until my application exits. To fix this I simply flush sys.stdout and sys.stderr every once in while by using the following code:
//Get handle to python stdout file and flush it PyObject *pyStdout = PySys_GetObject ("stdout"); if(pyStdout && PyFile_Check(py Stdout)) { PyObject *result = PyObject_CallMe thod(pyStdout," flush",NULL); Py_XDECREF(resu lt); }
//Get handle to python stderr file and flush it PyObject *pyStderr = PySys_GetObject ("stderr"); if(pyStderr && PyFile_Check(py Stderr)) { PyObject *result = PyObject_CallMe thod(pyStderr," flush",NULL); Py_XDECREF(resu lt); }
This works for stdout but not for stderr. In order to flush stderr I
have to do:
PyRun_SimpleStr ing("sys.stderr .flush()");
Does anybody know why my first method doesn't work with stderr? I
checked and PyObject_CallMe thod is being called for both objects and no errors
are occuring. The solution I have is acceptable, but I would still like to
know why my first method didn't work.
BTW, I'm running python 2.3 on Windows XP
Thanks,
Farshid I'm sorry to notice that you haven't received any responses back to your questions even though you have found a workaround. I've noticed this trend quite a lot in the python community and having learned python first and now C(in order to understand python better) I'm frankly disspapointed. I believe python and C are a wonderful marriage but I suppose because many ppl come to python as their intro language they info on extending and embedding is limited. I myself haven't started using the python api but will soon, I hope I for one will be someone who will be able to provide an answer next time.
Good luck.
BTW: from a C perspective. I don't understand your problem though. stdout and stderr are the normal output device for your system - usally your screen. I don't see why you should have to call one via python sys module. Perhaps because one doesn't normally flush(stderr) explicity but flush(stdout).
interested to know as well.
Quoth "Farshid Lashkari" <la********@SPA Mworldviz.com>:
| I don't understand it from a C perspective either. I poked around the source
| code for python and found where it is flushing the file. It is simply
| calling fflush on the FILE handle. Also, I'm somewhat bewildered as to why
| running the string "sys.stderr.flu sh()" is different than directly calling
| the flush method of the stderr object. Oh well, I guess some things are
| better left unknown.
It could be different if the stderr object - i.e., PySys_GetObject ("stderr") -
is actually not sys.stderr. I don't understand why, but am not familiar
with PySys_*, so I offer that as a hypothesis you might be able to test.
By the way, since as you observed it's only calling fflush(), I think you
could leave Python out of it altogether and do this with just
fflush(stdout);
fflush(stderr);
Donn Cave, do**@u.washingt on.edu
Here's the code for PySys_GetObject . It's getting the object from the sys
dictionary, so it should be the correct object.
PyObject *
PySys_GetObject (char *name)
{
PyThreadState *tstate = PyThreadState_G et();
PyObject *sd = tstate->interp->sysdict;
if (sd == NULL)
return NULL;
return PyDict_GetItemS tring(sd, name);
}
I've tried calling fflush on stderr, but that doesn't work either.
I've encountered another weird problem also. If my python code causes an
error and I don't flush stderr, then I get a runtime error when my app
exits. It also prints out:
Exception exceptions.Name Error: "global name 'y' is not defined" in 'garbage
collection' ignored
Fatal Python error: unexpected exception during garbage collection.
Does this give anybody a clue as to what's happening?
"Donn Cave" <do**@drizzle.c om> wrote in message
news:1100065836 .697971@yasure. .. Quoth "Farshid Lashkari" <la********@SPA Mworldviz.com>: | I don't understand it from a C perspective either. I poked around the
source | code for python and found where it is flushing the file. It is simply | calling fflush on the FILE handle. Also, I'm somewhat bewildered as to
why | running the string "sys.stderr.flu sh()" is different than directly
calling | the flush method of the stderr object. Oh well, I guess some things are | better left unknown.
It could be different if the stderr object - i.e.,
PySys_GetObject ("stderr") - is actually not sys.stderr. I don't understand why, but am not familiar with PySys_*, so I offer that as a hypothesis you might be able to test. By the way, since as you observed it's only calling fflush(), I think you could leave Python out of it altogether and do this with just fflush(stdout); fflush(stderr);
Donn Cave, do**@u.washingt on.edu
Quoth "Farshid Lashkari" <la********@SPA Mworldviz.com>:
....
| I've tried calling fflush on stderr, but that doesn't work either.
OK, this would seem to mean that your sys.stderr has been replaced
with something else. I guess I don't know for sure what you can
count on in Windows, but I would have to expect that fflush on stderr
really does work - that is, it flushes stderr's buffer.
| I've encountered another weird problem also. If my python code causes an
| error and I don't flush stderr, then I get a runtime error when my app
| exits. It also prints out:
|
| Exception exceptions.Name Error: "global name 'y' is not defined" in 'garbage
| collection' ignored
| Fatal Python error: unexpected exception during garbage collection.
|
| Does this give anybody a clue as to what's happening?
Doesn't do anything for me, but you may find out what that is when you
locate the code for the faux stderr you're (not) flushing. I would be
more worried about this 'y' thing in principle, though, because it sounds
like a problem in the C (or whatever) to Python interface that could have
other unpleasant consequences.
Donn Cave, do**@drizzle.co m
Well I've somewhat solved my problem. First off, the 'y' problem was just an
error I purposely created in order to trigger an exception. My application
uses a bunch of calls to PyObject_Call to run python code. After each of
these calls I added:
if(PyErr_Occurr ed()) PyErr_Print();
This will force the errors to be printed to stderr, and now calling the
flush method on the stderr object works. I still don't understand why
running the string "sys.stderr.flu sh()" is different. Maybe it forces any
current errors to be printed and then flushes them too. My app doesn't
replace the python stderr object either.
Anyways, thanks to everybody for your help and suggestions.
-Farshid
"Donn Cave" <do**@drizzle.c om> wrote in message
news:1100156058 .567069@yasure. .. Quoth "Farshid Lashkari" <la********@SPA Mworldviz.com>: ... | I've tried calling fflush on stderr, but that doesn't work either.
OK, this would seem to mean that your sys.stderr has been replaced with something else. I guess I don't know for sure what you can count on in Windows, but I would have to expect that fflush on stderr really does work - that is, it flushes stderr's buffer.
| I've encountered another weird problem also. If my python code causes an | error and I don't flush stderr, then I get a runtime error when my app | exits. It also prints out: | | Exception exceptions.Name Error: "global name 'y' is not defined" in
'garbage | collection' ignored | Fatal Python error: unexpected exception during garbage collection. | | Does this give anybody a clue as to what's happening?
Doesn't do anything for me, but you may find out what that is when you locate the code for the faux stderr you're (not) flushing. I would be more worried about this 'y' thing in principle, though, because it sounds like a problem in the C (or whatever) to Python interface that could have other unpleasant consequences.
Donn Cave, do**@drizzle.co m This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Vincent Touquet |
last post by:
Hi,
In a project where I have embedded Python in a C++ application, I have
the need to replace what Python considers to be stdin, stdout and/or
stderr.
In sysmodule.c in the Python sources, I find the following lines of
code:
sysin = PyFile_FromFile(stdin, "<stdin>", "r", NULL);
|
by: gf gf |
last post by:
Is there any way to make Python's print() flush
automatically, similar to...mmm...that other
language's $|=1 ?
If not, how can I flush it manually?
sys.stdout.flush() didn't seem to work.
__________________________________
|
by: Bryan Olson |
last post by:
Here's a module to show stderr output from console-less Python
apps, and stay out of the way otherwise. I plan to make a ASPN
recipe of it, but I thought I'd run it by this group first.
To use it, import the module. That's it. Upon import it will
assign sys.stderr.
In the normal case, your code is perfect so nothing ever gets
written to stderr, and the module won't do much of anything.
Upon the first write to stderr, if any, the...
|
by: Xah Lee |
last post by:
Python Doc Problem Example: os.system
Xah Lee, 2005-09
today i'm trying to use Python to call shell commands. e.g. in Perl
something like
output=qx(ls)
in Python i quickly located the the function due to its
|
by: Stewart Midwinter |
last post by:
I have a Tkinter app running on cygwin. It includes a Test menu item
that does nothing more than fetch a directory listing and display it
in a Toplevel window (I'd use a tkMessageBox showinfo widget, but for
some reason the text is invisible on cygwin).
After I close the Toplevel widget, all of the menus in my app behave
as though they have no contents to them, i..e I can press on the File
menu button, and see it depress, but the Exit...
| |
by: A.M |
last post by:
Hi,
I am having difficulty with shell scripting in Python.
I use the following command to run a DOS command and put the return value in
a Python variable:
|
by: Robin Becker |
last post by:
We have a client using a fully embedded python in a large DTP app. It used to be
Mac OS 9/X only, but the MAC 9 support has gone away and we now have support for
the PC with the embedding being used in C# via external DLL aliasing/marshalling
etc etc.
The embedding DLL has effectively a single entry point called _run_string; this
takes an LPStr string and with minor wrapping (to collect sys.stderr) is passed
to an exec statement doing
...
|
by: Vince C. |
last post by:
Hi all.
I've installed Bloodshed Dev-C++ on a Windows 2000 SP4 machine. I'm using
MinGW 3.4.2.
I'd like to temporarily disable standard functions to write to stderr, i.e.
for instance redirect stderr to a temporary file (or /dev/null but is there
an equivalent under Windows? Is it "nul:") and then to *restore* the
default stderr so that standard library functions that write to stderr
produce output again.
|
by: grbgooglefan |
last post by:
I am in a perculiar situation. I want to use PyRun_SimpleString for
creating Python functions in embedded Python in C++.
But there could be cases when Python function code compilation could
fail & PyRun_SimpleString will return -1 as return status. At this
time, it prints the error message to sys.stderr.
So, we do not have any control or cannot use that message to show to
user to correct the errors in the function code.
I could assign an...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |