473,592 Members | 2,921 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

En Thu, 25 Sep 2008 09:49:31 -0300, Almar Klein <al*********@gm ail.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_prom pt(stdout):
while True:
line = stdout.read(4)
if line==sys.ps1:
yield line
return
else:
line += stdout.readline ()
yield line

def print_output(st dout):
for line in read_until_prom pt(stdout):
sys.stdout.writ e(line)

s = subprocess.Pope n(["python", '-i'],
stdout=subproce ss.PIPE,
stderr=subproce ss.STDOUT,
stdin=subproces s.PIPE)
stdout = s.stdout
stdin = s.stdin
print_output(st dout)
stdin.write("di r()\n")
print_output(st dout)
stdin.write("de f foo(x):\n")
stdin.write(" for i in range(x):\n")
stdin.write(" print i\n")
stdin.write("\n ")
stdin.write("fo o(5)\n")
print_output(st dout)
stdout.close()
stdin.close()
s.wait()
print "bye"
--- begin ---

--
Gabriel Genellina

Sep 25 '08 #1
0 1986

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

Similar topics

25
2647
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 these languages... Are they more advanced than Python? Can Python be integrated with Dreamweaver? Hope some of you can get me out of confusion...
4
2317
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 notify the views that the model has been updated??
1
1913
by: chanmy8 | last post by:
does python supported in WinCE/ProcketPC?
5
2173
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, "matched!" Does Python notice that re.match is called with the same expression, and thus lift it out of the loop? Or do I need to always optimize by hand
9
1633
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
2340
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 computers memory? Or does Python hide such implementation details that deep, that there is no way to get down to them? The test code below shows, that extracting bits from an integer value n is faster when using n&0x01 than when using n%2 and I...
5
4050
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() method. Is it possible to do so in Python? Your answer would be much appreciated.
4
1296
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
967
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 trying to figure out what's available in 2.5.2 as well as what to expect in 2.6 and 3.0. Thank you, Malcolm
9
2592
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
7935
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, 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...
0
7871
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8236
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8227
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6642
agi2029
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...
1
5735
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3851
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...
0
3893
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1202
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.