Ticket #4522: 0002-Changes-from-James-review.patch

File 0002-Changes-from-James-review.patch, 4.4 KB (added by Carlos Valiente, 12 years ago)

Addressing James' review

  • twisted/internet/process.py

    diff --git a/twisted/internet/process.py b/twisted/internet/process.py
    index c5f1ac3..4a34ad0 100644
    a b class _BaseProcess(BaseProcess, object): 
    463463        return "<%s pid=%s status=%s>" % (self.__class__.__name__,
    464464                                          self.pid, self.status)
    465465
     466def listOpenFDs():
     467    """
     468    Return an iterable of potentially open file descriptors.
     469
     470    This function returns an iterable over the contents of /dev/fd or
     471    /proc/<pid>/fd, if they're available on the platform. If they're not, the
     472    returned value is the range [0, maxfds], where 'maxfds' is at least 256.
     473    """
     474    dname = "/dev/fd"
     475    try:
     476        return tuple(int(fd) for fd in os.listdir(dname))
     477    except:
     478        dname = "/proc/%d/fd" % os.getpid()
     479        try:
     480            return tuple(int(fd) for fd in os.listdir(dname))
     481        except:
     482            try:
     483                import resource
     484                maxfds = resource.getrlimit(resource.RLIMIT_NOFILE)[1] + 1
     485                # OS-X reports 9223372036854775808. That's a lot of fds
     486                # to close
     487                if maxfds > 1024:
     488                    maxfds = 1024
     489            except:
     490                maxfds = 256
     491            return xrange(maxfds)
     492
    466493class Process(_BaseProcess):
    467494    """
    468495    An operating-system Process.
    class Process(_BaseProcess): 
    601628        This is accomplished in two steps::
    602629
    603630            1. close all file descriptors that aren't values of fdmap.  This
    604                means 0 .. maxfds.
     631               means 0 .. maxfds (or just the open fds within that range, if
     632               the platform supports '/proc/<pid>/fd').
    605633
    606634            2. for each childFD::
    607635
    class Process(_BaseProcess): 
    625653            errfd.write("starting _setupChild\n")
    626654
    627655        destList = fdmap.values()
    628         try:
    629             import resource
    630             maxfds = resource.getrlimit(resource.RLIMIT_NOFILE)[1] + 1
    631             # OS-X reports 9223372036854775808. That's a lot of fds to close
    632             if maxfds > 1024:
    633                 maxfds = 1024
    634         except:
    635             maxfds = 256
    636 
    637         for fd in xrange(maxfds):
     656        for fd in listOpenFDs():
    638657            if fd in destList:
    639658                continue
    640659            if debug and fd == errfd.fileno():
  • twisted/internet/test/test_process.py

    diff --git a/twisted/internet/test/test_process.py b/twisted/internet/test/test_process.py
    index 06197c0..ac2fa67 100644
    a b class ProcessTestsBuilder(ProcessTestsBuilderBase): 
    476476
    477477        reactor.callWhenRunning(spawnChild)
    478478        self.runReactor(reactor)
     479
     480
     481    def test_listOpenFDs(self):
     482        """
     483        A spawned process has only stdin, stdout and stderr open
     484        (file descriptor 3 is also reported as open, because of the call to
     485        'os.listdir()').
     486        """
     487        here = os.path.dirname(__file__)
     488        top = os.path.normpath(os.path.join(here, "..", "..", ".."))
     489        source = (
     490            "import sys",
     491            "sys.path.insert(0, '%s')" % top,
     492            "from twisted.internet import process",
     493            "sys.stdout.write(str(process.listOpenFDs()))",
     494            "sys.stdout.flush()")
     495
     496        reactor = self.buildReactor()
     497
     498        def processFinished(output):
     499            self.assertEqual('(0, 1, 2, 3)', output)
     500
     501        def shutdown(result):
     502            reactor.stop()
     503            return result
     504
     505        def spawnChild():
     506            print top
     507            d = succeed(None)
     508            d.addCallback(lambda dummy: utils.getProcessOutput(
     509                sys.executable, ["-c", "\n".join(source)], reactor=reactor))
     510            d.addCallback(processFinished)
     511            d.addBoth(shutdown)
     512
     513        reactor.callWhenRunning(spawnChild)
     514        self.runReactor(reactor)
    479515globals().update(ProcessTestsBuilder.makeTestCaseClasses())
    480516
    481517
  • twisted/test/test_process.py

    diff --git a/twisted/test/test_process.py b/twisted/test/test_process.py
    index 48918be..00cec9d 100644
    a b class MockOS(object): 
    15331533        """
    15341534        return 0, 0, 1, 2
    15351535
     1536    def listdir(self, path):
     1537        """
     1538        Override C{os.listdir}, returning fake contents of '/dev/fd'
     1539        """
     1540        return "-1", "-2"
     1541
    15361542
    15371543
    15381544if process is not None: