PtyEofTestCase: test unbuffered fdopen
authorZac Medico <zmedico@gentoo.org>
Fri, 21 Jan 2011 01:59:32 +0000 (17:59 -0800)
committerZac Medico <zmedico@gentoo.org>
Fri, 21 Jan 2011 01:59:32 +0000 (17:59 -0800)
New development: It appears that array.fromfile() is usable
with python3 as long as fdopen is called with a bufsize
argument of 0.

pym/portage/tests/ebuild/test_pty_eof.py
pym/portage/util/_pty.py

index 042227b672648c05099460c66ddad7180a118d19..c4386e944e7dfcb00c4a58d1adf640b27feac385 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright 2009-2010 Gentoo Foundation
+# Copyright 2009-2011 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
 from portage.tests import TestCase
@@ -6,7 +6,7 @@ from portage.util._pty import _can_test_pty_eof, _test_pty_eof
 
 class PtyEofTestCase(TestCase):
 
-       def testPtyEof(self):
+       def testPtyEofFdopenBuffered(self):
                # This tests if the following python issue is fixed yet:
                #   http://bugs.python.org/issue5380
                # Since it might not be fixed, mark as todo.
@@ -17,3 +17,15 @@ class PtyEofTestCase(TestCase):
                                self.assertEqual(_test_pty_eof(), True)
                        except EnvironmentError:
                                pass
+
+       def testPtyEofFdopenUnBuffered(self):
+               # New development: It appears that array.fromfile() is usable
+               # with python3 as long as fdopen is called with a bufsize
+               # argument of 0.
+
+               # The result is only valid if openpty does not raise EnvironmentError.
+               if _can_test_pty_eof():
+                       try:
+                               self.assertEqual(_test_pty_eof(fdopen_buffered=False), True)
+                       except EnvironmentError:
+                               pass
index 877430e96e9835566602fc7c69d59f76d3bd4fd6..7e769d204db2368184375f8a3f0afb41a6ad037a 100644 (file)
@@ -28,11 +28,19 @@ def _can_test_pty_eof():
        """
        return platform.system() in ("Linux",)
 
-def _test_pty_eof():
+def _test_pty_eof(fdopen_buffered=True):
        """
        Returns True if this issues is fixed for the currently
        running version of python: http://bugs.python.org/issue5380
        Raises an EnvironmentError from openpty() if it fails.
+
+       NOTE: This issue is only problematic when array.fromfile()
+       is used, rather than os.read(). However, array.fromfile()
+       is preferred since it is approximatly 10% faster.
+
+       New development: It appears that array.fromfile() is usable
+       with python3 as long as fdopen is called with a bufsize
+       argument of 0.
        """
 
        use_fork = False
@@ -78,7 +86,10 @@ def _test_pty_eof():
        if pid is not None:
                os.waitpid(pid, 0)
 
-       master_file = os.fdopen(master_fd, 'rb')
+       if fdopen_buffered:
+               master_file = os.fdopen(master_fd, 'rb')
+       else:
+               master_file = os.fdopen(master_fd, 'rb', 0)
        eof = False
        data = []
        iwtd = [master_file]