From d445a7f776470c7813ddce462333f10310d070fe Mon Sep 17 00:00:00 2001 From: Vinay Sajip Date: Wed, 16 Jul 2014 15:41:46 +0100 Subject: Corrected fix in test for Python 2.x/3.x compatibility. --- tests/test_dictconfig.py | 52 +++++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/tests/test_dictconfig.py b/tests/test_dictconfig.py index e53bf5b..ff94a4d 100644 --- a/tests/test_dictconfig.py +++ b/tests/test_dictconfig.py @@ -27,12 +27,6 @@ def testHandler(): def handlerFunc(): return logging.StreamHandler() -def is_python2(): - if sys.version_info >= (2, 0) and sys.version_info < (3, 0): - return True - else: - return False - class CustomHandler(logging.StreamHandler): pass @@ -50,8 +44,11 @@ class ConfigDictTest(unittest.TestCase): self.saved_handlers = logging._handlers.copy() self.saved_handler_list = logging._handlerList[:] self.saved_loggers = logger_dict.copy() - if is_python2(): + if hasattr(logging, '_levelNames'): self.saved_level_names = logging._levelNames.copy() + else: + self.saved_level_to_name = logging._levelToName.copy() + self.saved_name_to_level = logging._nameToLevel.copy() finally: logging._releaseLock() @@ -63,9 +60,14 @@ class ConfigDictTest(unittest.TestCase): self.root_logger.setLevel(self.original_logging_level) logging._acquireLock() try: - if is_python2(): + if hasattr(logging, '_levelNames'): logging._levelNames.clear() logging._levelNames.update(self.saved_level_names) + else: + logging._levelToName.clear() + logging._levelToName.update(self.saved_level_to_name) + logging._nameToLevel.clear() + logging._nameToLevel.update(self.saved_name_to_level) logging._handlers.clear() logging._handlers.update(self.saved_handlers) logging._handlerList[:] = self.saved_handler_list @@ -126,7 +128,7 @@ class ConfigDictTest(unittest.TestCase): 'root' : { 'level' : 'WARNING', }, - } + } # config2 has a subtle configuration error that should be reported config2 = { @@ -152,7 +154,7 @@ class ConfigDictTest(unittest.TestCase): 'root' : { 'level' : 'WARNING', }, - } + } #As config1 but with a misspelt level on a handler config2a = { @@ -178,7 +180,7 @@ class ConfigDictTest(unittest.TestCase): 'root' : { 'level' : 'WARNING', }, - } + } #As config1 but with a misspelt level on a logger @@ -205,7 +207,7 @@ class ConfigDictTest(unittest.TestCase): 'root' : { 'level' : 'WRANING', }, - } + } # config3 has a less subtle configuration error config3 = { @@ -231,7 +233,7 @@ class ConfigDictTest(unittest.TestCase): 'root' : { 'level' : 'WARNING', }, - } + } # config4 specifies a custom formatter class to be loaded config4 = { @@ -252,7 +254,7 @@ class ConfigDictTest(unittest.TestCase): 'level' : 'NOTSET', 'handlers' : ['hand1'], }, - } + } # As config4 but using an actual callable rather than a string config4a = { @@ -284,7 +286,7 @@ class ConfigDictTest(unittest.TestCase): 'level' : 'NOTSET', 'handlers' : ['hand1'], }, - } + } # config5 specifies a custom handler class to be loaded config5 = { @@ -309,7 +311,7 @@ class ConfigDictTest(unittest.TestCase): 'root' : { 'level' : 'WARNING', }, - } + } # config6 specifies a custom handler class to be loaded # but has bad arguments @@ -337,7 +339,7 @@ class ConfigDictTest(unittest.TestCase): 'root' : { 'level' : 'WARNING', }, - } + } #config 7 does not define compiler.parser but defines compiler.lexer #so compiler.parser should be disabled after applying it @@ -363,7 +365,7 @@ class ConfigDictTest(unittest.TestCase): 'root' : { 'level' : 'WARNING', }, - } + } config8 = { 'version': 1, @@ -390,7 +392,7 @@ class ConfigDictTest(unittest.TestCase): 'root' : { 'level' : 'WARNING', }, - } + } config9 = { 'version': 1, @@ -414,7 +416,7 @@ class ConfigDictTest(unittest.TestCase): 'root' : { 'level' : 'NOTSET', }, - } + } config9a = { 'version': 1, @@ -429,7 +431,7 @@ class ConfigDictTest(unittest.TestCase): 'level' : 'INFO', }, }, - } + } config9b = { 'version': 1, @@ -444,7 +446,7 @@ class ConfigDictTest(unittest.TestCase): 'level' : 'INFO', }, }, - } + } #As config1 but with a filter added config10 = { @@ -476,7 +478,7 @@ class ConfigDictTest(unittest.TestCase): 'level' : 'WARNING', 'handlers' : ['hand1'], }, - } + } # As config10, but declaring a handler in a module using # absolute imports @@ -509,7 +511,7 @@ class ConfigDictTest(unittest.TestCase): 'level' : 'WARNING', 'handlers' : ['hand1'], }, - } + } def apply_config(self, conf): dictConfig(conf) @@ -687,7 +689,7 @@ class ConfigDictTest(unittest.TestCase): dict(levelname='WARNING', message='1'), dict(levelname='ERROR', message='4'), ])) - + def test_config_11_ok(self): self.apply_config(self.config11) h = logging.getLogger().handlers[0] -- cgit v1.2.1