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

Incomplete traceback with exec on SyntaxError #74681

Closed
stefanseefeld mannequin opened this issue May 28, 2017 · 5 comments
Closed

Incomplete traceback with exec on SyntaxError #74681

stefanseefeld mannequin opened this issue May 28, 2017 · 5 comments
Labels
3.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@stefanseefeld
Copy link
Mannequin

stefanseefeld mannequin commented May 28, 2017

BPO 30496
Nosy @brettcannon, @stefanseefeld, @ncoghlan, @benjaminp, @serhiy-storchaka, @1st1
Files
  • pyerror.tgz
  • 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 2017-05-29.18:46:42.063>
    created_at = <Date 2017-05-28.13:28:54.786>
    labels = ['interpreter-core', 'invalid', 'type-bug', '3.7']
    title = 'Incomplete traceback with `exec` on SyntaxError'
    updated_at = <Date 2017-05-29.18:46:42.062>
    user = 'https://github.com/stefanseefeld'

    bugs.python.org fields:

    activity = <Date 2017-05-29.18:46:42.062>
    actor = 'stefan'
    assignee = 'none'
    closed = True
    closed_date = <Date 2017-05-29.18:46:42.063>
    closer = 'stefan'
    components = ['Interpreter Core']
    creation = <Date 2017-05-28.13:28:54.786>
    creator = 'stefan'
    dependencies = []
    files = ['46908']
    hgrepos = []
    issue_num = 30496
    keywords = []
    message_count = 5.0
    messages = ['294645', '294691', '294699', '294707', '294709']
    nosy_count = 6.0
    nosy_names = ['brett.cannon', 'stefan', 'ncoghlan', 'benjamin.peterson', 'serhiy.storchaka', 'yselivanov']
    pr_nums = []
    priority = 'normal'
    resolution = 'not a bug'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue30496'
    versions = ['Python 2.7', 'Python 3.6', 'Python 3.7']

    @stefanseefeld
    Copy link
    Mannequin Author

    stefanseefeld mannequin commented May 28, 2017

    The following code is supposed to catch and report errors encountered during the execution of a (python) script:

    import traceback
    import sys
    
    try:
        env = {}
        with open('script') as f:
            exec(f.read(), env)
    except:
        type_, value_, tb = sys.exc_info()
        print (traceback.print_tb(tb))
    

    However, depending on the nature of the error, the traceback may contain the location of the error within the executed script file, or it may only report the above exec(f.read(), env) line.

    The attached tarball contains both the above as well as a 'script' that exhibit the problem.

    Is this a bug or am I missing something ? Are there ways to work around this, i.e. determine the correct (inner) location of the error ?

    (I'm observing this with both Python 2.7 and Python 3.5)

    @stefanseefeld stefanseefeld mannequin added the type-bug An unexpected behavior, bug, or error label May 28, 2017
    @pitrou pitrou added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label May 29, 2017
    @pitrou pitrou changed the title Incomplete traceback with exec Incomplete traceback with exec on SyntaxError May 29, 2017
    @pitrou pitrou added the 3.7 (EOL) end of life label May 29, 2017
    @stefanseefeld
    Copy link
    Mannequin Author

    stefanseefeld mannequin commented May 29, 2017

    Some further experiements:

    Replacing the exec(f.read(), env) line by

    code = compile(f.read(), 'script', 'exec')
    exec(code, env)
    

    exhibits the same behaviour. If I remove the try...except, the correct
    (full) traceback is printed out. So it looks like the issue is with the traceback propagation through exception handlers when the error happens during parsing.

    @stefanseefeld
    Copy link
    Mannequin Author

    stefanseefeld mannequin commented May 29, 2017

    Answering my own question:

    It appears I can get the location of a syntax error by inspecting the
    raised SyntaxError, which solves my specific use-case.
    The bug remains, though: The traceback is incomplete if it stems from a syntax error.

    @serhiy-storchaka
    Copy link
    Member

    The traceback contain the location of the code that raised an exception when executed. In case of NameError this is a line in your script "x=u". In case of SyntaxError the code that failed is not in your script (it still is not executed), but in a compiler implicitly called by exec(). The line with exec() is correctly reported.

    The behavior looks correct to me.

    @stefanseefeld
    Copy link
    Mannequin Author

    stefanseefeld mannequin commented May 29, 2017

    OK, fair enough, that makes sense. As I said, in my last message, I was mainly simply trying to figure out the exact location of the error in the executed script, which I got from inspecting the SyntaxError.
    So if all of this is expected behaviour, I think we can close this issue.

    Many thanks for following up.

    @stefanseefeld stefanseefeld mannequin closed this as completed May 29, 2017
    @stefanseefeld stefanseefeld mannequin added the invalid label May 29, 2017
    @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
    3.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants