By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,112 Members | 1,584 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,112 IT Pros & Developers. It's quick & easy.

Subprocess confusion: how file-like must stdin be?

P: n/a
Question:
import subprocess, StringIO

input = StringIO.StringIO("abcdefgh\nabc\n")
# I don't know of a compact, evocative, and
# cross-platform way to exhibit this behavior.
# For now, depend on cat(1).
p = subprocess.Popen(["cat"], stdout = subprocess.PIPE,
stdin = response)

Why this is a question:
A. it tosses an AttributeError.
B. I *expected* it to do the equivalent of
cat << HERE
abcdefgh
abc
HERE

In <URL: http://docs.python.org/dev/lib/node530.html >, I read "Valid
values are ... an existing file object ..." Even though StringIO is
a "file-like object", it lacks a fileno. Is there a way to get what
I'm after?
Aug 17 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Dennis Lee Bieber <wl*****@ix.netcom.comwrote:
On Thu, 17 Aug 2006 17:16:25 +0000, cl****@lairds.us (Cameron Laird)
declaimed the following in comp.lang.python:
Question:
import subprocess, StringIO

input = StringIO.StringIO("abcdefgh\nabc\n")

Here you override the builtin function "input()"
# I don't know of a compact, evocative, and
# cross-platform way to exhibit this behavior.
# For now, depend on cat(1).
p = subprocess.Popen(["cat"], stdout = subprocess.PIPE,
stdin = response)

Here you specify the non-existant "response"
Assume the OP meant to write this
>>import subprocess, StringIO
inp = StringIO.StringIO("abcdefgh\nabc\n")
p = subprocess.Popen(["cat"], stdout = subprocess.PIPE, stdin = inp)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.4/subprocess.py", line 534, in __init__
(p2cread, p2cwrite,
File "/usr/lib/python2.4/subprocess.py", line 830, in _get_handles
p2cread = stdin.fileno()
AttributeError: StringIO instance has no attribute 'fileno'
>>>


--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Aug 18 '06 #2

P: n/a
In article <sl*****************@irishsea.home.craig-wood.com>,
Nick Craig-Wood <ni**@craig-wood.comwrote:
>Dennis Lee Bieber <wl*****@ix.netcom.comwrote:
> On Thu, 17 Aug 2006 17:16:25 +0000, cl****@lairds.us (Cameron Laird)
declaimed the following in comp.lang.python:
Question:
import subprocess, StringIO

input = StringIO.StringIO("abcdefgh\nabc\n")

Here you override the builtin function "input()"
# I don't know of a compact, evocative, and
# cross-platform way to exhibit this behavior.
# For now, depend on cat(1).
p = subprocess.Popen(["cat"], stdout = subprocess.PIPE,
stdin = response)

Here you specify the non-existant "response"

Assume the OP meant to write this
>>>import subprocess, StringIO
inp = StringIO.StringIO("abcdefgh\nabc\n")
p = subprocess.Popen(["cat"], stdout = subprocess.PIPE, stdin = inp)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.4/subprocess.py", line 534, in __init__
(p2cread, p2cwrite,
File "/usr/lib/python2.4/subprocess.py", line 830, in _get_handles
p2cread = stdin.fileno()
AttributeError: StringIO instance has no attribute 'fileno'
>>>>
Aug 18 '06 #3

P: n/a
Cameron Laird a écrit :
In article <sl*****************@irishsea.home.craig-wood.com>,
Nick Craig-Wood <ni**@craig-wood.comwrote:
>Dennis Lee Bieber <wl*****@ix.netcom.comwrote:
>> On Thu, 17 Aug 2006 17:16:25 +0000, cl****@lairds.us (Cameron Laird)
declaimed the following in comp.lang.python:

Question:
import subprocess, StringIO

input = StringIO.StringIO("abcdefgh\nabc\n")
Here you override the builtin function "input()"
# I don't know of a compact, evocative, and
# cross-platform way to exhibit this behavior.
# For now, depend on cat(1).
p = subprocess.Popen(["cat"], stdout = subprocess.PIPE,
stdin = response)
Here you specify the non-existant "response"
Assume the OP meant to write this
>>>>import subprocess, StringIO
inp = StringIO.StringIO("abcdefgh\nabc\n")
p = subprocess.Popen(["cat"], stdout = subprocess.PIPE, stdin = inp)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.4/subprocess.py", line 534, in __init__
(p2cread, p2cwrite,
File "/usr/lib/python2.4/subprocess.py", line 830, in _get_handles
p2cread = stdin.fileno()
AttributeError: StringIO instance has no attribute 'fileno'
.
.
.
Yes; my apologies for the confusion I introduced by "editing
for publication", and doing it badly.

Your interactive session does indeed exhibit the behavior that
puzzles me. My expectation was that StringIO and the std*
parameters to Popen() were made for each other; certainly there
are many cases where stdout and stderr can be redirected *to* a
StringIO. Is it simply the case that stdin demands a more
file-like object? While that disappoints me, I certainly can
program around it. My question, then: does stdin effectively
require something really in the filesystem, or perhaps the
stdout of a previous subprocess? Is there no built-in way to
feed it an in-memory construct?
As this is a pipe at OS level, there may be no other way than using
os-level tools (ie. real files with fileno), maybe creating an anonymous
pipe, writing to it (relying on pipe buffering by the OS to avoid the
creation of a writer thread), and giving this pipe (which should have a
fileno) to the subprocess.Popen stdin parameter.
Such a construction (pipe/writer thread) would be welcome as standard
subprocess tool.
A+

Laurent.

Aug 18 '06 #4

P: n/a
Cameron Laird wrote:
Your interactive session does indeed exhibit the behavior that
puzzles me. My expectation was that StringIO and the std*
parameters to Popen() were made for each other; certainly there
are many cases where stdout and stderr can be redirected *to* a
StringIO. Is it simply the case that stdin demands a more
file-like object? While that disappoints me, I certainly can
program around it. My question, then: does stdin effectively
require something really in the filesystem, or perhaps the
stdout of a previous subprocess? Is there no built-in way to
feed it an in-memory construct?
set the appropriate stream to subprocess.PIPE, and write to it.

p = subprocess.Popen(..., stdin=subprocess.PIPE)
p.stdin.write("hello")
p.stdin.close() # signal end of file

</F>

Aug 18 '06 #5

P: n/a
In article <ma***************************************@python. org>,
Fredrik Lundh <fr*****@pythonware.comwrote:
>Cameron Laird wrote:
>Your interactive session does indeed exhibit the behavior that
puzzles me. My expectation was that StringIO and the std*
parameters to Popen() were made for each other; certainly there
are many cases where stdout and stderr can be redirected *to* a
StringIO. Is it simply the case that stdin demands a more
file-like object? While that disappoints me, I certainly can
program around it. My question, then: does stdin effectively
require something really in the filesystem, or perhaps the
stdout of a previous subprocess? Is there no built-in way to
feed it an in-memory construct?

set the appropriate stream to subprocess.PIPE, and write to it.

p = subprocess.Popen(..., stdin=subprocess.PIPE)
p.stdin.write("hello")
p.stdin.close() # signal end of file
Aug 18 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.