Use sys.__std*.fileno() in case of overrides.
authorZac Medico <zmedico@gentoo.org>
Wed, 22 Aug 2012 19:46:04 +0000 (12:46 -0700)
committerZac Medico <zmedico@gentoo.org>
Wed, 22 Aug 2012 19:46:04 +0000 (12:46 -0700)
This fixes AttributeError exceptions for API consumers that override
sys.std* streams pseudo-file objects.

bin/dispatch-conf
pym/_emerge/BinpkgFetcher.py
pym/_emerge/EbuildMetadataPhase.py
pym/_emerge/SpawnProcess.py
pym/portage/getbinpkg.py
pym/portage/package/ebuild/doebuild.py
pym/portage/package/ebuild/fetch.py
pym/portage/process.py

index 139a001e8f7b129bad21aaf90f530848470f91fa..35979dbb2c387a9905cfe4ccece3317fa38a7b6b 100755 (executable)
@@ -1,5 +1,5 @@
 #!/usr/bin/python -O
-# Copyright 1999-2011 Gentoo Foundation
+# Copyright 1999-2012 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
 #
@@ -463,10 +463,12 @@ if not shell or not os.access(shell, os.EX_OK):
 
 def spawn_shell(cmd):
     if shell:
+        sys.__stdout__.flush()
+        sys.__stderr__.flush()
         spawn([shell, "-c", cmd], env=os.environ,
-            fd_pipes = {  0 : sys.stdin.fileno(),
-                          1 : sys.stdout.fileno(),
-                          2 : sys.stderr.fileno()})
+            fd_pipes = {  0 : sys.__stdin__.fileno(),
+                          1 : sys.__stdout__.fileno(),
+                          2 : sys.__stderr__.fileno()})
     else:
         os.system(cmd)
 
index f415e2ec7161db703f8e5bfe02780db0cac907ae..1913b44311ba145b11508f84037f1ba168ace4be 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright 1999-2011 Gentoo Foundation
+# Copyright 1999-2012 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
 from _emerge.AsynchronousLock import AsynchronousLock
@@ -91,9 +91,9 @@ class BinpkgFetcher(SpawnProcess):
                # Redirect all output to stdout since some fetchers like
                # wget pollute stderr (if portage detects a problem then it
                # can send it's own message to stderr).
-               fd_pipes.setdefault(0, sys.stdin.fileno())
-               fd_pipes.setdefault(1, sys.stdout.fileno())
-               fd_pipes.setdefault(2, sys.stdout.fileno())
+               fd_pipes.setdefault(0, sys.__stdin__.fileno())
+               fd_pipes.setdefault(1, sys.__stdout__.fileno())
+               fd_pipes.setdefault(2, sys.__stdout__.fileno())
 
                self.args = fetch_args
                self.env = fetch_env
@@ -104,7 +104,7 @@ class BinpkgFetcher(SpawnProcess):
        def _pipe(self, fd_pipes):
                """When appropriate, use a pty so that fetcher progress bars,
                like wget has, will work properly."""
-               if self.background or not sys.stdout.isatty():
+               if self.background or not sys.__stdout__.isatty():
                        # When the output only goes to a log file,
                        # there's no point in creating a pty.
                        return os.pipe()
index c2d3747f7f45c060e9eb9402a3178c5d64daffb1..d49c51f7906786d0e5bea9b83e22864c8a6aaa93 100644 (file)
@@ -74,15 +74,15 @@ class EbuildMetadataPhase(SubProcess):
 
                null_input = open('/dev/null', 'rb')
                fd_pipes.setdefault(0, null_input.fileno())
-               fd_pipes.setdefault(1, sys.stdout.fileno())
-               fd_pipes.setdefault(2, sys.stderr.fileno())
+               fd_pipes.setdefault(1, sys.__stdout__.fileno())
+               fd_pipes.setdefault(2, sys.__stderr__.fileno())
 
                # flush any pending output
                for fd in fd_pipes.values():
-                       if fd == sys.stdout.fileno():
-                               sys.stdout.flush()
-                       if fd == sys.stderr.fileno():
-                               sys.stderr.flush()
+                       if fd == sys.__stdout__.fileno():
+                               sys.__stdout__.flush()
+                       if fd == sys.__stderr__.fileno():
+                               sys.__stderr__.flush()
 
                self._files = self._files_dict()
                files = self._files
index 9fbc9647260807ba71914a23086297287c0d8843..dfcf088bcfbceb177076a0ad10ba253c6ffa1ec7 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright 1999-2011 Gentoo Foundation
+# Copyright 1999-2012 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
 from _emerge.SubProcess import SubProcess
@@ -62,16 +62,16 @@ class SpawnProcess(SubProcess):
                        null_input = os.open('/dev/null', os.O_RDWR)
                        fd_pipes[0] = null_input
 
-               fd_pipes.setdefault(0, sys.stdin.fileno())
-               fd_pipes.setdefault(1, sys.stdout.fileno())
-               fd_pipes.setdefault(2, sys.stderr.fileno())
+               fd_pipes.setdefault(0, sys.__stdin__.fileno())
+               fd_pipes.setdefault(1, sys.__stdout__.fileno())
+               fd_pipes.setdefault(2, sys.__stderr__.fileno())
 
                # flush any pending output
                for fd in fd_pipes.values():
-                       if fd == sys.stdout.fileno():
-                               sys.stdout.flush()
-                       if fd == sys.stderr.fileno():
-                               sys.stderr.flush()
+                       if fd == sys.__stdout__.fileno():
+                               sys.__stdout__.flush()
+                       if fd == sys.__stderr__.fileno():
+                               sys.__stderr__.flush()
 
                if logfile is not None:
 
index 212f78889ab2b9458ce81a21b16834e43c20f8cc..34a5e0efe7bcbdafc871a955569a6660b47d2f8e 100644 (file)
@@ -466,10 +466,12 @@ def file_get(baseurl,dest,conn=None,fcmd=None,filename=None):
        myfetch = portage.util.shlex_split(fcmd)
        myfetch = [varexpand(x, mydict=variables) for x in myfetch]
        fd_pipes= {
-               0:sys.stdin.fileno(),
-               1:sys.stdout.fileno(),
-               2:sys.stdout.fileno()
+               0:sys.__stdin__.fileno(),
+               1:sys.__stdout__.fileno(),
+               2:sys.__stdout__.fileno()
        }
+       sys.__stdout__.flush()
+       sys.__stderr__.flush()
        retval = spawn(myfetch, env=os.environ.copy(), fd_pipes=fd_pipes)
        if retval != os.EX_OK:
                sys.stderr.write(_("Fetcher exited with a failure condition.\n"))
index 395e0eeec08061f9d14c87c8760940e6e9c155dd..ef51da1b481e9c50b2ef0fd13f395026b3eeb6a3 100644 (file)
@@ -690,9 +690,9 @@ def doebuild(myebuild, mydo, _unused=None, settings=None, debug=0, listonly=0,
                                mysettings["dbkey"] = ""
                                pr, pw = os.pipe()
                                fd_pipes = {
-                                       0:sys.stdin.fileno(),
-                                       1:sys.stdout.fileno(),
-                                       2:sys.stderr.fileno(),
+                                       0:sys.__stdin__.fileno(),
+                                       1:sys.__stdout__.fileno(),
+                                       2:sys.__stderr__.fileno(),
                                        9:pw}
                                mypids = _spawn_phase(mydo, mysettings, returnpid=True,
                                        fd_pipes=fd_pipes)
@@ -1367,18 +1367,18 @@ def spawn(mystring, mysettings, debug=0, free=0, droppriv=0, sesandbox=0, fakero
        fd_pipes = keywords.get("fd_pipes")
        if fd_pipes is None:
                fd_pipes = {
-                       0:sys.stdin.fileno(),
-                       1:sys.stdout.fileno(),
-                       2:sys.stderr.fileno(),
+                       0:sys.__stdin__.fileno(),
+                       1:sys.__stdout__.fileno(),
+                       2:sys.__stderr__.fileno(),
                }
        # In some cases the above print statements don't flush stdout, so
        # it needs to be flushed before allowing a child process to use it
        # so that output always shows in the correct order.
-       stdout_filenos = (sys.stdout.fileno(), sys.stderr.fileno())
+       stdout_filenos = (sys.__stdout__.fileno(), sys.__stderr__.fileno())
        for fd in fd_pipes.values():
                if fd in stdout_filenos:
-                       sys.stdout.flush()
-                       sys.stderr.flush()
+                       sys.__stdout__.flush()
+                       sys.__stderr__.flush()
                        break
 
        features = mysettings.features
index 576a91239690fc89d119fcc9211f9d5d9a827571..260bf10bb4701f5fe138c8be8fe37e5c85c5e73b 100644 (file)
@@ -64,9 +64,9 @@ def _spawn_fetch(settings, args, **kwargs):
        if "fd_pipes" not in kwargs:
 
                kwargs["fd_pipes"] = {
-                       0 : sys.stdin.fileno(),
-                       1 : sys.stdout.fileno(),
-                       2 : sys.stdout.fileno(),
+                       0 : sys.__stdin__.fileno(),
+                       1 : sys.__stdout__.fileno(),
+                       2 : sys.__stdout__.fileno(),
                }
 
        if "userfetch" in settings.features and \
index f3cec88153cbae3d4a82bd88b8b1d8905ecfd14e..32e60ac500ff19f1d529be200c40e28b7ab91f12 100644 (file)
@@ -226,9 +226,9 @@ def spawn(mycommand, env={}, opt_name=None, fd_pipes=None, returnpid=False,
        # default to propagating our stdin, stdout and stderr.
        if fd_pipes is None:
                fd_pipes = {
-                       0:sys.stdin.fileno(),
-                       1:sys.stdout.fileno(),
-                       2:sys.stderr.fileno(),
+                       0:sys.__stdin__.fileno(),
+                       1:sys.__stdout__.fileno(),
+                       2:sys.__stderr__.fileno(),
                }
 
        # mypids will hold the pids of all processes created.