Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #48856
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <jonathan-h@moving-picture.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'output': 0.05; 'received:169.254.1': 0.05; '"__main__":': 0.09; '[],': 0.09; '__name__': 0.09; 'executes': 0.09; 'newline': 0.09; 'pointers': 0.09; 'received:10.14': 0.09; 'seemed': 0.09; 'try:': 0.09; 'python': 0.11; 'def': 0.12; 'wrote': 0.14; '%r"': 0.16; '0.1)': 0.16; '2.6.4': 0.16; 'attaching': 0.16; 'fcntl': 0.16; 'googling': 0.16; 'pty': 0.16; 'simplest': 0.16; 'stdout': 0.16; 'true:': 0.16; 'fix': 0.17; 'thanks,': 0.17; 'select': 0.22; 'import': 0.22; 'to:name:python-list@python.org': 0.22; 'print': 0.22; 'environment': 0.24; "i've": 0.25; 'this:': 0.26; 'testing': 0.29; 'correct': 0.29; 'external': 0.29; 'mode': 0.30; 'specified': 0.30; '(which': 0.31; 'getting': 0.31; 'bunch': 0.31; 'controlled': 0.31; 'enabled': 0.31; 'anyone': 0.31; 'class': 0.32; 'received:169.254': 0.32; 'cases': 0.33; 'skip:# 10': 0.33; 'noticed': 0.34; 'skip:_ 10': 0.34; "i'd": 0.34; 'could': 0.34; 'subject:with': 0.35; 'except': 0.35; 'skip:u 20': 0.35; 'test': 0.35; 'done': 0.36; 'charset:us-ascii': 0.36; 'hi,': 0.36; 'received:169': 0.37; 'subject:New': 0.37; 'wrong': 0.37; 'received:10': 0.37; 'skip:o 20': 0.38; 'needed': 0.38; 'to:addr :python-list': 0.38; 'fact': 0.38; 'does': 0.39; 'environment.': 0.39; 'to:addr:python.org': 0.39; 'even': 0.60; 'skip:u 10': 0.60; 'break': 0.61; 'conversion': 0.61; 'new': 0.61; 'simple': 0.61; 'kept': 0.65; 'hours': 0.66; 'message).': 0.84; 'skip:| 10': 0.84 |
| From | Jonathan Harden <jonathan-h@moving-picture.com> |
| To | "python-list@python.org" <python-list@python.org> |
| Subject | New line conversion with Popen attached to a pty |
| Thread-Topic | New line conversion with Popen attached to a pty |
| Thread-Index | AQHObZyym1MomopBoEuTo4mCJc1l7w== |
| Date | Thu, 20 Jun 2013 10:20:20 +0000 |
| Accept-Language | en-GB, en-US |
| Content-Language | en-GB |
| X-MS-Has-Attach | |
| X-MS-TNEF-Correlator | |
| x-originating-ip | [172.16.22.176] |
| Content-Type | text/plain; charset="us-ascii" |
| Content-Transfer-Encoding | quoted-printable |
| MIME-Version | 1.0 |
| X-Spam-Score | -2.9 (--) |
| X-Spam-Report | Spam detection software, running on the system "caroline.mpc.local", has identified this incoming email as possible spam. The original message has been attached to this so you can view it (if it isn't spam) or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: Hi, We have a class which executes external processes in a controlled environment and does "things" specified by the client program with each line of output. To do this we have been attaching stdout from the subprocess.Popen to a pseudo terminal (pty) made with pty.openempty and opened with os.fdopen. I noticed that we kept getting a bunch of extra new line characters. [...] Content analysis details: (-2.9 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -1.0 ALL_TRUSTED Passed through trusted hosts only via SMTP -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] |
| X-Mailman-Approved-At | Fri, 21 Jun 2013 13:00:12 +0200 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3660.1371812413.3114.python-list@python.org> (permalink) |
| Lines | 89 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1371812413 news.xs4all.nl 15865 [2001:888:2000:d::a6]:45892 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:48856 |
Show key headers only | View raw
Hi,
We have a class which executes external processes in a controlled environment and does "things" specified by the client program with each line of output. To do this we have been attaching stdout from the subprocess.Popen to a pseudo terminal (pty) made with pty.openempty and opened with os.fdopen. I noticed that we kept getting a bunch of extra new line characters.
This is all using python 2.6.4 in a centos6 environment.
After some investigation I realised we needed to use universal_newline support so I enabled it for the Popen and specified the mode in the fdopen to be rU. Things still seemed to be coming out wrong so I wrote up a test program boiling it down to the simplest cases (which is at the end of this message). The output I was testing was this:
Fake\r\nData\r\n
as seen through hexdump -C:
> hexdump -C output.txt
00000000 46 61 6b 65 0d 0a 44 61 74 61 0d 0a |Fake..Data..|
0000000c
Now if I do a simple subprocess.Popen and set the stdout to subprocess.PIPE, then do p.stdout.read() I get the correct output of
Fake\nData\n
When do the Popen attached to a pty I end up with
Fake\n\nData\n\n
Does anyone know why the newline conversion would be incorrect, and what I could do to fix it? In fact if anyone even has any pointers to where this might be going wrong I'd be very helpful, I've done a lot of hours of fiddling with this and googling to no avail.
Thanks,
Jonathan
#!/usr/bin/env python2.6.4
import os
import pty
import subprocess
import select
import fcntl
class TestRead(object):
def __init__(self):
super(TestRead, self).__init__()
self.outputPipe()
self.outputPty()
def outputPipe(self):
p1 = subprocess.Popen(
("/bin/cat", "output.txt"),
stdout=subprocess.PIPE,
universal_newlines=True
)
print "1: %r" % p1.stdout.read()
def outputPty(self):
outMaster, outSlave = pty.openpty()
fcntl.fcntl(outMaster, fcntl.F_SETFL, os.O_NONBLOCK)
p2 = subprocess.Popen(
("/bin/cat", "output.txt"),
stdout=outSlave,
universal_newlines=True
)
with os.fdopen(outMaster, 'rU') as pty_stdout:
while True:
try:
rfds, _, _ = select.select([pty_stdout], [], [], 0.1)
break
except select.error:
continue
for fd in rfds:
buf = pty_stdout.read()
print "2: %r" % buf
if __name__ == "__main__":
t = TestRead()
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
New line conversion with Popen attached to a pty Jonathan Harden <jonathan-h@moving-picture.com> - 2013-06-20 10:20 +0000 Re: New line conversion with Popen attached to a pty jfharden@gmail.com - 2013-06-21 04:14 -0700
csiph-web