summaryrefslogtreecommitdiffstats
path: root/Lib/logging/__init__.py
diff options
context:
space:
mode:
authorVinay Sajip <vinay_sajip@yahoo.co.uk>2009-07-13 11:28:25 (GMT)
committerVinay Sajip <vinay_sajip@yahoo.co.uk>2009-07-13 11:28:25 (GMT)
commitd4fabf410dd1675e25db6d49d3d84336f59b620c (patch)
treeb0b10c823d1038edf64482f90616b414ab112ca7 /Lib/logging/__init__.py
parentf67367e28bc8665e518b8588b3ad90ea7950965d (diff)
downloadcpython-d4fabf410dd1675e25db6d49d3d84336f59b620c.zip
cpython-d4fabf410dd1675e25db6d49d3d84336f59b620c.tar.gz
cpython-d4fabf410dd1675e25db6d49d3d84336f59b620c.tar.bz2
Issue #6314: logging: Extra checks on the "level" argument.
Diffstat (limited to 'Lib/logging/__init__.py')
-rw-r--r--Lib/logging/__init__.py23
1 files changed, 15 insertions, 8 deletions
diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py
index f932110..5072019 100644
--- a/Lib/logging/__init__.py
+++ b/Lib/logging/__init__.py
@@ -176,6 +176,17 @@ def addLevelName(level, levelName):
finally:
_releaseLock()
+def _checkLevel(level):
+ if isinstance(level, int):
+ rv = level
+ elif str(level) == level:
+ if level not in _levelNames:
+ raise ValueError("Unknown level: %r" % level)
+ rv = _levelNames[level]
+ else:
+ raise TypeError("Level not an integer or a valid string: %r" % level)
+ return rv
+
#---------------------------------------------------------------------------
# Thread-related stuff
#---------------------------------------------------------------------------
@@ -593,7 +604,7 @@ class Handler(Filterer):
and the filter list to empty.
"""
Filterer.__init__(self)
- self.level = level
+ self.level = _checkLevel(level)
self.formatter = None
#get the module data lock, as we're updating a shared structure.
_acquireLock()
@@ -631,7 +642,7 @@ class Handler(Filterer):
"""
Set the logging level of this handler.
"""
- self.level = level
+ self.level = _checkLevel(level)
def format(self, record):
"""
@@ -1009,7 +1020,7 @@ class Logger(Filterer):
"""
Filterer.__init__(self)
self.name = name
- self.level = level
+ self.level = _checkLevel(level)
self.parent = None
self.propagate = 1
self.handlers = []
@@ -1019,7 +1030,7 @@ class Logger(Filterer):
"""
Set the logging level of this logger.
"""
- self.level = level
+ self.level = _checkLevel(level)
def debug(self, msg, *args, **kwargs):
"""
@@ -1396,10 +1407,6 @@ def basicConfig(**kwargs):
root.addHandler(hdlr)
level = kwargs.get("level")
if level is not None:
- if str(level) == level: # If a string was passed, do more checks
- if level not in _levelNames:
- raise ValueError("Unknown level: %r" % level)
- level = _levelNames[level]
root.setLevel(level)
#---------------------------------------------------------------------------