Issue 1463840: logging.StreamHandler ignores argument if it compares False (original) (raw)

The docs at http://docs.python.org/lib/node346.html say this: """ class StreamHandler( [strm]) Returns a new instance of the StreamHandler class. If strm is specified, the instance will use it for logging output; otherwise, sys.stderr will be used. """

However, that's not quite true. StreamHandler.init() actually tests for truth of strm, which means you have to be careful that strm does not happen to evaluate to boolean false.

My use case: I'm writing some tests that verify that certain methods put certain strings into the logs. So my setUp() adds a StreamHandler with its stream set to an instance of this trivial class:

class FakeLog(list): def write(self, msg): self.append(msg) def flush(self): pass

... which does not work, because an empty list evaluates to false, so my handler writes to stderr even though i told it not to. It's trivial to work around this by adding a nonzero method, but the need to do so is certainly not clear from the docs. Therefore imho this is a bug. The patch is trivial, and is attached.