]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
prevent !CommandLineInterface.run from accumulating logging handlers (fixes #227)
authorFelix Schwarz <felix.schwarz@oss.schwarz.eu>
Sat, 5 Mar 2011 14:48:59 +0000 (14:48 +0000)
committerFelix Schwarz <felix.schwarz@oss.schwarz.eu>
Sat, 5 Mar 2011 14:48:59 +0000 (14:48 +0000)
babel/messages/frontend.py
babel/messages/tests/frontend.py

index b1ebfb4a7c6202609af6369a53452cf9d227abeb..d6b855159cd854dd2ff9ff29388dbb0dd8505407 100755 (executable)
@@ -629,11 +629,17 @@ class CommandLineInterface(object):
         # Configure logging
         self.log = logging.getLogger('babel')
         self.log.setLevel(options.loglevel)
-        handler = logging.StreamHandler()
+        # Don't add a new handler for every instance initialization (#227), this
+        # would cause duplicated output when the CommandLineInterface as an
+        # normal Python class.
+        if self.log.handlers:
+            handler = self.log.handlers[0]
+        else:
+            handler = logging.StreamHandler()
+            self.log.addHandler(handler)
         handler.setLevel(options.loglevel)
         formatter = logging.Formatter('%(message)s')
         handler.setFormatter(formatter)
-        self.log.addHandler(handler)
 
         if options.list_locales:
             identifiers = localedata.list()
index 374b42e004e9b1ca353a7d51c5998e247fc0958f..00ddcce850710059b1a90af4b9144fc1d32e1b76 100644 (file)
@@ -16,6 +16,7 @@ from distutils.dist import Distribution
 from distutils.errors import DistutilsOptionError
 from distutils.log import _global_log
 import doctest
+import logging
 import os
 import shutil
 from StringIO import StringIO
@@ -509,6 +510,14 @@ class CommandLineInterfaceTestCase(unittest.TestCase):
         sys.argv = ['pybabel']
         sys.stdout = StringIO()
         sys.stderr = StringIO()
+        
+        # Logging handlers will be reused if possible (#227). This breaks the 
+        # implicit assumption that our newly created StringIO for sys.stderr 
+        # contains the console output. Removing the old handler ensures that a
+        # new handler with our new StringIO instance will be used.
+        log = logging.getLogger('babel')
+        for handler in log.handlers:
+            log.removeHandler(handler)
         self.cli = frontend.CommandLineInterface()
 
     def tearDown(self):