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: code.InteractiveInterpreter doesn't display the exception cause
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.5
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Claudiu.Popa, berker.peksag, georg.brandl, pitrou, pjenvey, python-dev, r.david.murray, serhiy.storchaka, terry.reedy
Priority: normal Keywords: patch

Created on 2013-03-16 21:17 by pjenvey, last changed 2022-04-11 14:57 by admin. This issue is now closed.

File name Uploaded Description Edit
code.patch Claudiu.Popa, 2013-08-20 22:04 review
issue17442.patch Claudiu.Popa, 2014-03-11 21:34 Remove trailing whitespaces review
issue17442_1.patch Claudiu.Popa, 2014-03-16 18:20 Added documentation note regarding this change. review
issue17442_2.patch Claudiu.Popa, 2014-09-10 12:44 review
Messages (16)
msg184355 - (view) Author: Philip Jenvey (pjenvey) * (Python committer) Date: 2013-03-16 21:17
The code module claims to emulate Python's interactive interpreter but it fails to emulate displaying of the exception cause.

It can utilize traceback._iter_chain to do this (see traceback.print_exception)
msg187275 - (view) Author: Philip Jenvey (pjenvey) * (Python committer) Date: 2013-04-18 18:00
PyPy's fixed this here:

The tests just need to be adapted to the stdlib test suite
msg195719 - (view) Author: PCManticore (Claudiu.Popa) * (Python triager) Date: 2013-08-20 22:04
Here's an updated patch for the stdlib, with adapted tests.
msg198837 - (view) Author: PCManticore (Claudiu.Popa) * (Python triager) Date: 2013-10-02 13:21
Could anyone review this patch, please?
msg217103 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2014-04-23 23:45
I am on the fence as to whether this should be treated as a bug fix or enhancement. Claudiu's pydev post gives this as the current InteractiveInterpreter behavior.

>>> try:
...    1 / 0
... except ZeroDivisionError as exc:
...    raise IOError from exc
Traceback (most recent call last):
  File "<console>", line 4, in <module>

This certainly is not an exact emulation (given below), but is it severe enough to be a bug, and moreover, would fixing it break code?
Again from Claudiu's post: with the patch.

>>> try:
...    1 / 0
... except ZeroDivisionError as exc:
...    raise IOError from exc
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
ZeroDivisionError: division by zero

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 4, in <module>

I verified that this is exactly what 3.4.0 prints for interactive  input, But... the Idle Shell also prints the above, plus it adds (or received from the user process) the offending code lines just as when the code is read from a file.
  File "<pyshell#0>", line 2, in <module>
    1 / 0
ZeroDivisionError: division by zero
  File "<pyshell#0>", line 4, in <module>
    raise IOError from exc

Is there a reason the console interpreter cannot do the same? (Changing this would be a different issue, but one this would depend on.)
msg217115 - (view) Author: PCManticore (Claudiu.Popa) * (Python triager) Date: 2014-04-24 04:58
Python's interactive interpreter doesn't show the offending code lines too. And given the fact that code.InteractiveInterpreter tries to be an emulation of the default interpreter, first the change should be addressed directly there, I think. But I agree that it could be useful.
msg221551 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-06-25 14:36
I would lean toward bug fix.  I'm sure this was just overlooked when exception chaining was added, and as Claudiu says, the contract of code is to emulate the interactive interpreter, so not doing so in this instance looks like a bug to me.

It is certainly conceivable that it could disrupt working programs, but I would think most such would be interactive programs that would benefit from the fix without breaking.  Does anyone know of a counter example or can think of a use case for the code module that this change would be likely to break?

(Note: if there is a use case that somehow parses the output, introducing blank lines and extra traceback clauses could easily be a breaking change...but is there such a use case?)
msg221942 - (view) Author: PCManticore (Claudiu.Popa) * (Python triager) Date: 2014-06-30 06:08
Well, for instance, my use cases with InteractiveInterpreter are for debugging or creating custom interpreters for various apps and in those cases the patch helps, by giving better debugging clues through the exception cause. I agree that this was overlooked when exception chaining was added. Also, idlelib's PyShell is based on InteractiveInterpreter, but in addition, it implements the exception chaining.
msg221943 - (view) Author: PCManticore (Claudiu.Popa) * (Python triager) Date: 2014-06-30 06:12
Also, solving this issue seems to be, partially, a prerequisite for issue14805.
msg226683 - (view) Author: PCManticore (Claudiu.Popa) * (Python triager) Date: 2014-09-10 06:45
If the patch doesn't need anything else, can it be committed?
msg226690 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2014-09-10 12:10
Claudiu, did you see Jim Jewett's review on Rietveld?
msg226691 - (view) Author: PCManticore (Claudiu.Popa) * (Python triager) Date: 2014-09-10 12:27
Actually, no, it seems that I didn't receive any mail regarding them. I'll update the patch accordingly.
msg226694 - (view) Author: PCManticore (Claudiu.Popa) * (Python triager) Date: 2014-09-10 12:44
Here's the new version which fixes the comments from the review.
msg227802 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-09-29 15:26
New changeset 2b212a8186e0 by R David Murray in branch 'default':
#17442: Add chained traceback support to InteractiveInterpreter.
msg227803 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-09-29 15:29
After reconsidering Terry's idle example, it seems to me that the change could adversely impact existing code that already works around the lack of chained tracebacks, even as idle does.  So I committed this to 3.5 only as an enhancement.

Thanks Claudiu.

As an aside, isn't it a (pre-existing) bug that if an excepthook exists, it gets passed None for the traceback?
msg227812 - (view) Author: PCManticore (Claudiu.Popa) * (Python triager) Date: 2014-09-29 17:45
Indeed, it's a preexisting bug. I'll try to come up with a patch shortly.
Date User Action Args
2022-04-11 14:57:42adminsetgithub: 61644
2014-09-29 17:45:51Claudiu.Popasetmessages: + msg227812
2014-09-29 15:29:43r.david.murraysetstatus: open -> closed
versions: - Python 3.4
type: behavior -> enhancement
messages: + msg227803

resolution: fixed
stage: commit review -> resolved
2014-09-29 15:26:05python-devsetnosy: + python-dev
messages: + msg227802
2014-09-10 12:44:51Claudiu.Popasetfiles: + issue17442_2.patch

messages: + msg226694
2014-09-10 12:27:13Claudiu.Popasetmessages: + msg226691
2014-09-10 12:10:29berker.peksagsetnosy: + berker.peksag
messages: + msg226690
2014-09-10 06:46:00Claudiu.Popasetmessages: + msg226683
stage: patch review -> commit review
2014-06-30 06:12:11Claudiu.Popasetmessages: + msg221943
2014-06-30 06:08:16Claudiu.Popasetmessages: + msg221942
2014-06-25 14:36:35r.david.murraysetmessages: + msg221551
2014-06-25 13:20:29Claudiu.Popasetnosy: + r.david.murray
2014-04-24 04:58:41Claudiu.Popasetmessages: + msg217115
2014-04-23 23:45:12terry.reedysetnosy: + terry.reedy
messages: + msg217103
2014-03-19 20:01:56zach.waresetversions: + Python 3.5, - Python 3.3
2014-03-16 18:20:49Claudiu.Popasetfiles: + issue17442_1.patch
2014-03-11 21:34:24Claudiu.Popasetfiles: + issue17442.patch
2013-10-03 19:34:00pitrousetnosy: + georg.brandl, serhiy.storchaka
2013-10-02 21:59:56pitrousetnosy: + pitrou
stage: patch review

versions: - Python 3.2
2013-10-02 13:21:26Claudiu.Popasetmessages: + msg198837
2013-08-20 22:04:11Claudiu.Popasetfiles: + code.patch

nosy: + Claudiu.Popa
messages: + msg195719

keywords: + patch
2013-04-18 18:00:17pjenveysetmessages: + msg187275
2013-03-16 21:17:44pjenveycreate