From c3be57f9047eb3f48769b8523a43b36b05bd430b Mon Sep 17 00:00:00 2001 From: Matthias Bussonnier Date: Tue, 15 Dec 2020 10:00:45 -0800 Subject: [PATCH 1/2] 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. --- Lib/logging/__init__.py | 8 ++++++++ Lib/test/test_logging.py | 9 +++++++++ .../Library/2020-12-15-10-00-04.bpo-42644.XgLCNx.rst | 7 +++++++ 3 files changed, 24 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2020-12-15-10-00-04.bpo-42644.XgLCNx.rst diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py index badfd654b16895..50b7378cd6386b 100644 --- a/Lib/logging/__init__.py +++ b/Lib/logging/__init__.py @@ -1289,6 +1289,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 e2196736dcdf4d..859baa4738ba7e 100644 --- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -4219,6 +4219,15 @@ def test_disable(self): 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 index 00000000000000..c271d7f38f6689 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-12-15-10-00-04.bpo-42644.XgLCNx.rst @@ -0,0 +1,7 @@ +`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. + +# Write your Misc/NEWS entry below. It should be a simple ReST paragraph. # +Don't start with "- Issue #: " or "- bpo-: " or that sort of stuff. +########################################################################### From a884eef0c12cdda4ff8ae6be2b1d9ed5a25d2e38 Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Wed, 16 Dec 2020 11:09:47 +0200 Subject: [PATCH 2/2] Update Misc/NEWS.d/next/Library/2020-12-15-10-00-04.bpo-42644.XgLCNx.rst --- .../next/Library/2020-12-15-10-00-04.bpo-42644.XgLCNx.rst | 4 ---- 1 file changed, 4 deletions(-) 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 index c271d7f38f6689..f58b58e4002ada 100644 --- 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 @@ -1,7 +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. - -# Write your Misc/NEWS entry below. It should be a simple ReST paragraph. # -Don't start with "- Issue #: " or "- bpo-: " or that sort of stuff. -###########################################################################