472,805 Members | 1,102 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Python Interactive Shell - outputting to stdout?

Hello,

I was unable to use popen2.popen4 to grab python.exe's (2.3) output, for
starts, it doesn't show the version information at the beginning and
won't return anything when writing to the stdin pipe, it seems that if I
give it some error nous expression, the pipe would return the exception
data, though nothing else comes through.

A friend of mine also tried this using win32api on delphi, and got the
same result.

I also tried "python.exe > data.txt" and was amazed to find out that it
won't redirect the output to the file.

So, may someone please shed some light over this?

Avi.
Jul 18 '05 #1
6 3492
Avi Berkovich wrote:
Hello,

I was unable to use popen2.popen4 to grab python.exe's (2.3) output, for
starts, it doesn't show the version information at the beginning and
won't return anything when writing to the stdin pipe, it seems that if I
give it some error nous expression, the pipe would return the exception
data, though nothing else comes through.

A friend of mine also tried this using win32api on delphi, and got the
same result.

I also tried "python.exe > data.txt" and was amazed to find out that it
won't redirect the output to the file.

So, may someone please shed some light over this?

Avi.


Well, your first misunderstanding appears to be the omission of the
standard error file. Interactive messages and prompts are written to
stderr, output to stdout:

C:\Steve>python.exe > python.txt
Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
123
345
^Z

C:\Steve>type python.txt
123
345

When you try to write a program that communicates with a client that's
intended for interactive use (such as the Python interpreter when called
with no arguments), you have the problem of divining exactly when the
client is waiting for input.

Rather than blame the Python interpreter, first write a program that
interacts with (say) the Windows command-line interpreter.

If your experience is substantially different then it *might* be a
problem with the interpreter. If your experience is more or less the
same then the problem is probably to do with the nature of the
interactions, which are bad enough when only considering stdin and
stdout. When you add the stderr stream into the picture it's sometimes
impossible to know when the interactive client is waiting for input.

One final comment: you haven't so far said *why* you want to interact
with Python in this way. Given the availability of exec and eval(), just
what is it that you are trying to do that they won't let you?

regards
Steve
--
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
Holden Web LLC +1 703 861 4237 +1 800 494 3119
Jul 18 '05 #2
Hey Steve,

I did write a program to deal with the windows command interpreter, and
it works.

I don't need to do this, but a friend of mine needed to issue commands
to the interpreter via pipes from a non python program, and he has a
fully functional component for using pipes, and has done it with other
interpreters before.

I guess I'll have to separate the std's and continue from there.

Thanks for your input.

Avi.
Jul 18 '05 #3
Hey,

I can't make it work, I don't get any data from either stdout nor stderr.
If I send lines and then close the stdin pipe, I may get an exception
message from several lines up.

I tried manually reading from the stdout pipe, but it just blocks and
hangs no matter what I send over via the stdin pipe.

This behavior isn't presented by the command line interpreter by any chance.

Any suggestions?
Jul 18 '05 #4
Avi Berkovich wrote:
Hey,

I can't make it work, I don't get any data from either stdout nor stderr.
If I send lines and then close the stdin pipe, I may get an exception
message from several lines up.

I tried manually reading from the stdout pipe, but it just blocks and
hangs no matter what I send over via the stdin pipe.

This behavior isn't presented by the command line interpreter by any
chance.

Any suggestions?


Yes: post your code along with messages (if any)!

regards
Steve
--
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
Holden Web LLC +1 703 861 4237 +1 800 494 3119
Jul 18 '05 #5
Steve Holden wrote:
Avi Berkovich wrote:
Hey,

I can't make it work, I don't get any data from either stdout nor stderr.
If I send lines and then close the stdin pipe, I may get an exception
message from several lines up.

I tried manually reading from the stdout pipe, but it just blocks and
hangs no matter what I send over via the stdin pipe.

This behavior isn't presented by the command line interpreter by any
chance.

Any suggestions?

Yes: post your code along with messages (if any)!

regards
Steve


Sorry, for some reason I can no longer find the message you posted the
code in -- it didn't get correctly threaded in Mozilla. Anyway, I can't
say that I exactly understand what's going on, but here are a couple of
observations:

1. The interpreter performs buffering even when running in interactive
mode unless it can see a terminal (which here it can't). Hence adding a
"-u" to the interpreter command line is useful.

2. There's a problem with your stop condition, which is why the attached
modified version sleeps before closing the pipe to the interpreter.

import threading
import sys
import popen2

class Piper(threading.Thread):

def __init__(self, readPipe, output):
threading.Thread.__init__(self)

if not isinstance(readPipe, file):
raise TypeError, "readPipe parameter must be of File type"
#if not isinstance(output, file):
# raise TypeError, "output parameter must be of File type"

self.readPipe = readPipe
self.output = output
self.toStop = False

def run(self):
print "Running"
while not self.toStop and not self.readPipe.closed:
read = self.readPipe.readline()
self.output.write(read)
self.output.flush()
print "Stopped"

def stop(self):
self.toStop = True
if __name__ == "__main__":
r, w = popen2.popen4('c:\\python24\\python.exe -u')
piper = Piper(r, sys.stdout)
piper.start()
w.write("print 'Hello!'\r\n")
w.flush()
w.write("print 'Goodbye!'\r\n")
w.flush()
import time; time.sleep(2)
w.close()
#import time; time.sleep(3)
#r.close()
piper.stop()

Various other bits and pieces of messing about didn't really yield any
useful conclusions, so I pass this on only in the hope that you may be
more motivated to follow up now you can actually see some interpreter
output. I would have thought the flush() calls would have made output
appear one line at a time, but sadly they do not.

regards
Steve
--
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
Holden Web LLC +1 703 861 4237 +1 800 494 3119
Jul 18 '05 #6

Hey Steve,

Well, I've tried flush() before, but I didn't know about the "-u" switch.

Thank you for tinkering on this, I shall post again if I make any progress.

Avi
Steve Holden wrote:
Steve Holden wrote:
Avi Berkovich wrote:
Hey,

I can't make it work, I don't get any data from either stdout nor
stderr.
If I send lines and then close the stdin pipe, I may get an exception
message from several lines up.

I tried manually reading from the stdout pipe, but it just blocks and
hangs no matter what I send over via the stdin pipe.

This behavior isn't presented by the command line interpreter by any
chance.

Any suggestions?


Yes: post your code along with messages (if any)!

regards
Steve

Sorry, for some reason I can no longer find the message you posted the
code in -- it didn't get correctly threaded in Mozilla. Anyway, I can't
say that I exactly understand what's going on, but here are a couple of
observations:

1. The interpreter performs buffering even when running in interactive
mode unless it can see a terminal (which here it can't). Hence adding a
"-u" to the interpreter command line is useful.

2. There's a problem with your stop condition, which is why the attached
modified version sleeps before closing the pipe to the interpreter.

import threading
import sys
import popen2

class Piper(threading.Thread):

def __init__(self, readPipe, output):
threading.Thread.__init__(self)

if not isinstance(readPipe, file):
raise TypeError, "readPipe parameter must be of File type"
#if not isinstance(output, file):
# raise TypeError, "output parameter must be of File type"

self.readPipe = readPipe
self.output = output
self.toStop = False

def run(self):
print "Running"
while not self.toStop and not self.readPipe.closed:
read = self.readPipe.readline()
self.output.write(read)
self.output.flush()
print "Stopped"

def stop(self):
self.toStop = True
if __name__ == "__main__":
r, w = popen2.popen4('c:\\python24\\python.exe -u')
piper = Piper(r, sys.stdout)
piper.start()
w.write("print 'Hello!'\r\n")
w.flush()
w.write("print 'Goodbye!'\r\n")
w.flush()
import time; time.sleep(2)
w.close()
#import time; time.sleep(3)
#r.close()
piper.stop()

Various other bits and pieces of messing about didn't really yield any
useful conclusions, so I pass this on only in the hope that you may be
more motivated to follow up now you can actually see some interpreter
output. I would have thought the flush() calls would have made output
appear one line at a time, but sadly they do not.

regards
Steve

Jul 18 '05 #7

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

Similar topics

3
by: Christoph Becker-Freyseng | last post by:
Hello, is there a way to dump (and save) the command-history of the python interactive mode. Thanks, Christoph Becker-Freyseng
3
by: Tomas | last post by:
Hi! I'm looking for a replacement for the standard interactive python shell. So far I've tried IPython and PyCrust. I liked both, but I'm not 100% happy with any of them. My main complaint about...
11
by: rmm | last post by:
If I replace the open builtin eg import main __main__.__builtins__.open=None Is there any way, from here on, to access the original open function?? Extending this slightly, lets say I put a...
5
by: A. L. | last post by:
In Python interactive mode, is there some function acting like 'clear' command in bash? Could somebody here give some advice? Thanks in advance.
4
by: tnoell | last post by:
Hi comp.lang.python: New to the group and new to python, so don't tear me up too much ... I installed the GNU readline support in python2.4, and it is working, but there is one annoying behaviour...
2
by: Bo Peng | last post by:
Dear list, This may sound strange but I need to start a python shell from python. The motivation is that I have a bunch of (numeric) python functions to provide to a user. The best way I can...
0
by: pkassianidis | last post by:
Hello everybody, I have written a python script which executes some functions and then returns to the python interactive shell prompt (In other words I use the command "#!/usr/bin/python -i "...
2
by: NightHawk | last post by:
Im not a total noob but i don't know the command and the module to go from python to the default shell. (not from interactive mode - $python)
1
by: Charles Cheever | last post by:
Hi, We're releasing a readline-based interactive shell for PHP called phpsh. Users can enter php statements and expressions and they will be evaluated on the fly. Here is a transcript of a...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?

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.