diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2020-12-16 10:10:32 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-16 10:10:32 (GMT) |
commit | 0a24a57888798905e3b8891c59e61ed4f1bfc5a8 (patch) | |
tree | 85f628aadf26b0e26b7bfc9d1cbebc6c521444da /Lib | |
parent | aedc94b8e9d0f7700c665d8d1ba9c93df33e63a8 (diff) | |
download | cpython-0a24a57888798905e3b8891c59e61ed4f1bfc5a8.zip cpython-0a24a57888798905e3b8891c59e61ed4f1bfc5a8.tar.gz cpython-0a24a57888798905e3b8891c59e61ed4f1bfc5a8.tar.bz2 |
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
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>
(cherry picked from commit b32d8b4f9bcd2e7d11240b6b9de0262cf8f5e09d)
Co-authored-by: Matthias Bussonnier <bussonniermatthias@gmail.com>
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/logging/__init__.py | 8 | ||||
-rw-r--r-- | Lib/test/test_logging.py | 9 |
2 files changed, 17 insertions, 0 deletions
diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py index 79e0787a..da3f7b1 100644 --- a/Lib/logging/__init__.py +++ b/Lib/logging/__init__.py @@ -1269,6 +1269,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 diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py index b9392ae..3e15db0 100644 --- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -4170,6 +4170,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() |