PipeLogger: handle file object for input_fd
authorZac Medico <zmedico@gentoo.org>
Mon, 7 Jan 2013 09:16:11 +0000 (01:16 -0800)
committerZac Medico <zmedico@gentoo.org>
Mon, 7 Jan 2013 09:16:11 +0000 (01:16 -0800)
pym/portage/tests/process/test_PopenProcess.py
pym/portage/util/_async/PipeLogger.py

index 5ede164af920a58dc5f460f5b752a9c066c57886..88da0b3540356b80bd6bc615e6c2eafe6314698e 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright 2012 Gentoo Foundation
+# Copyright 2012-2013 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
 import subprocess
@@ -53,12 +53,9 @@ class PopenPipeTestCase(TestCase):
                try:
 
                        consumer = PipeLogger(background=True,
-                               input_fd=os.dup(producer.proc.stdout.fileno()),
+                               input_fd=producer.proc.stdout,
                                log_file_path=log_file_path)
 
-                       # Close the stdout pipe, since we duplicated it, and it
-                       # must be closed in order to avoid a ResourceWarning.
-                       producer.proc.stdout.close()
                        producer.pipe_reader = consumer
 
                        producer.start()
index 5464879ff58c2968d30cffc3ed9c3a7f8ff93ec8..d0b13237847ecc14506fb10533d4519ec49e068a 100644 (file)
@@ -46,10 +46,15 @@ class PipeLogger(AbstractPollTask):
                else:
                        fcntl_flags |= fcntl.FD_CLOEXEC
 
-               fcntl.fcntl(self.input_fd, fcntl.F_SETFL,
-                       fcntl.fcntl(self.input_fd, fcntl.F_GETFL) | fcntl_flags)
+               if isinstance(self.input_fd, int):
+                       fd = self.input_fd
+               else:
+                       fd = self.input_fd.fileno()
+
+               fcntl.fcntl(fd, fcntl.F_SETFL,
+                       fcntl.fcntl(fd, fcntl.F_GETFL) | fcntl_flags)
 
-               self._reg_id = self.scheduler.io_add_watch(self.input_fd,
+               self._reg_id = self.scheduler.io_add_watch(fd,
                        self._registered_events, self._output_handler)
                self._registered = True
 
@@ -133,7 +138,10 @@ class PipeLogger(AbstractPollTask):
                        self._reg_id = None
 
                if self.input_fd is not None:
-                       os.close(self.input_fd)
+                       if isinstance(self.input_fd, int):
+                               os.close(self.input_fd)
+                       else:
+                               self.input_fd.close()
                        self.input_fd = None
 
                if self.stdout_fd is not None: