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

Re: what does "python -i" use as input stream (stdin)?

En Thu, 25 Sep 2008 09:49:31 -0300, Almar Klein <al*********@gmail.com>
escribió:
Hi,
I want to start "python -i" from a subprocess and change its stdin
stream,
so I get control over the commands I feed the interpreter.

I thought just changing sys.stdin to my custom file-like object would
suffice, but this does not work. Neither does changing sys.__stdin__.

I guess the interpreter got a reference to the original stdin (the Pipe)
before
I could change it, and is using that instead of sys.stdin.

Any thoughts how I can get the interpreter to use MY custom stream?
Use subprocess.PIPE
Usually the tricky part is to figure out exactly whether there is more
input or not. With Python it's easy, use the ps1 prompt.

--- begin ---
import sys
import subprocess

def read_until_prompt(stdout):
while True:
line = stdout.read(4)
if line==sys.ps1:
yield line
return
else:
line += stdout.readline()
yield line

def print_output(stdout):
for line in read_until_prompt(stdout):
sys.stdout.write(line)

s = subprocess.Popen(["python", '-i'],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
stdin=subprocess.PIPE)
stdout = s.stdout
stdin = s.stdin
print_output(stdout)
stdin.write("dir()\n")
print_output(stdout)
stdin.write("def foo(x):\n")
stdin.write(" for i in range(x):\n")
stdin.write(" print i\n")
stdin.write("\n")
stdin.write("foo(5)\n")
print_output(stdout)
stdout.close()
stdin.close()
s.wait()
print "bye"
--- begin ---

--
Gabriel Genellina

Sep 25 '08 #1
0 1838

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

Similar topics

25
by: Ben | last post by:
Hi all! I learned Python as part of my university coursework... I enjoyed it. Now I'm just wondering how Python compares with asp, jsp, php and what not?? I don't have slightest knowledge about...
4
by: ketulp_baroda | last post by:
Hi Does python support MVC architecture? Java has register & notify obsever methods in javax.util . Does python has these functions. If not then how to register the views with the models & how to...
1
by: chanmy8 | last post by:
does python supported in WinCE/ProcketPC?
5
by: Jason Smith | last post by:
Hi. I just have a question about optimizations Python does when converting to bytecode. import re for someString in someListOfStrings: if re.match('foo', someString): print someString,...
9
by: angel | last post by:
Hi Java and cpp support one function name multi function prototype. For example: A: int func1(String s){} int func1(int i){} B: int func1(String s){}
15
by: Claudio Grondi | last post by:
Let's consider a test source code given at the very end of this posting. The question is if Python allows somehow access to the bytes of the representation of a long integer or integer in...
5
by: Avi Kak | last post by:
Hello: Does Python support a peek like method for its file objects? I'd like to be able to look at the next byte in a disk file before deciding whether I should read it with, say, the read()...
4
by: =?ISO-8859-2?Q?Micha=B3_Bentkowski?= | last post by:
Why does python create a reference here, not just copy the variable? Shouldn't k remain the same? -- Micha³ Bentkowski mr.ecik@gmail.com
0
by: python | last post by:
Does Python 2.5.2's embedded SQLite support full text searching? Any recommendations on a source where one can find out which SQLite features are enabled/disabled in each release of Python? I'm...
9
by: Ed Leafe | last post by:
On Apr 21, 2008, at 1:05 PM, Daniel Fetchinson wrote: Don't most binary distributions include SQLite itself? I installed 2.5.2 on a new WinXP VM, and SQLite is working fine. -- Ed Leafe
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...
0
by: F22F35 | last post by:
I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...

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.