[Python-checkins] cpython: Add cgi.closelog() function to close the log file

victor.stinner python-checkins at python.org
Thu Jul 14 22:31:56 CEST 2011


http://hg.python.org/cpython/rev/66e519792e4c
changeset:   71337:66e519792e4c
user:        Victor Stinner <victor.stinner at haypocalc.com>
date:        Thu Jul 14 22:28:36 2011 +0200
summary:
  Add cgi.closelog() function to close the log file

files:
  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
--- a/Lib/cgi.py
+++ b/Lib/cgi.py
@@ -76,7 +76,7 @@
     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 @@
     """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
--- a/Lib/test/test_cgi.py
+++ b/Lib/test/test_cgi.py
@@ -155,13 +155,7 @@
             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
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -225,6 +225,8 @@
 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

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list