Ticket #4522: 0008-Also-use-_listOpenFDs-in-PTYProcess.patch

File 0008-Also-use-_listOpenFDs-in-PTYProcess.patch, 4.4 KB (added by Carlos Valiente, 12 years ago)

Generated from Git mirror, branch svn/faster-close-fds-4522

  • twisted/internet/process.py

    From 744a1bf3c8ce8b0dc1d75ff7e2a6de7aa9781736 Mon Sep 17 00:00:00 2001
    From: Carlos Valiente <carlos.valiente@ecmwf.int>
    Date: Fri, 2 Jul 2010 14:14:34 +0100
    Subject: [PATCH] Also use _listOpenFDs in PTYProcess
    
    ---
     twisted/internet/process.py           |   11 +++--
     twisted/internet/test/test_process.py |   79 ++++++++++++++++-----------------
     2 files changed, 45 insertions(+), 45 deletions(-)
    
    diff --git a/twisted/internet/process.py b/twisted/internet/process.py
    index a846f95..f8d89d3 100644
    a b class PTYProcess(abstract.FileDescriptor, _BaseProcess): 
    908908        os.dup2(slavefd, 1) # stdout
    909909        os.dup2(slavefd, 2) # stderr
    910910
    911         for fd in xrange(3, 256):
    912             try:
    913                 os.close(fd)
    914             except:
    915                 pass
     911        for fd in _listOpenFDs():
     912            if fd > 2:
     913                try:
     914                    os.close(fd)
     915                except:
     916                    pass
    916917
    917918        self._resetSignalDisposition()
    918919
  • twisted/internet/test/test_process.py

    diff --git a/twisted/internet/test/test_process.py b/twisted/internet/test/test_process.py
    index 1dab301..adbb04c 100644
    a b class ProcessTestsBuilderBase(ReactorBuilder): 
    219219        self.assertEqual(result, ["Foo\n"])
    220220
    221221
     222    def test_openFileDescriptors(self):
     223        """
     224        A spawned process has only stdin, stdout and stderr open
     225        (file descriptor 3 is also reported as open, because of the call to
     226        'os.listdir()').
     227        """
     228        from twisted.python.runtime import platformType
     229        if platformType == "posix":
     230            raise SkipTest("Test only applies to POSIX platforms")
     231
     232        here = FilePath(__file__)
     233        top = here.parent().parent().parent().parent()
     234        source = (
     235            "import sys",
     236            "sys.path.insert(0, '%s')" % (top.path,),
     237            "from twisted.internet import process",
     238            "sys.stdout.write(str(process._listOpenFDs()))",
     239            "sys.stdout.flush()")
     240
     241        reactor = self.buildReactor()
     242
     243        def processFinished(output):
     244            self.assertEquals('[0, 1, 2, 3]', output)
     245
     246        def shutdown(result):
     247            reactor.stop()
     248            return result
     249
     250        def spawnChild():
     251            msg("Spawning child with Twisted at %r" % (top.path,))
     252            d = succeed(None)
     253            d.addCallback(lambda dummy: utils.getProcessOutput(
     254                sys.executable, ["-c", "\n".join(source)], reactor=reactor))
     255            d.addCallback(processFinished)
     256            d.addBoth(shutdown)
     257
     258        reactor.callWhenRunning(spawnChild)
     259        self.runReactor(reactor)
     260
    222261
    223262class ProcessTestsBuilder(ProcessTestsBuilderBase):
    224263    """
    class ProcessTestsBuilder(ProcessTestsBuilderBase): 
    476515
    477516        reactor.callWhenRunning(spawnChild)
    478517        self.runReactor(reactor)
    479 
    480 
    481     def test_openFileDescriptors(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         from twisted.python.runtime import platformType
    488         if platformType == "posix":
    489             raise SkipTest("Test only applies to POSIX platforms")
    490 
    491         here = FilePath(__file__)
    492         top = here.parent().parent().parent().parent()
    493         source = (
    494             "import sys",
    495             "sys.path.insert(0, '%s')" % (top.path,),
    496             "from twisted.internet import process",
    497             "sys.stdout.write(str(process._listOpenFDs()))",
    498             "sys.stdout.flush()")
    499 
    500         reactor = self.buildReactor()
    501 
    502         def processFinished(output):
    503             self.assertEquals('[0, 1, 2, 3]', output)
    504 
    505         def shutdown(result):
    506             reactor.stop()
    507             return result
    508 
    509         def spawnChild():
    510             msg("Spawning child with Twisted at %r" % (top.path,))
    511             d = succeed(None)
    512             d.addCallback(lambda dummy: utils.getProcessOutput(
    513                 sys.executable, ["-c", "\n".join(source)], reactor=reactor))
    514             d.addCallback(processFinished)
    515             d.addBoth(shutdown)
    516 
    517         reactor.callWhenRunning(spawnChild)
    518         self.runReactor(reactor)
    519518globals().update(ProcessTestsBuilder.makeTestCaseClasses())
    520519
    521520