Ticket #4522: 0002-Changes-from-James-review.patch
File 0002-Changes-from-James-review.patch, 4.4 KB (added by , 12 years ago) |
---|
-
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): 463 463 return "<%s pid=%s status=%s>" % (self.__class__.__name__, 464 464 self.pid, self.status) 465 465 466 def 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 466 493 class Process(_BaseProcess): 467 494 """ 468 495 An operating-system Process. … … class Process(_BaseProcess): 601 628 This is accomplished in two steps:: 602 629 603 630 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'). 605 633 606 634 2. for each childFD:: 607 635 … … class Process(_BaseProcess): 625 653 errfd.write("starting _setupChild\n") 626 654 627 655 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(): 638 657 if fd in destList: 639 658 continue 640 659 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): 476 476 477 477 reactor.callWhenRunning(spawnChild) 478 478 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) 479 515 globals().update(ProcessTestsBuilder.makeTestCaseClasses()) 480 516 481 517 -
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): 1533 1533 """ 1534 1534 return 0, 0, 1, 2 1535 1535 1536 def listdir(self, path): 1537 """ 1538 Override C{os.listdir}, returning fake contents of '/dev/fd' 1539 """ 1540 return "-1", "-2" 1541 1536 1542 1537 1543 1538 1544 if process is not None: