get_open_fds: use /dev/fd or /proc/self/fd
authorZac Medico <zmedico@gentoo.org>
Sun, 28 Apr 2013 22:39:24 +0000 (15:39 -0700)
committerZac Medico <zmedico@gentoo.org>
Sun, 28 Apr 2013 22:39:24 +0000 (15:39 -0700)
Since /dev/fd is supposed to be a fairly standard unix feature, try
that first.

pym/portage/process.py

index f00775cf5363654c02e0cb4dda1e04553e9caae7..24e347c2600ab75f97bd82bb0957537619225f44 100644 (file)
@@ -30,10 +30,15 @@ except ImportError:
 if sys.hexversion >= 0x3000000:
        basestring = str
 
-if os.path.isdir("/proc/%i/fd" % os.getpid()):
+for _fd_dir in ("/dev/fd", "/proc/self/fd"):
+       if os.path.isdir(_fd_dir):
+               break
+       else:
+               _fd_dir = None
+
+if _fd_dir is not None:
        def get_open_fds():
-               return (int(fd) for fd in os.listdir("/proc/%i/fd" % os.getpid()) \
-                       if fd.isdigit())
+               return (int(fd) for fd in os.listdir(_fd_dir) if fd.isdigit())
 
        if platform.python_implementation() == 'PyPy':
                # EAGAIN observed with PyPy 1.8.