From: Joan Miller on
Does `raw_input` uses internally `sys.stdout.write`?
From: Peter Otten on
Joan Miller wrote:

> Does `raw_input` uses internally `sys.stdout.write`?

You can test this yourself without reading the C source:

Python 2.6.4 (r264:75706, Dec 7 2009, 18:43:55)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> class A:
.... def write(self, s):
.... sys.__stdout__.write(s.upper())
....
>>> sys.stdout = A()
>>> print "yadda"
YADDA
>>> raw_input("foo")
FOObar
'BAR'

Peter
From: Joan Miller on
On 15 feb, 10:11, Peter Otten <__pete...(a)web.de> wrote:
> Joan Miller wrote:
> > Does `raw_input` uses internally `sys.stdout.write`?
>
> You can test this yourself without reading the C source:
>
> Python 2.6.4 (r264:75706, Dec  7 2009, 18:43:55)
> [GCC 4.4.1] on linux2
> Type "help", "copyright", "credits" or "license" for more information.>>> import sys
> >>> class A:
>
> ...     def write(self, s):
> ...             sys.__stdout__.write(s.upper())
> ...>>> sys.stdout = A()
> >>> print "yadda"
> YADDA
> >>> raw_input("foo")
>
> FOObar
> 'BAR'
>
> Peter

It was to display the output inside a GUI app. overriding
`sys.stdout`. And as `print` also uses internally `sys.stdout.write`
then can be used `print` the shell script and get the output too in
the GUI, cann't it?
From: Peter Otten on
Joan Miller wrote:

>> > Does `raw_input` uses internally `sys.stdout.write`?

> It was to display the output inside a GUI app. overriding
> `sys.stdout`. And as `print` also uses internally `sys.stdout.write`
> then can be used `print` the shell script and get the output too in
> the GUI, cann't it?

It should be easy to collect data written with print and show it in a gui,
but I can't see how you would integrate raw_input() into a gui app.

As to shell scripts, you can invoke them via subprocess, or, if the script
needs user interaction, via pexpect.

Peter
From: Peter Otten on
Shashwat Anand wrote:

> raw_input uses sys.stderr I guess ?

I had a look at the C code, but it's a bit confusing.

If I'm reading it correctly the prompt is written to the "real" stderr if
and only if sys.stdin and sys.stdout are attached to a terminal.

$ python -c"raw_input('prompt\n')" 2>tmp.txt
foo
$ cat tmp.txt
prompt
$ python -c"raw_input('prompt\n')" 2>tmp.txt | cat
foo
prompt
$ cat tmp.txt

I wonder if that is intentional.

Peter