This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: traceback.print_exception throws AttributeError when exception is None
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.1, Python 3.2
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Boris.FELD, Claudiu.Popa, Giovanni.Funchal, abingham, ncoghlan, pitrou
Priority: normal Keywords: patch

Created on 2011-01-02 14:23 by abingham, last changed 2022-04-11 14:57 by admin. This issue is now closed.

File name Uploaded Description Edit
new_traceback_test_print_traceback.patch Boris.FELD, 2011-12-17 23:33 New traceback test patch review
Messages (5)
msg125054 - (view) Author: Austin Bingham (abingham) Date: 2011-01-02 14:23
traceback.print_exception() will throw an AttributeException if `value` is None and `chain` is True. This is because `_iter_chain` assumes that the exception object has a `__cause__` attribute. You can trigger this by trying for format a non-existent exception:

  >>> import logging, sys
  >>> logging.Formatter().formatException(sys.exc_info())
  Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.1/logging/", line 418, in formatException
    traceback.print_exception(ei[0], ei[1], ei[2], None, sio)
  File "/usr/lib/python3.1/", line 155, in print_exception
    for value, tb in values:
  File "/usr/lib/python3.1/", line 122, in _iter_chain
    cause = exc.__cause__

This is assuming that sys.exc_info() returns (None, None, None).
msg125216 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2011-01-03 19:03
Well, under 2.x, print_traceback(None, None, None) would just print "None", but I'm not sure that's a supported use case.
msg149719 - (view) Author: Boris FELD (Boris.FELD) * Date: 2011-12-17 23:33
I add a test to for this bug. Bug is confirmed on python 3.2 and python3.3. I use 2.x behavior as reference.

I don't know if we should add an assertion in traceback.print_exception or traceback._iter_chain, so I add the test for traceback.print_exception.
msg154055 - (view) Author: Giovanni Funchal (Giovanni.Funchal) Date: 2012-02-23 09:07
This bug affects me, found it when migrating from 2.7 to 3.2, in a function calling traceback.print_exc() called while there were no "active" exception being handled. Previous behavior was to print "None".
msg237518 - (view) Author: PCManticore (Claudiu.Popa) * (Python triager) Date: 2015-03-08 08:23
This was recently fixed in 3.5 by 73afda5a4e4c.
Date User Action Args
2022-04-11 14:57:10adminsetgithub: 55014
2015-03-08 08:23:16Claudiu.Popasetstatus: open -> closed

nosy: + Claudiu.Popa
messages: + msg237518

resolution: fixed
stage: resolved
2012-02-23 09:07:56Giovanni.Funchalsetnosy: + Giovanni.Funchal
messages: + msg154055
2011-12-17 23:33:41Boris.FELDsetfiles: + new_traceback_test_print_traceback.patch

nosy: + Boris.FELD
messages: + msg149719

keywords: + patch
2011-01-03 19:03:12pitrousetnosy: + ncoghlan
messages: + msg125216
2011-01-03 01:44:40pitrousetnosy: + pitrou

versions: + Python 3.2
2011-01-02 14:23:48abinghamcreate