[devscripts] 01/02: Use a context handler to ensure null fd gets closed

James McCoy jamessan at debian.org
Sun May 11 18:27:09 UTC 2014


This is an automated email from the git hooks/post-receive script.

jamessan pushed a commit to branch master
in repository devscripts.

commit 4110c2702423fa482e8a0a69e1a1aa72e4704094
Author: James McCoy <jamessan at debian.org>
Date:   Sun May 11 14:00:45 2014 -0400

    Use a context handler to ensure null fd gets closed
    
    Signed-off-by: James McCoy <jamessan at debian.org>
---
 scripts/devscripts/test/test_help.py | 41 ++++++++++++++++++------------------
 1 file changed, 20 insertions(+), 21 deletions(-)

diff --git a/scripts/devscripts/test/test_help.py b/scripts/devscripts/test/test_help.py
index da324fe..3673b14 100644
--- a/scripts/devscripts/test/test_help.py
+++ b/scripts/devscripts/test/test_help.py
@@ -42,31 +42,30 @@ class HelpTestCase(unittest.TestCase):
     @classmethod
     def make_help_tester(cls, script):
         def tester(self):
-            null = open('/dev/null', 'r')
-            process = subprocess.Popen(['./' + script, '--help'],
-                                       close_fds=True, stdin=null,
-                                       stdout=subprocess.PIPE,
-                                       stderr=subprocess.PIPE)
-            started = time.time()
-            out = []
+            with open('/dev/null', 'r') as null:
+                process = subprocess.Popen(['./' + script, '--help'],
+                                           close_fds=True, stdin=null,
+                                           stdout=subprocess.PIPE,
+                                           stderr=subprocess.PIPE)
+                started = time.time()
+                out = []
 
-            fds = [process.stdout.fileno(), process.stderr.fileno()]
-            for fd in fds:
-                fcntl.fcntl(fd, fcntl.F_SETFL,
-                            fcntl.fcntl(fd, fcntl.F_GETFL) | os.O_NONBLOCK)
+                fds = [process.stdout.fileno(), process.stderr.fileno()]
+                for fd in fds:
+                    fcntl.fcntl(fd, fcntl.F_SETFL,
+                                fcntl.fcntl(fd, fcntl.F_GETFL) | os.O_NONBLOCK)
 
-            while time.time() - started < TIMEOUT:
-                for fd in select.select(fds, [], fds, TIMEOUT)[0]:
-                    out.append(os.read(fd, 1024))
-                if process.poll() is not None:
-                    break
+                while time.time() - started < TIMEOUT:
+                    for fd in select.select(fds, [], fds, TIMEOUT)[0]:
+                        out.append(os.read(fd, 1024))
+                    if process.poll() is not None:
+                        break
 
-            if process.poll() is None:
-                os.kill(process.pid, signal.SIGTERM)
-                time.sleep(1)
                 if process.poll() is None:
-                    os.kill(process.pid, signal.SIGKILL)
-            null.close()
+                    os.kill(process.pid, signal.SIGTERM)
+                    time.sleep(1)
+                    if process.poll() is None:
+                        os.kill(process.pid, signal.SIGKILL)
 
             self.assertEqual(process.poll(), 0,
                              "%s failed to return usage within %i seconds.\n"

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/collab-maint/devscripts.git



More information about the devscripts-devel mailing list