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

traceback.py: Allow customization of per-frame line formatting in StackSummary #88735

Closed
ammaraskar opened this issue Jul 5, 2021 · 2 comments
Assignees
Labels
3.11 only security fixes stdlib Python modules in the Lib dir

Comments

@ammaraskar
Copy link
Member

BPO 44569
Nosy @terryjreedy, @ammaraskar, @pablogsal, @isidentical
PRs
  • bpo-44569: Decouple frame formatting in traceback.py #27038
  • 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 = 'https://github.com/ammaraskar'
    closed_at = <Date 2021-07-16.12:21:24.792>
    created_at = <Date 2021-07-05.16:47:05.034>
    labels = ['library', '3.11']
    title = 'traceback.py: Allow customization of per-frame line formatting in StackSummary'
    updated_at = <Date 2021-07-16.12:21:24.930>
    user = 'https://github.com/ammaraskar'

    bugs.python.org fields:

    activity = <Date 2021-07-16.12:21:24.930>
    actor = 'pablogsal'
    assignee = 'ammar2'
    closed = True
    closed_date = <Date 2021-07-16.12:21:24.792>
    closer = 'pablogsal'
    components = ['Library (Lib)']
    creation = <Date 2021-07-05.16:47:05.034>
    creator = 'ammar2'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 44569
    keywords = ['patch']
    message_count = 2.0
    messages = ['397009', '397616']
    nosy_count = 4.0
    nosy_names = ['terry.reedy', 'ammar2', 'pablogsal', 'BTaskaya']
    pr_nums = ['27038']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue44569'
    versions = ['Python 3.11']

    @ammaraskar
    Copy link
    Member Author

    During the implementation of PEP-657, Terry Jan Reedy pointed out that in the format method of traceback.StackSummary there are two roles being fulfilled, there is some logic to handle omitting repeated lines involved in recursive calls and then the core per-frame formatting logic:

    cpython/Lib/traceback.py

    Lines 484 to 503 in 17f94e2

    row = []
    row.append(' File "{}", line {}, in {}\n'.format(
    frame.filename, frame.lineno, frame.name))
    if frame.line:
    row.append(' {}\n'.format(frame.line.strip()))
    stripped_characters = len(frame._original_line) - len(frame.line.lstrip())
    if frame.end_lineno == frame.lineno and frame.end_colno != 0:
    colno = _byte_offset_to_character_offset(frame._original_line, frame.colno)
    end_colno = _byte_offset_to_character_offset(frame._original_line, frame.end_colno)
    row.append(' ')
    row.append(' ' * (colno - stripped_characters))
    row.append('^' * (end_colno - colno))
    row.append('\n')
    if frame.locals:
    for name, value in sorted(frame.locals.items()):
    row.append(' {name} = {value}\n'.format(name=name, value=value))
    result.append(''.join(row))

    To allow the per-line formatting to be overridden easily, these lines should be split into a separate method.

    @ammaraskar ammaraskar added the 3.11 only security fixes label Jul 5, 2021
    @ammaraskar ammaraskar self-assigned this Jul 5, 2021
    @ammaraskar ammaraskar added stdlib Python modules in the Lib dir 3.11 only security fixes labels Jul 5, 2021
    @ammaraskar ammaraskar self-assigned this Jul 5, 2021
    @ammaraskar ammaraskar added the stdlib Python modules in the Lib dir label Jul 5, 2021
    @pablogsal
    Copy link
    Member

    New changeset 8ce3008 by Ammar Askar in branch 'main':
    bpo-44569: Decouple frame formatting in traceback.py (GH-27038)
    8ce3008

    @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.11 only security fixes stdlib Python modules in the Lib dir
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants