]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-42644: Validate values in logging.disable() (#23786)
authorMatthias Bussonnier <bussonniermatthias@gmail.com>
Wed, 16 Dec 2020 09:43:39 +0000 (01:43 -0800)
committerGitHub <noreply@github.com>
Wed, 16 Dec 2020 09:43:39 +0000 (11:43 +0200)
* 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
warnings.

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
down.

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@gmail.com>
Lib/logging/__init__.py
Lib/test/test_logging.py
Misc/NEWS.d/next/Library/2020-12-15-10-00-04.bpo-42644.XgLCNx.rst [new file with mode: 0644]

index badfd654b16895092e26ad60d02d6bd6affc3164..50b7378cd6386b7f48d490b81787baf427441983 100644 (file)
@@ -1289,6 +1289,14 @@ class Manager(object):
         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
index e2196736dcdf4dc421a0a14f1c5c2ed5798f5da5..859baa4738ba7e6531b8e7949e412a24a33180b1 100644 (file)
@@ -4219,6 +4219,15 @@ class ModuleLevelMiscTest(BaseTest):
         logging.disable(83)
         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)
         logging.disable()
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 (file)
index 0000000..f58b58e
--- /dev/null
@@ -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.