SpawnProcess/AbstractPollTask: eliminate array
authorZac Medico <zmedico@gentoo.org>
Fri, 16 Dec 2011 19:32:39 +0000 (11:32 -0800)
committerZac Medico <zmedico@gentoo.org>
Fri, 16 Dec 2011 19:32:39 +0000 (11:32 -0800)
Since commit 30d2d0a9db486c5a70848ad5d27b37a3ec48f271, we use os.read()
due to bugs in array.fromfile(). So, eliminate array usage entirely.

pym/_emerge/AbstractPollTask.py
pym/_emerge/SpawnProcess.py

index d4785a2a1d17b5e014e9004da124278423bfe9ad..b3c0b2d16e7c74ad7b0269410063ad75091543cc 100644 (file)
@@ -1,7 +1,6 @@
 # Copyright 1999-2011 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
-import array
 import errno
 import logging
 import os
@@ -27,10 +26,9 @@ class AbstractPollTask(AsynchronousTask):
                | POLLIN | RETURN
                | BIT    | VALUE
                | ---------------------------------------------------
-               | 1      | Read self._bufsize into an instance of
-               |        | array.array('B') and return it, ignoring
-               |        | EOFError and IOError. An empty array
-               |        | indicates EOF.
+               | 1      | Read self._bufsize into a string of bytes,
+               |        | handling EAGAIN and EIO. An empty string
+               |        | of bytes indicates EOF.
                | ---------------------------------------------------
                | 0      | None
                """
@@ -39,20 +37,14 @@ class AbstractPollTask(AsynchronousTask):
                # and Python 3.2).
                buf = None
                if event & PollConstants.POLLIN:
-                       buf = array.array('B')
                        try:
-                               # Python >=3.2
-                               frombytes = buf.frombytes
-                       except AttributeError:
-                               frombytes = buf.fromstring
-                       try:
-                               frombytes(os.read(fd, self._bufsize))
+                               buf = 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 == errno.EIO:
-                                       # EOF: return empty buffer
-                                       pass
+                                       # EOF: return empty string of bytes
+                                       buf = b''
                                elif e.errno == errno.EAGAIN:
                                        # EAGAIN: return None
                                        buf = None
index 9f83ef0dee78cbeaf815ced5b79603d015149ba2..ec5bf7d2f952bad23c0c84f2ef1df76359b7e296 100644 (file)
@@ -183,7 +183,7 @@ class SpawnProcess(SubProcess):
                                        while True:
                                                try:
                                                        if not write_successful:
-                                                               buf.tofile(files.stdout)
+                                                               files.stdout.write(buf)
                                                                write_successful = True
                                                        files.stdout.flush()
                                                        break
@@ -213,16 +213,7 @@ class SpawnProcess(SubProcess):
                                                                fcntl.fcntl(files.stdout.fileno(),
                                                                fcntl.F_GETFL) ^ os.O_NONBLOCK)
 
-                               try:
-                                       buf.tofile(files.log)
-                               except TypeError:
-                                       # array.tofile() doesn't work with GzipFile
-                                       try:
-                                               # Python >=3.2
-                                               data = buf.tobytes()
-                                       except AttributeError:
-                                               data = buf.tostring()
-                                       files.log.write(data)
+                               files.log.write(buf)
                                files.log.flush()
 
                self._unregister_if_appropriate(event)