Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

code.InteractiveInterpreter doesn't display the exception cause #61644

Closed
pjenvey opened this issue Mar 16, 2013 · 16 comments
Closed

code.InteractiveInterpreter doesn't display the exception cause #61644

pjenvey opened this issue Mar 16, 2013 · 16 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@pjenvey
Copy link
Member

pjenvey commented Mar 16, 2013

BPO 17442
Nosy @birkenfeld, @terryjreedy, @pitrou, @pjenvey, @bitdancer, @PCManticore, @berkerpeksag, @serhiy-storchaka
Files
  • code.patch
  • issue17442.patch: Remove trailing whitespaces
  • issue17442_1.patch: Added documentation note regarding this change.
  • issue17442_2.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2014-09-29.15:29:43.080>
    created_at = <Date 2013-03-16.21:17:44.889>
    labels = ['type-feature', 'library']
    title = "code.InteractiveInterpreter doesn't display the exception cause"
    updated_at = <Date 2014-09-29.17:45:51.417>
    user = 'https://github.com/pjenvey'

    bugs.python.org fields:

    activity = <Date 2014-09-29.17:45:51.417>
    actor = 'Claudiu.Popa'
    assignee = 'none'
    closed = True
    closed_date = <Date 2014-09-29.15:29:43.080>
    closer = 'r.david.murray'
    components = ['Library (Lib)']
    creation = <Date 2013-03-16.21:17:44.889>
    creator = 'pjenvey'
    dependencies = []
    files = ['31388', '34361', '34443', '36592']
    hgrepos = []
    issue_num = 17442
    keywords = ['patch']
    message_count = 16.0
    messages = ['184355', '187275', '195719', '198837', '217103', '217115', '221551', '221942', '221943', '226683', '226690', '226691', '226694', '227802', '227803', '227812']
    nosy_count = 9.0
    nosy_names = ['georg.brandl', 'terry.reedy', 'pitrou', 'pjenvey', 'r.david.murray', 'Claudiu.Popa', 'python-dev', 'berker.peksag', 'serhiy.storchaka']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue17442'
    versions = ['Python 3.5']

    @pjenvey
    Copy link
    Member Author

    pjenvey commented Mar 16, 2013

    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)

    @pjenvey pjenvey added type-bug An unexpected behavior, bug, or error stdlib Python modules in the Lib dir labels Mar 16, 2013
    @pjenvey
    Copy link
    Member Author

    pjenvey commented Apr 18, 2013

    PyPy's fixed this here:

    https://bitbucket.org/pypy/pypy/commits/1341a432e134

    The tests just need to be adapted to the stdlib test suite

    @PCManticore
    Copy link
    Mannequin

    PCManticore mannequin commented Aug 20, 2013

    Here's an updated patch for the stdlib, with adapted tests.

    @PCManticore
    Copy link
    Mannequin

    PCManticore mannequin commented Oct 2, 2013

    Could anyone review this patch, please?

    @terryjreedy
    Copy link
    Member

    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>
    OSError

    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>
    OSError

    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
    OSError

    Is there a reason the console interpreter cannot do the same? (Changing this would be a different issue, but one this would depend on.)

    @PCManticore
    Copy link
    Mannequin

    PCManticore mannequin commented Apr 24, 2014

    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.

    @bitdancer
    Copy link
    Member

    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?)

    @PCManticore
    Copy link
    Mannequin

    PCManticore mannequin commented Jun 30, 2014

    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.

    @PCManticore
    Copy link
    Mannequin

    PCManticore mannequin commented Jun 30, 2014

    Also, solving this issue seems to be, partially, a prerequisite for bpo-14805.

    @PCManticore
    Copy link
    Mannequin

    PCManticore mannequin commented Sep 10, 2014

    If the patch doesn't need anything else, can it be committed?

    @berkerpeksag
    Copy link
    Member

    Claudiu, did you see Jim Jewett's review on Rietveld?

    @PCManticore
    Copy link
    Mannequin

    PCManticore mannequin commented Sep 10, 2014

    Actually, no, it seems that I didn't receive any mail regarding them. I'll update the patch accordingly.

    @PCManticore
    Copy link
    Mannequin

    PCManticore mannequin commented Sep 10, 2014

    Here's the new version which fixes the comments from the review.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Sep 29, 2014

    New changeset 2b212a8186e0 by R David Murray in branch 'default':
    bpo-17442: Add chained traceback support to InteractiveInterpreter.
    https://hg.python.org/cpython/rev/2b212a8186e0

    @bitdancer
    Copy link
    Member

    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?

    @bitdancer bitdancer added type-feature A feature request or enhancement and removed type-bug An unexpected behavior, bug, or error labels Sep 29, 2014
    @PCManticore
    Copy link
    Mannequin

    PCManticore mannequin commented Sep 29, 2014

    Indeed, it's a preexisting bug. I'll try to come up with a patch shortly.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants