From 6bac0216aa2d1753dbca39a2d059a07e4dcfa206 Mon Sep 17 00:00:00 2001 From: Randall Leeds Date: Sun, 18 Sep 2011 03:53:35 -0700 Subject: [PATCH] add util.closerange using os module or fallback --- gunicorn/util.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/gunicorn/util.py b/gunicorn/util.py index a6fe2ad5..dfa4d32e 100644 --- a/gunicorn/util.py +++ b/gunicorn/util.py @@ -169,6 +169,17 @@ def close(sock): except socket.error: pass +try: + from os import closerange +except ImportError: + def closerange(fd_low, fd_high): + # Iterate through and close all file descriptors. + for fd in krange(fd_low, fd_high): + try: + os.close(fd) + except OSError: # ERROR, fd wasn't open to begin with (ignored) + pass + def write_chunk(sock, data): chunk = "".join(("%X\r\n" % len(data), data, "\r\n")) sock.sendall(chunk) @@ -281,13 +292,7 @@ def daemonize(): os.umask(0) maxfd = get_maxfd() - - # Iterate through and close all file descriptors. - for fd in range(0, maxfd): - try: - os.close(fd) - except OSError: # ERROR, fd wasn't open to begin with (ignored) - pass + closerange(0, maxfd) os.open(REDIRECT_TO, os.O_RDWR) os.dup2(0, 1)