[pypy-commit] pypy default: Return level's constant when level's name is passed.
Flavio Percoco
noreply at buildbot.pypy.org
Mon Jun 17 00:33:18 CEST 2013
Author: Flavio Percoco <flaper87 at gmail.com>
Branch:
Changeset: r64898:26a150624b95
Date: 2013-06-16 23:58 +0200
http://bitbucket.org/pypy/pypy/changeset/26a150624b95/
Log: Return level's constant when level's name is passed.
After splitting _levelNames dictionary into 2 separate dicts, the
getLevelName function stopped returning the level's integer value
when the level's name is passed as argument. This patch restores
that behavior.
Fixes bug: 1517
diff --git a/lib-python/2.7/logging/__init__.py b/lib-python/2.7/logging/__init__.py
--- a/lib-python/2.7/logging/__init__.py
+++ b/lib-python/2.7/logging/__init__.py
@@ -134,6 +134,11 @@
DEBUG = 10
NOTSET = 0
+# NOTE(flaper87): This is different from
+# python's stdlib module since pypy's
+# dicts are much faster when their
+# keys are all of the same type.
+# Introduced in commit 9de7b40c586f
_levelToName = {
CRITICAL: 'CRITICAL',
ERROR: 'ERROR',
@@ -168,7 +173,11 @@
Otherwise, the string "Level %s" % level is returned.
"""
- return _levelToName.get(level, ("Level %s" % level))
+
+ # NOTE(flaper87): Check also in _nameToLevel
+ # if value is None.
+ return (_levelToName.get(level) or
+ _nameToLevel.get(level, ("Level %s" % level)))
def addLevelName(level, levelName):
"""
diff --git a/lib-python/2.7/test/test_logging.py b/lib-python/2.7/test/test_logging.py
--- a/lib-python/2.7/test/test_logging.py
+++ b/lib-python/2.7/test/test_logging.py
@@ -278,6 +278,24 @@
def test_invalid_name(self):
self.assertRaises(TypeError, logging.getLogger, any)
+ def test_get_level_name(self):
+ """Test getLevelName returns level constant."""
+ # NOTE(flaper87): Bug #1517
+ self.assertEqual(logging.getLevelName('NOTSET'), 0)
+ self.assertEqual(logging.getLevelName('DEBUG'), 10)
+ self.assertEqual(logging.getLevelName('INFO'), 20)
+ self.assertEqual(logging.getLevelName('WARN'), 30)
+ self.assertEqual(logging.getLevelName('WARNING'), 30)
+ self.assertEqual(logging.getLevelName('ERROR'), 40)
+ self.assertEqual(logging.getLevelName('CRITICAL'), 50)
+
+ self.assertEqual(logging.getLevelName(0), 'NOTSET')
+ self.assertEqual(logging.getLevelName(10), 'DEBUG')
+ self.assertEqual(logging.getLevelName(20), 'INFO')
+ self.assertEqual(logging.getLevelName(30), 'WARNING')
+ self.assertEqual(logging.getLevelName(40), 'ERROR')
+ self.assertEqual(logging.getLevelName(50), 'CRITICAL')
+
class BasicFilterTest(BaseTest):
"""Test the bundled Filter class."""
More information about the pypy-commit
mailing list