[Python-Dev] Problems with regrtest and with logging (original) (raw)
Vinay Sajip vinay_sajip at yahoo.co.uk
Sun May 8 16:22:18 CEST 2011
- Previous message: [Python-Dev] Problems with regrtest and with logging
- Next message: [Python-Dev] Problems with regrtest and with logging
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Éric Araujo <merwok 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
http://plumberjack.blogspot.com/2010/09/unit-testing-and-logging.html
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()) logging.getLogger().addHandler(self.handler)
def tearDown(self):
logging.getLogger().removeHandler(self.handler)
self.handler.close()
refs = weakref.getweakrefs(self.handler)
for ref in refs:
logging._removeHandlerRef(ref)
def test_something(self):
logging.warning('Test')
self.assertTrue(self.handler.matches(message='Test'))
Regards,
Vinay Sajip
- Previous message: [Python-Dev] Problems with regrtest and with logging
- Next message: [Python-Dev] Problems with regrtest and with logging
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]