logging.FileHandler diff Py2 v Py3

Skip Montanaro skip.montanaro at gmail.com
Tue Apr 3 11:14:23 EDT 2018


I've encountered a problem in an application I'm porting from Python
2.7 to 3.6. The logginng.FileHandler class likes "/dev/stderr" as a
destination in Python 2, but complains in Python 3.

Python 2.7.14:

>>> import logging
>>> logging.FileHandler("/dev/stderr")
<logging.FileHandler object at 0x7fbbba9bbdd0>

Python 3.6.4:

>>> import logging
>>> logging.FileHandler("/dev/stderr")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/skip/miniconda3/envs/python3/lib/python3.6/logging/__init__.py",
line 1030, in __init__
    StreamHandler.__init__(self, self._open())
  File "/home/skip/miniconda3/envs/python3/lib/python3.6/logging/__init__.py",
line 1059, in _open
    return open(self.baseFilename, self.mode, encoding=self.encoding)
OSError: [Errno 29] Illegal seek

I can see how to work around the error, pass mode="w" when passing
/dev/{stdout,stderr} to the constructor. Shouldn't that bit of
ugliness be hidden in the logging module?

Skip



More information about the Python-list mailing list