Skip to content

Commit b32d8b4

Browse files
Carreauasvetlov
andauthored
bpo-42644: Validate values in logging.disable() (#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 <[email protected]>
1 parent 3f9fe23 commit b32d8b4

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
@@ -1289,6 +1289,14 @@ def __init__(self, rootnode):
12891289
self.loggerClass = None
12901290
self.logRecordFactory = None
12911291

1292+
@property
1293+
def disable(self):
1294+
return self._disable
1295+
1296+
@disable.setter
1297+
def disable(self, value):
1298+
self._disable = _checkLevel(value)
1299+
12921300
def getLogger(self, name):
12931301
"""
12941302
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
@@ -4219,6 +4219,15 @@ def test_disable(self):
42194219
logging.disable(83)
42204220
self.assertEqual(logging.root.manager.disable, 83)
42214221

4222+
self.assertRaises(ValueError, logging.disable, "doesnotexists")
4223+
4224+
class _NotAnIntOrString:
4225+
pass
4226+
4227+
self.assertRaises(TypeError, logging.disable, _NotAnIntOrString())
4228+
4229+
logging.disable("WARN")
4230+
42224231
# test the default value introduced in 3.7
42234232
# (Issue #28524)
42244233
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)