Skip to content

Commit 0a24a57

Browse files
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 b32d8b4) Co-authored-by: Matthias Bussonnier <bussonniermatthias@gmail.com>
1 parent aedc94b commit 0a24a57

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

Lib/logging/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,6 +1269,14 @@ def __init__(self, rootnode):
12691269
self.loggerClass = None
12701270
self.logRecordFactory = None
12711271

1272+
@property
1273+
def disable(self):
1274+
return self._disable
1275+
1276+
@disable.setter
1277+
def disable(self, value):
1278+
self._disable = _checkLevel(value)
1279+
12721280
def getLogger(self, name):
12731281
"""
12741282
Get a logger with the specified name (channel name), creating it

Lib/test/test_logging.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4170,6 +4170,15 @@ def test_disable(self):
41704170
logging.disable(83)
41714171
self.assertEqual(logging.root.manager.disable, 83)
41724172

4173+
self.assertRaises(ValueError, logging.disable, "doesnotexists")
4174+
4175+
class _NotAnIntOrString:
4176+
pass
4177+
4178+
self.assertRaises(TypeError, logging.disable, _NotAnIntOrString())
4179+
4180+
logging.disable("WARN")
4181+
41734182
# test the default value introduced in 3.7
41744183
# (Issue #28524)
41754184
logging.disable()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
`logging.disable` will now validate the types and value of its parameter. It
2+
also now accepts strings representing the levels (as does `loging.setLevel`)
3+
instead of only the numerical values.

0 commit comments

Comments
 (0)