Skip to content

traceback.clear_frames() doesn't clear *all* frames #75502

Closed

Description

@vstinner
BPO 31321
Nosy @vstinner
PRs
  • bpo-31321: Fix traceback.clear_frames() #3262
  • Files
  • clear_frames_bug.py
  • 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 2018-09-19.23:09:12.715>
    created_at = <Date 2017-09-01.14:50:17.380>
    labels = ['3.7']
    title = "traceback.clear_frames() doesn't clear *all* frames"
    updated_at = <Date 2018-09-19.23:09:12.714>
    user = 'https://github.com/vstinner'

    bugs.python.org fields:

    activity = <Date 2018-09-19.23:09:12.714>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-09-19.23:09:12.715>
    closer = 'vstinner'
    components = []
    creation = <Date 2017-09-01.14:50:17.380>
    creator = 'vstinner'
    dependencies = []
    files = ['47116']
    hgrepos = []
    issue_num = 31321
    keywords = []
    message_count = 4.0
    messages = ['301111', '301113', '301114', '301117']
    nosy_count = 1.0
    nosy_names = ['vstinner']
    pr_nums = ['3262']
    priority = 'normal'
    resolution = 'out of date'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue31321'
    versions = ['Python 3.6', 'Python 3.7']

    Activity

    1. vstinner commented on Sep 1, 2017

      @vstinner
      MemberAuthor

      traceback.clear_frames():
      "Clears the local variables of all the stack frames in a traceback tb by calling the clear() method of each frame object."
      https://docs.python.org/dev/library/traceback.html#traceback.clear_frames

      That's wrong: it only calls frame.clear() on the top frame of each traceback object.

      Output of attached clear_frames_bug.py script:
      ---
      locals:
      frame locals {'z': 3}
      frame locals {'y': 2}
      frame locals {'x': 1}

      clear_frames()

      locals:
      frame locals {}
      frame locals {'y': 2}
      frame locals {'x': 1}

      ---

      As you can see, only the locals of the func3() frame, where the exception was raised, is clear. Locals of other frames are not cleared.

      Attached PR fixes the issue.

      Output with the fix:
      ---
      locals:
      frame locals {'z': 3}
      frame locals {'y': 2}
      frame locals {'x': 1}

      clear_frames()

      locals:
      frame locals {}
      frame locals {}
      frame locals {}

      ---

    2. vstinner commented on Sep 1, 2017

      @vstinner
      MemberAuthor

      Would it be ok to backport such change to Python 3.6? It's a bugfix, no?

    3. vstinner commented on Sep 1, 2017

      @vstinner
      MemberAuthor

      traceback.clear_frames() was added by bpo-1565525: commit 173a157.

    4. vstinner commented on Sep 1, 2017

      @vstinner
      MemberAuthor

      See bpo-31323 where I wanted to use traceback.clear_frames() to manually break a reference cycle.

    5. transferred this issue fromon Apr 10, 2022
    6. clchiou commented on Sep 29, 2022

      @clchiou

      Hi, I stumbled upon this bug, and gave it a look.

      The current implementation of clear_frames traverses the tb_next linked list. In effect, it clears frames from the frame where the exception is caught to the top of the stack (the top half of the stack).

      But it does not clear the bottom half of the stack, which is what this issue is about. To clear the bottom half, I think what clear_frames needs to do is to traverse the f_back linked list from the exception-catching frame, which goes all the way to to bottom of the stack.

      Basically the new implementation of clear_frames would look like this:

      def clear_frames(tb):
          if tb is None:
              return
          frame = tb.tb_frame
          while frame is not None:
              try:
                  frame.clear()
              except RuntimeError:
                  pass
              frame = frame.f_back
          tb = tb.tb_next
          while tb is not None:
              try:
                  tb.tb_frame.clear()
              except RuntimeError:
                  pass
              tb = tb.tb_next
      

      If you think this change is a good starting point, I will create a PR for review. What do you think? @vstinner @pitrou

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions