Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d4fabf4

Browse files
committed
Issue #6314: logging: Extra checks on the "level" argument.
1 parent f67367e commit d4fabf4

2 files changed

Lines changed: 23 additions & 17 deletions

File tree

Lib/logging/__init__.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,17 @@ def addLevelName(level, levelName):
176176
finally:
177177
_releaseLock()
178178

179+
def _checkLevel(level):
180+
if isinstance(level, int):
181+
rv = level
182+
elif str(level) == level:
183+
if level not in _levelNames:
184+
raise ValueError("Unknown level: %r" % level)
185+
rv = _levelNames[level]
186+
else:
187+
raise TypeError("Level not an integer or a valid string: %r" % level)
188+
return rv
189+
179190
#---------------------------------------------------------------------------
180191
# Thread-related stuff
181192
#---------------------------------------------------------------------------
@@ -593,7 +604,7 @@ def __init__(self, level=NOTSET):
593604
and the filter list to empty.
594605
"""
595606
Filterer.__init__(self)
596-
self.level = level
607+
self.level = _checkLevel(level)
597608
self.formatter = None
598609
#get the module data lock, as we're updating a shared structure.
599610
_acquireLock()
@@ -631,7 +642,7 @@ def setLevel(self, level):
631642
"""
632643
Set the logging level of this handler.
633644
"""
634-
self.level = level
645+
self.level = _checkLevel(level)
635646

636647
def format(self, record):
637648
"""
@@ -1009,7 +1020,7 @@ def __init__(self, name, level=NOTSET):
10091020
"""
10101021
Filterer.__init__(self)
10111022
self.name = name
1012-
self.level = level
1023+
self.level = _checkLevel(level)
10131024
self.parent = None
10141025
self.propagate = 1
10151026
self.handlers = []
@@ -1019,7 +1030,7 @@ def setLevel(self, level):
10191030
"""
10201031
Set the logging level of this logger.
10211032
"""
1022-
self.level = level
1033+
self.level = _checkLevel(level)
10231034

10241035
def debug(self, msg, *args, **kwargs):
10251036
"""
@@ -1396,10 +1407,6 @@ def basicConfig(**kwargs):
13961407
root.addHandler(hdlr)
13971408
level = kwargs.get("level")
13981409
if level is not None:
1399-
if str(level) == level: # If a string was passed, do more checks
1400-
if level not in _levelNames:
1401-
raise ValueError("Unknown level: %r" % level)
1402-
level = _levelNames[level]
14031410
root.setLevel(level)
14041411

14051412
#---------------------------------------------------------------------------

Misc/NEWS

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ Core and Builtins
9090
Library
9191
-------
9292

93-
- Issue #6314: logging.basicConfig() performs extra checks on the "level"
94-
argument.
93+
- Issue #6314: logging: performs extra checks on the "level" argument.
9594

9695
- Issue #6274: Fixed possible file descriptors leak in subprocess.py
9796

@@ -133,7 +132,7 @@ Library
133132
-------
134133

135134
- Issue #6438: Fixed distutils.cygwinccompiler.get_versions : the regular
136-
expression string pattern was trying to match against a bytes returned by
135+
expression string pattern was trying to match against a bytes returned by
137136
Popen. Tested under win32 to build the py-postgresql project.
138137

139138
- Issue #6258: Support AMD64 in bdist_msi.
@@ -892,22 +891,22 @@ Core and Builtins
892891
Library
893892
-------
894893

895-
- Issue #6459: distutils.command.build_ext.get_export_symbols now uses the
896-
"PyInit" prefix, rather than "init".
894+
- Issue #6459: distutils.command.build_ext.get_export_symbols now uses the
895+
"PyInit" prefix, rather than "init".
897896

898-
- Issue #6455: Fixed test_build_ext under win32.
897+
- Issue #6455: Fixed test_build_ext under win32.
899898

900-
- Issue #6377: Enabled the compiler option, and deprecate its usage as an
899+
- Issue #6377: Enabled the compiler option, and deprecate its usage as an
901900
attribute.
902901

903902
- Issue #6413: Fixed the log level in distutils.dist for announce.
904903

905904
- Issue #6403: Fixed package path usage in build_ext.
906905

907-
- Issue #6365: Distutils build_ext inplace mode was copying the compiled
906+
- Issue #6365: Distutils build_ext inplace mode was copying the compiled
908907
extension in a subdirectory if the extension name had dots.
909908

910-
- Issue #6164: Added an AIX specific linker argument in Distutils
909+
- Issue #6164: Added an AIX specific linker argument in Distutils
911910
unixcompiler. Original patch by Sridhar Ratnakumar.
912911

913912
- Issue #6286: Now Distutils upload command is based on urllib2 instead of

0 commit comments

Comments
 (0)