This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author ngrover
Recipients ngrover
Date 2007-12-15.01:01:32
SpamBayes Score 0.0
Marked as misclassified No
Message-id <1197680493.69.0.0120420737472.issue1631@psf.upfronthosting.co.za>
In-reply-to
Content
It would be nice if we could send output from subprocess.Popen objects
to any object with a write() method.

Consider the following example, where I'm using Python 2.4.4 (#1, Jun 28
2007, 15:10:17, GCC 3.4.3 on linux2)...

>>>
>>> fh = open('/tmp/file.txt', 'w')
>>> cmdObj = subprocess.Popen('uname -a', shell=True, stdout=fh)
>>> fh.close()
>>> fh = open('/tmp/file.txt', 'r')
>>> fh.read()
'Linux northstar 2.6.9-42.0.2.IT111843.1.ELsmp #1 SMP Fri May 11
18:50:54 EDT 2007 x86_64 x86_64 x86_64 GNU/Linux\n'
>>>

That demonstrates that I can successfully send the stdout output from my
subprocess.Popen object to a file handle.

I tried to send that output to a custom object that has a write() method...

>>>
>>> class Foo(object):
...     def write(self, msg):
...         sys.stdout.write('*** %s ***' % msg.rstrip() + os.linesep)
...         sys.stdout.flush()
...     def close(self):
...         pass
...
>>>
>>> foo = Foo()
>>>
>>> cmdObj = subprocess.Popen('uname -a', shell=True, stdout=foo)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/rel/lang/python/2.4.4-4/lib/python2.4/subprocess.py", line 534,
in __init__
    (p2cread, p2cwrite,
  File "/rel/lang/python/2.4.4-4/lib/python2.4/subprocess.py", line 840,
in _get_handles
    c2pwrite = stdout.fileno()
AttributeError: 'Foo' object has no attribute 'fileno'
>>>

so I gave it a fileno() method that returns None...

>>>
>>> class Foo(object):
...     def write(self, msg):
...         sys.stdout.write('*** %s ***' % msg.rstrip() + os.linesep)
...         sys.stdout.flush()
...     def close(self):
...         pass
...     def fileno(self):
...         return None
...
>>>
>>> foo = Foo()
>>>
>>> cmdObj = subprocess.Popen('uname -a', shell=True, stdout=foo)
>>> Linux northstar 2.6.9-42.0.2.IT111843.1.ELsmp #1 SMP Fri May 11
18:50:54 EDT 2007 x86_64 x86_64 x86_64 GNU/Linux
>>>

Notice that the output went straight to the console and bypassed my
'foo' object's write() method.
History
Date User Action Args
2009-03-09 14:31:06georg.brandlsetspambayes_score: 0.690444 -> 0.0
2007-12-15 01:01:33ngroversetspambayes_score: 0.690444 -> 0.690444
recipients: + ngrover
2007-12-15 01:01:33ngroversetspambayes_score: 0.690444 -> 0.690444
messageid: <1197680493.69.0.0120420737472.issue1631@psf.upfronthosting.co.za>
2007-12-15 01:01:33ngroverlinkissue1631 messages
2007-12-15 01:01:33ngrovercreate