Skip to content

Commit

Permalink
bpo-42644: Validate values in logging.disable() (python#23786)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
2 people authored and adorilson committed Mar 11, 2021
1 parent e498947 commit 680ad15
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Lib/logging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions Lib/test/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit 680ad15

Please sign in to comment.