]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Add cgi.closelog() function to close the log file
authorVictor Stinner <victor.stinner@haypocalc.com>
Thu, 14 Jul 2011 20:28:36 +0000 (22:28 +0200)
committerVictor Stinner <victor.stinner@haypocalc.com>
Thu, 14 Jul 2011 20:28:36 +0000 (22:28 +0200)
Lib/cgi.py
Lib/test/test_cgi.py
Misc/NEWS

index e198ed8653f20db60370f94f7fe439f76cf0efc8..63771fc9c6c38f94d471a40b41e83eeec274e1b8 100755 (executable)
@@ -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
 
 
index c42db4e70d989004e3be636e3133f2099a8f194f..dba77276d2f9732de7af1fba76d40126a72373c2 100644 (file)
@@ -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):
index 18c79a023326c5084e26475890ba50f8f4c8bd06..7e97bed311edf8fe8d92cc6b5d3928e1e27f3fb8 100644 (file)
--- 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