# HG changeset patch # User fschwarz # Date 1299337138 0 # Node ID 22ad1d9936e76a3994b487b765fdc45aa3684579 # Parent 931424efdcf0a5e0b16feed960c7d92297e73f99 merge r573, r575 from trunk to 0.9 branch diff --git a/0.9.x/babel/messages/frontend.py b/0.9.x/babel/messages/frontend.py --- a/0.9.x/babel/messages/frontend.py +++ b/0.9.x/babel/messages/frontend.py @@ -626,15 +626,7 @@ options, args = self.parser.parse_args(argv[1:]) - # Configure logging - self.log = logging.getLogger('babel') - self.log.setLevel(options.loglevel) - handler = logging.StreamHandler() - handler.setLevel(options.loglevel) - formatter = logging.Formatter('%(message)s') - handler.setFormatter(formatter) - self.log.addHandler(handler) - + self._configure_logging(options.loglevel) if options.list_locales: identifiers = localedata.list() longest = max([len(identifier) for identifier in identifiers]) @@ -658,6 +650,21 @@ return getattr(self, cmdname)(args[1:]) + def _configure_logging(self, loglevel): + self.log = logging.getLogger('babel') + self.log.setLevel(loglevel) + # 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(loglevel) + formatter = logging.Formatter('%(message)s') + handler.setFormatter(formatter) + def _help(self): print self.parser.format_help() print "commands:" diff --git a/0.9.x/babel/messages/tests/frontend.py b/0.9.x/babel/messages/tests/frontend.py --- a/0.9.x/babel/messages/tests/frontend.py +++ b/0.9.x/babel/messages/tests/frontend.py @@ -16,6 +16,7 @@ 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 @@ 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):