From d33344a030bececd68ce487445cd47a11ebdb3fd Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 14 Jul 2011 22:28:36 +0200 Subject: Add cgi.closelog() function to close the log file --- Lib/cgi.py | 11 ++++++++++- Lib/test/test_cgi.py | 8 +------- Misc/NEWS | 2 ++ 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/Lib/cgi.py b/Lib/cgi.py index e198ed8..63771fc 100755 --- a/Lib/cgi.py +++ b/Lib/cgi.py @@ -76,7 +76,7 @@ def initlog(*allargs): send an error message). """ - global logfp, log + global log, logfile, logfp if logfile and not logfp: try: logfp = open(logfile, "a") @@ -96,6 +96,15 @@ def nolog(*allargs): """Dummy function, assigned to log when logging is disabled.""" pass +def closelog(): + """Close the log file.""" + global log, logfile, logfp + logfile = '' + if logfp: + logfp.close() + logfp = None + log = initlog + log = initlog # The current logging function diff --git a/Lib/test/test_cgi.py b/Lib/test/test_cgi.py index c42db4e..dba7727 100644 --- a/Lib/test/test_cgi.py +++ b/Lib/test/test_cgi.py @@ -155,13 +155,7 @@ class CgiTests(unittest.TestCase): cgi.logfp = None cgi.logfile = "/dev/null" cgi.initlog("%s", "Testing log 3") - def log_cleanup(): - """Restore the global state of the log vars.""" - cgi.logfile = '' - cgi.logfp.close() - cgi.logfp = None - cgi.log = cgi.initlog - self.addCleanup(log_cleanup) + self.addCleanup(cgi.closelog) cgi.log("Testing log 4") def test_fieldstorage_readline(self): diff --git a/Misc/NEWS b/Misc/NEWS index 18c79a0..7e97bed 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -225,6 +225,8 @@ Core and Builtins Library ------- +- Add cgi.closelog() function to close the log file. + - Issue #12502: asyncore: fix polling loop with AF_UNIX sockets. - Issue #4376: ctypes now supports nested structures in a endian different than -- cgit v0.12