[Python-Dev] Problems with regrtest and with logging

Vinay Sajip vinay_sajip at yahoo.co.uk
Sun May 8 16:22:18 CEST 2011

Éric Araujo <merwok <at> netwok.org> writes:

>  The code is on https://bitbucket.org/tarek/cpython, in Lib/packaging.

The cases you refer to seem to be _set_logger in packaging/run.py (which appears
not to be used at all - there appear to be no other references to it in the
code), Dispatcher.__init__ in packaging/run.py and
Distribution.parse_command_line in packaging/dist.py.

I can't see why the first case is there.

In the second and third cases, can you be sure that only one of these code paths
will be executed, at most once? If not, multiple StreamHandler instances would
be added to the logger, resulting in duplicated messages. If the code paths will
be executed at most once, then the code seems to be acceptable. You may wish to
add a guard using "if not logger.hasHandlers():" so that even if the code is
executed multiple times, a handler isn't added multiple times.

In the case of the test support code, I'm not really sure that LoggingCatcher is
needed. There is already a TestHandler class in test.support which captures
records in a buffer, and allows flexible matching for assertions, as described in


The _handlerList in logging contains weak references to handlers, and when the
referent is finalised, it's removed from the list. If you want to control this
more finely, you could do something like (untested):

class MyTestCase(unittest.TestCase):
    def setUp(self):
        self.handler = TestHandler(Matcher())

    def tearDown(self):
        refs = weakref.getweakrefs(self.handler)
        for ref in refs:
    def test_something(self):


Vinay Sajip

More information about the Python-Dev mailing list