Split out a AbstractPollTask._read_buf() helper method.
authorZac Medico <zmedico@gentoo.org>
Fri, 10 Sep 2010 03:05:19 +0000 (20:05 -0700)
committerZac Medico <zmedico@gentoo.org>
Fri, 10 Sep 2010 03:05:19 +0000 (20:05 -0700)
pym/_emerge/AbstractPollTask.py
pym/_emerge/SpawnProcess.py

index 1feee15f63083110e6d084ecf75e0171b12c317e..833ee3b4c95db0aa0eeafa9dcaeb03d788004379 100644 (file)
@@ -1,6 +1,8 @@
-# Copyright 1999-2009 Gentoo Foundation
+# Copyright 1999-2010 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
+import array
+
 from _emerge.AsynchronousTask import AsynchronousTask
 from _emerge.PollConstants import PollConstants
 class AbstractPollTask(AsynchronousTask):
@@ -13,6 +15,27 @@ class AbstractPollTask(AsynchronousTask):
        _registered_events = PollConstants.POLLIN | PollConstants.POLLHUP | \
                _exceptional_events
 
+       def _read_buf(self, f, event):
+               """
+               | 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.
+               | ---------------------------------------------------
+               | 0      | None
+               """
+               buf = None
+               if event & PollConstants.POLLIN:
+                       buf = array.array('B')
+                       try:
+                               buf.fromfile(f, self._bufsize)
+                       except (EOFError, IOError):
+                               pass
+               return buf
+
        def _unregister(self):
                raise NotImplementedError(self)
 
index aeb206a06b67b26f8c2dc234bd5c7e54036e7740..0cddbe801cd29e6dfa3ff7ce808f02d011abfec8 100644 (file)
@@ -1,8 +1,7 @@
-# Copyright 1999-2009 Gentoo Foundation
+# Copyright 1999-2010 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
 from _emerge.SubProcess import SubProcess
-from _emerge.PollConstants import PollConstants
 import sys
 from portage.cache.mappings import slot_dict_class
 import portage
@@ -11,7 +10,6 @@ from portage import _unicode_encode
 from portage import os
 import fcntl
 import errno
-import array
 import gzip
 
 class SpawnProcess(SubProcess):
@@ -150,14 +148,10 @@ class SpawnProcess(SubProcess):
 
        def _output_handler(self, fd, event):
 
-               if event & PollConstants.POLLIN:
+               files = self._files
+               buf = self._read_buf(files.process, event)
 
-                       files = self._files
-                       buf = array.array('B')
-                       try:
-                               buf.fromfile(files.process, self._bufsize)
-                       except (EOFError, IOError):
-                               pass
+               if buf is not None:
 
                        if buf:
                                if not self.background:
@@ -215,13 +209,9 @@ class SpawnProcess(SubProcess):
                monitor the process from inside a poll() loop.
                """
 
-               if event & PollConstants.POLLIN:
+               buf = self._read_buf(self._files.process, event)
 
-                       buf = array.array('B')
-                       try:
-                               buf.fromfile(self._files.process, self._bufsize)
-                       except (EOFError, IOError):
-                               pass
+               if buf is not None:
 
                        if buf:
                                pass