[Python-checkins] bpo-42644: Validate values in logging.disable() (GH-23786)

miss-islington webhook-mailer at python.org
Wed Dec 16 05:12:18 EST 2020

commit: db63da7e5d4a98925a04f903a19bf3595b9c2c46
branch: 3.9
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2020-12-16T02:12:08-08:00

bpo-42644: Validate values in logging.disable() (GH-23786)

* bpo-42644: Validate values in logging.disable()

Technically make the value of manager a property that checks and convert
values assigned to it properly. This has the side effect of making
`logging.disable` also accept strings representing the various level of

We want to validate the type of the disable attribute at assignment
time, as it is later compared to other levels when emitting warnings and
would generate a `TypeError: '>=' not supported between ....` in a
different part of the code base, which can make it difficult to track

When assigned an incorrect value; it will raise a TypeError when the
wrong type, or ValueError if an invalid str.

Co-authored-by: Andrew Svetlov <andrew.svetlov at gmail.com>
(cherry picked from commit b32d8b4f9bcd2e7d11240b6b9de0262cf8f5e09d)

Co-authored-by: Matthias Bussonnier <bussonniermatthias at gmail.com>

A Misc/NEWS.d/next/Library/2020-12-15-10-00-04.bpo-42644.XgLCNx.rst
M Lib/logging/__init__.py
M Lib/test/test_logging.py

diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py
index 7b169a16fbb70..6920a7b654a59 100644
--- a/Lib/logging/__init__.py
+++ b/Lib/logging/__init__.py
@@ -1269,6 +1269,14 @@ def __init__(self, rootnode):
         self.loggerClass = None
         self.logRecordFactory = None
+    @property
+    def disable(self):
+        return self._disable
+    @disable.setter
+    def disable(self, value):
+        self._disable = _checkLevel(value)
     def getLogger(self, name):
         Get a logger with the specified name (channel name), creating it
diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py
index 410eae2208688..a6cd291c9a553 100644
--- a/Lib/test/test_logging.py
+++ b/Lib/test/test_logging.py
@@ -4191,6 +4191,15 @@ def test_disable(self):
         self.assertEqual(logging.root.manager.disable, 83)
+        self.assertRaises(ValueError, logging.disable, "doesnotexists")
+        class _NotAnIntOrString:
+            pass
+        self.assertRaises(TypeError, logging.disable, _NotAnIntOrString())
+        logging.disable("WARN")
         # test the default value introduced in 3.7
         # (Issue #28524)
diff --git a/Misc/NEWS.d/next/Library/2020-12-15-10-00-04.bpo-42644.XgLCNx.rst b/Misc/NEWS.d/next/Library/2020-12-15-10-00-04.bpo-42644.XgLCNx.rst
new file mode 100644
index 0000000000000..f58b58e4002ad
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-12-15-10-00-04.bpo-42644.XgLCNx.rst
@@ -0,0 +1,3 @@
+`logging.disable` will now validate the types and value of its parameter. It
+also now accepts strings representing the levels (as does `loging.setLevel`)
+instead of only the numerical values.

More information about the Python-checkins mailing list