Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Doc/whatsnew/3.12.rst
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,9 @@ Removed
*context* parameter instead.
(Contributed by Victor Stinner in :gh:`94172`.)

* :mod:`cgi`: Remove ``cgi.log()``, deprecated since Python 3.10.
Use :mod:`logging` instead.
(Contributed by Hugo van Kemenade in :gh:`101850`.)

Porting to Python 3.12
======================
Expand Down
62 changes: 0 additions & 62 deletions Lib/cgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,68 +56,6 @@

warnings._deprecated(__name__, remove=(3,13))

# Logging support
# ===============

logfile = "" # Filename to log to, if not empty
logfp = None # File object to log to, if not None

def initlog(*allargs):
"""Write a log message, if there is a log file.

Even though this function is called initlog(), you should always
use log(); log is a variable that is set either to initlog
(initially), to dolog (once the log file has been opened), or to
nolog (when logging is disabled).

The first argument is a format string; the remaining arguments (if
any) are arguments to the % operator, so e.g.
log("%s: %s", "a", "b")
will write "a: b" to the log file, followed by a newline.

If the global logfp is not None, it should be a file object to
which log data is written.

If the global logfp is None, the global logfile may be a string
giving a filename to open, in append mode. This file should be
world writable!!! If the file can't be opened, logging is
silently disabled (since there is no safe place where we could
send an error message).

"""
global log, logfile, logfp
warnings.warn("cgi.log() is deprecated as of 3.10. Use logging instead",
DeprecationWarning, stacklevel=2)
if logfile and not logfp:
try:
logfp = open(logfile, "a", encoding="locale")
except OSError:
pass
if not logfp:
log = nolog
else:
log = dolog
log(*allargs)

def dolog(fmt, *args):
"""Write a log message to the log file. See initlog() for docs."""
logfp.write(fmt%args + "\n")

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


# Parsing functions
# =================
Expand Down
14 changes: 0 additions & 14 deletions Lib/test/test_cgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,20 +223,6 @@ def test_separator(self):
else:
self.assertEqual(fs.getvalue(key), expect_val[0])

@warnings_helper.ignore_warnings(category=DeprecationWarning)
def test_log(self):
cgi.log("Testing")

cgi.logfp = StringIO()
cgi.initlog("%s", "Testing initlog 1")
cgi.log("%s", "Testing log 2")
self.assertEqual(cgi.logfp.getvalue(), "Testing initlog 1\nTesting log 2\n")
if os.path.exists(os.devnull):
cgi.logfp = None
cgi.logfile = os.devnull
cgi.initlog("%s", "Testing log 3")
self.addCleanup(cgi.closelog)
cgi.log("Testing log 4")

def test_fieldstorage_readline(self):
# FieldStorage uses readline, which has the capacity to read all
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_pyclbr.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def test_others(self):
cm('random', ignore=('Random',)) # from _random import Random as CoreGenerator
with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning)
cm('cgi', ignore=('log',)) # set with = in module
cm('cgi') # set with = in module
cm('pickle', ignore=('partial', 'PickleBuffer'))
with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:mod:`cgi`: Remove ``cgi.log()``, deprecated since Python 3.10. Use
:mod:`logging` instead. Patch by Hugo van Kemenade.