Kaydet (Commit) d33344a0 authored tarafından Victor Stinner's avatar Victor Stinner

Add cgi.closelog() function to close the log file

üst f64f9e9e
...@@ -76,7 +76,7 @@ def initlog(*allargs): ...@@ -76,7 +76,7 @@ def initlog(*allargs):
send an error message). send an error message).
""" """
global logfp, log global log, logfile, logfp
if logfile and not logfp: if logfile and not logfp:
try: try:
logfp = open(logfile, "a") logfp = open(logfile, "a")
...@@ -96,6 +96,15 @@ def nolog(*allargs): ...@@ -96,6 +96,15 @@ def nolog(*allargs):
"""Dummy function, assigned to log when logging is disabled.""" """Dummy function, assigned to log when logging is disabled."""
pass 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 log = initlog # The current logging function
......
...@@ -155,13 +155,7 @@ class CgiTests(unittest.TestCase): ...@@ -155,13 +155,7 @@ class CgiTests(unittest.TestCase):
cgi.logfp = None cgi.logfp = None
cgi.logfile = "/dev/null" cgi.logfile = "/dev/null"
cgi.initlog("%s", "Testing log 3") cgi.initlog("%s", "Testing log 3")
def log_cleanup(): self.addCleanup(cgi.closelog)
"""Restore the global state of the log vars."""
cgi.logfile = ''
cgi.logfp.close()
cgi.logfp = None
cgi.log = cgi.initlog
self.addCleanup(log_cleanup)
cgi.log("Testing log 4") cgi.log("Testing log 4")
def test_fieldstorage_readline(self): def test_fieldstorage_readline(self):
......
...@@ -225,6 +225,8 @@ Core and Builtins ...@@ -225,6 +225,8 @@ Core and Builtins
Library Library
------- -------
- Add cgi.closelog() function to close the log file.
- Issue #12502: asyncore: fix polling loop with AF_UNIX sockets. - Issue #12502: asyncore: fix polling loop with AF_UNIX sockets.
- Issue #4376: ctypes now supports nested structures in a endian different than - Issue #4376: ctypes now supports nested structures in a endian different than
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment