Issue1631
Created on 2007-12-15 01:01 by ngrover, last changed 2007-12-18 19:23 by gvanrossum. This issue is now closed.
| Messages (2) | |||
|---|---|---|---|
| msg58646 - (view) | Author: Nishkar Grover (ngrover) | Date: 2007-12-15 01:01 | |
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.
|
|||
| msg58648 - (view) | Author: Christian Heimes (christian.heimes) * ![]() |
Date: 2007-12-15 01:35 | |
It's not going to be easy and there is also no point to implement the feature. The subprocess module requires either a real file or a PIPE. A real file is needed because the subprocess module uses some low level operation system functions for speed efficiency. It writes data directly to a file descriptor (the number returned by fileno()). In your case it seems have to interpreted None as 0 which is - by accident - the file descriptor of stdout. For small amounts of data use Popen(..., stdout=subprocess.PIPE) and use out, err = communicate([stdin]), for large amounts of data you should use a temporary file (tempfile module). In your case please use the platform module instead of uname :) |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2007-12-18 19:23:58 | gvanrossum | set | status: pending -> closed |
| 2007-12-15 01:35:41 | christian.heimes | set | status: open -> pending resolution: wont fix messages: + msg58648 nosy: + christian.heimes |
| 2007-12-15 01:01:33 | ngrover | create | |
