classification
Title: SyslogHandler's record formatting during emit(..) not covered by try-except block
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.6, Python 3.5, Python 3.4, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Yoel, python-dev, vinay.sajip
Priority: normal Keywords:

Created on 2014-10-31 18:13 by Yoel, last changed 2014-11-01 20:03 by vinay.sajip. This issue is now closed.

Messages (3)
msg230364 - (view) Author: Yoel (Yoel) Date: 2014-10-31 18:13
Not all exceptions occurring during SyslogHandler's emit(..) method are caught and handled since the try-except block only covers the latter part of the method.
Thus, since handleError is not invoked, such exceptions might cause the whole program to crash, even though logging.raiseExceptions is unset.

Execution example:

In [1]: import sys

In [2]: sys.version
Out[2]: '2.7.6 (default, Mar 22 2014, 22:59:56) \n[GCC 4.8.2]'

In [3]: import logging.handlers

In [4]: logging.raiseExceptions = 0

In [5]: syslogHandler = logging.handlers.SysLogHandler()

In [6]: logger = logging.getLogger()

In [7]: logger.addHandler(syslogHandler)

In [8]: logger.critical('1', '1')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-3d1c89a066c4> in <module>()
----> 1 logger.critical('1', '1')

/usr/lib/python2.7/logging/__init__.pyc in critical(self, msg, *args, **kwargs)
   1195         """
   1196         if self.isEnabledFor(CRITICAL):
-> 1197             self._log(CRITICAL, msg, args, **kwargs)
   1198 
   1199     fatal = critical

/usr/lib/python2.7/logging/__init__.pyc in _log(self, level, msg, args, exc_info, extra)
   1269                 exc_info = sys.exc_info()
   1270         record = self.makeRecord(self.name, level, fn, lno, msg, args, exc_info, func, extra)
-> 1271         self.handle(record)
   1272 
   1273     def handle(self, record):

/usr/lib/python2.7/logging/__init__.pyc in handle(self, record)
   1279         """
   1280         if (not self.disabled) and self.filter(record):
-> 1281             self.callHandlers(record)
   1282 
   1283     def addHandler(self, hdlr):

/usr/lib/python2.7/logging/__init__.pyc in callHandlers(self, record)
   1319                 found = found + 1
   1320                 if record.levelno >= hdlr.level:
-> 1321                     hdlr.handle(record)
   1322             if not c.propagate:
   1323                 c = None    #break out

/usr/lib/python2.7/logging/__init__.pyc in handle(self, record)
    747             self.acquire()
    748             try:
--> 749                 self.emit(record)
    750             finally:
    751                 self.release()

/usr/lib/python2.7/logging/handlers.pyc in emit(self, record)
    840         exception information is present, it is NOT sent to the server.
    841         """
--> 842         msg = self.format(record) + '\000'
    843         """
    844         We need to convert record level to lowercase, maybe this will

/usr/lib/python2.7/logging/__init__.pyc in format(self, record)
    722         else:
    723             fmt = _defaultFormatter
--> 724         return fmt.format(record)
    725 
    726     def emit(self, record):

/usr/lib/python2.7/logging/__init__.pyc in format(self, record)
    462         it is formatted using formatException() and appended to the message.
    463         """
--> 464         record.message = record.getMessage()
    465         if self.usesTime():
    466             record.asctime = self.formatTime(record, self.datefmt)

/usr/lib/python2.7/logging/__init__.pyc in getMessage(self)
    326                     msg = self.msg      #Defer encoding till later
    327         if self.args:
--> 328             msg = msg % self.args
    329         return msg
    330 

TypeError: not all arguments converted during string formatting

In [9]: 

Executing similar code via StreamHandler for example, doesn't raise the TypeError exception.
msg230458 - (view) Author: Roundup Robot (python-dev) Date: 2014-11-01 20:01
New changeset 54549f9b2ecc by Vinay Sajip in branch 'default':
Closes #22776: Merged fix from 3.4.
https://hg.python.org/cpython/rev/54549f9b2ecc
msg230459 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2014-11-01 20:03
N.B. 2.7 and 3.4 also updated, but I accidentally left the issue no. out of the commit message. Relevant changesets are ea7b64406396 and f6a906541476.
History
Date User Action Args
2014-11-01 20:03:23vinay.sajipsetversions: - Python 3.2, Python 3.3
2014-11-01 20:03:09vinay.sajipsetmessages: + msg230459
2014-11-01 20:01:15python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg230458

resolution: fixed
stage: resolved
2014-10-31 18:18:38Yoelsettype: behavior
2014-10-31 18:13:10Yoelcreate