Skip to content

Commit d33344a

Browse files
author
Victor Stinner
committed
Add cgi.closelog() function to close the log file
1 parent f64f9e9 commit d33344a

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

Lib/cgi.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def initlog(*allargs):
7676
send an error message).
7777
7878
"""
79-
global logfp, log
79+
global log, logfile, logfp
8080
if logfile and not logfp:
8181
try:
8282
logfp = open(logfile, "a")
@@ -96,6 +96,15 @@ def nolog(*allargs):
9696
"""Dummy function, assigned to log when logging is disabled."""
9797
pass
9898

99+
def closelog():
100+
"""Close the log file."""
101+
global log, logfile, logfp
102+
logfile = ''
103+
if logfp:
104+
logfp.close()
105+
logfp = None
106+
log = initlog
107+
99108
log = initlog # The current logging function
100109

101110

Lib/test/test_cgi.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,7 @@ def test_log(self):
155155
cgi.logfp = None
156156
cgi.logfile = "/dev/null"
157157
cgi.initlog("%s", "Testing log 3")
158-
def log_cleanup():
159-
"""Restore the global state of the log vars."""
160-
cgi.logfile = ''
161-
cgi.logfp.close()
162-
cgi.logfp = None
163-
cgi.log = cgi.initlog
164-
self.addCleanup(log_cleanup)
158+
self.addCleanup(cgi.closelog)
165159
cgi.log("Testing log 4")
166160

167161
def test_fieldstorage_readline(self):

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ Core and Builtins
225225
Library
226226
-------
227227

228+
- Add cgi.closelog() function to close the log file.
229+
228230
- Issue #12502: asyncore: fix polling loop with AF_UNIX sockets.
229231

230232
- Issue #4376: ctypes now supports nested structures in a endian different than

0 commit comments

Comments
 (0)