From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from pigeon.gentoo.org ([208.92.234.80] helo=lists.gentoo.org) by finch.gentoo.org with esmtp (Exim 4.60) (envelope-from ) id 1RbN8k-00089o-2M for garchives@archives.gentoo.org; Fri, 16 Dec 2011 02:03:17 +0000 Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 81A9121C037; Fri, 16 Dec 2011 02:03:06 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) by pigeon.gentoo.org (Postfix) with ESMTP id 3F69521C037 for ; Fri, 16 Dec 2011 02:03:06 +0000 (UTC) Received: from pelican.gentoo.org (unknown [66.219.59.40]) (using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id A598C1B4085 for ; Fri, 16 Dec 2011 02:03:05 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by pelican.gentoo.org (Postfix) with ESMTP id CE9828004A for ; Fri, 16 Dec 2011 02:03:04 +0000 (UTC) From: "Zac Medico" To: gentoo-commits@lists.gentoo.org Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Zac Medico" Message-ID: <30d2d0a9db486c5a70848ad5d27b37a3ec48f271.zmedico@gentoo> Subject: [gentoo-commits] proj/portage:master commit in: pym/portage/tests/process/, pym/_emerge/ X-VCS-Repository: proj/portage X-VCS-Files: pym/_emerge/AbstractPollTask.py pym/_emerge/SpawnProcess.py pym/portage/tests/process/test_poll.py X-VCS-Directories: pym/portage/tests/process/ pym/_emerge/ X-VCS-Committer: zmedico X-VCS-Committer-Name: Zac Medico X-VCS-Revision: 30d2d0a9db486c5a70848ad5d27b37a3ec48f271 Date: Fri, 16 Dec 2011 02:03:04 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: quoted-printable X-Archives-Salt: b8089020-6b0b-4817-937b-b8ed791bfef9 X-Archives-Hash: 793edaefd83ee932b83b19ac2e7fa91c commit: 30d2d0a9db486c5a70848ad5d27b37a3ec48f271 Author: Zac Medico gentoo org> AuthorDate: Fri Dec 16 02:02:32 2011 +0000 Commit: Zac Medico gentoo org> CommitDate: Fri Dec 16 02:02:32 2011 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=3Dproj/portage.git;a= =3Dcommit;h=3D30d2d0a9 SpawnProcess: work around array.fromfile() bugs When I extended test_poll to test SpawnProcess array.fromfile() usage, it exposed bugs in array.fromfile() that I couldn't find a way to handle. So, use os.read() instead. --- pym/_emerge/AbstractPollTask.py | 23 +++++++++-- pym/_emerge/SpawnProcess.py | 31 ++++++++++----- pym/portage/tests/process/test_poll.py | 66 ++++++++++++++++++++++++++= +++-- 3 files changed, 100 insertions(+), 20 deletions(-) diff --git a/pym/_emerge/AbstractPollTask.py b/pym/_emerge/AbstractPollTa= sk.py index f7f3a95..ea13587 100644 --- a/pym/_emerge/AbstractPollTask.py +++ b/pym/_emerge/AbstractPollTask.py @@ -2,7 +2,9 @@ # Distributed under the terms of the GNU General Public License v2 =20 import array +import errno import logging +import os =20 from portage.util import writemsg_level from _emerge.AsynchronousTask import AsynchronousTask @@ -20,7 +22,7 @@ class AbstractPollTask(AsynchronousTask): def isAlive(self): return bool(self._registered) =20 - def _read_buf(self, f, event): + def _read_buf(self, fd, event): """ | POLLIN | RETURN | BIT | VALUE @@ -32,13 +34,26 @@ class AbstractPollTask(AsynchronousTask): | --------------------------------------------------- | 0 | None """ + # NOTE: array.fromfile() is no longer used here because it has + # bugs in all known versions of Python (including Python 2.7 + # and Python 3.2). buf =3D None if event & PollConstants.POLLIN: buf =3D array.array('B') try: - buf.fromfile(f, self._bufsize) - except (EOFError, IOError): - pass + # Python >=3D3.2 + frombytes =3D buf.frombytes + except AttributeError: + frombytes =3D buf.fromstring + try: + frombytes(os.read(fd, self._bufsize)) + except OSError as e: + # EIO happens with pty on Linux after the + # slave end of the pty has been closed. + if e.errno not in (errno.EAGAIN, errno.EIO): + raise + buf =3D None + return buf =20 def _unregister(self): diff --git a/pym/_emerge/SpawnProcess.py b/pym/_emerge/SpawnProcess.py index 84493fe..c2f4928 100644 --- a/pym/_emerge/SpawnProcess.py +++ b/pym/_emerge/SpawnProcess.py @@ -165,11 +165,20 @@ class SpawnProcess(SubProcess): def _output_handler(self, fd, event): =20 files =3D self._files - buf =3D self._read_buf(files.process, event) + while True: + buf =3D self._read_buf(fd, event) =20 - if buf is not None: + if buf is None: + # not a POLLIN event, EAGAIN, etc... + break =20 - if buf: + if not buf: + # EOF + self._unregister() + self.wait() + break + + else: if not self.background: write_successful =3D False failures =3D 0 @@ -217,9 +226,6 @@ class SpawnProcess(SubProcess): data =3D buf.tostring() files.log.write(data) files.log.flush() - else: - self._unregister() - self.wait() =20 self._unregister_if_appropriate(event) =20 @@ -230,15 +236,18 @@ class SpawnProcess(SubProcess): monitor the process from inside a poll() loop. """ =20 - buf =3D self._read_buf(self._files.process, event) + while True: + buf =3D self._read_buf(fd, event) =20 - if buf is not None: + if buf is None: + # not a POLLIN event, EAGAIN, etc... + break =20 - if buf: - pass - else: + if not buf: + # EOF self._unregister() self.wait() + break =20 self._unregister_if_appropriate(event) =20 diff --git a/pym/portage/tests/process/test_poll.py b/pym/portage/tests/p= rocess/test_poll.py index e7a4702..9b1f9cb 100644 --- a/pym/portage/tests/process/test_poll.py +++ b/pym/portage/tests/process/test_poll.py @@ -1,6 +1,8 @@ # Copyright 1998-2011 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 =20 +import tempfile + from portage import os from portage.tests import TestCase from portage.util._pty import _create_pty_or_pipe @@ -8,6 +10,13 @@ from _emerge.PollScheduler import PollScheduler from _emerge.PipeReader import PipeReader from _emerge.SpawnProcess import SpawnProcess =20 +class _SpawnProcessPty(SpawnProcess): + __slots__ =3D ("got_pty",) + def _pipe(self, fd_pipes): + got_pty, master_fd, slave_fd =3D _create_pty_or_pipe() + self.got_pty =3D got_pty + return (master_fd, slave_fd) + class PipeReaderTestCase(TestCase): =20 def _testPipeReader(self, test_string, use_pty): @@ -49,10 +58,57 @@ class PipeReaderTestCase(TestCase): output =3D consumer.getvalue().decode('ascii', 'replace') return (output, got_pty) =20 + def _testPipeReaderArray(self, test_string, use_pty): + """ + Use a poll loop to read data from a pipe and assert that + the data written to the pipe is identical to the data + read from the pipe. + """ + + scheduler =3D PollScheduler().sched_iface + if use_pty: + spawn_process =3D _SpawnProcessPty + else: + spawn_process =3D SpawnProcess + + fd, logfile =3D tempfile.mkstemp() + os.close(fd) + producer =3D spawn_process( + background=3DTrue, + args=3D["bash", "-c", "echo -n '%s'" % test_string], + env=3Dos.environ, + scheduler=3Dscheduler, logfile=3Dlogfile) + + try: + producer.start() + scheduler.schedule() + self.assertEqual(producer.returncode, os.EX_OK) + + if use_pty: + got_pty =3D producer.got_pty + else: + got_pty =3D False + + with open(logfile, 'rb') as f: + output =3D f.read().decode('ascii') + return (output, got_pty) + finally: + try: + os.unlink(logfile) + except OSError: + pass + def testPipeReader(self): for use_pty in (False, True): - for x in (1, 2, 5, 6, 7, 8, 2**5, 2**10, 2**12, 2**13, 2**14): - test_string =3D x * "a" - output, got_pty =3D self._testPipeReader(test_string, use_pty) - self.assertEqual(test_string, output, - "x =3D %s, use_pty =3D %s, got_pty =3D %s" % (x, use_pty, got_pty)) + for use_array in (False, True): + for x in (1, 2, 5, 6, 7, 8, 2**5, 2**10, 2**12, 2**13, 2**14): + test_string =3D x * "a" + if use_array: + method =3D self._testPipeReaderArray + else: + method =3D self._testPipeReader + output, got_pty =3D method(test_string, use_pty) + self.assertEqual(test_string, output, + "x =3D %s, len(output) =3D %s, use_array =3D %s, " + "use_pty =3D %s, got_pty =3D %s" % + (x, len(output), use_array, use_pty, got_pty))