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

pdb.set_trace() clobbering traceback on error #60686

Closed
akaptur mannequin opened this issue Nov 15, 2012 · 11 comments
Closed

pdb.set_trace() clobbering traceback on error #60686

akaptur mannequin opened this issue Nov 15, 2012 · 11 comments
Labels
3.7 (EOL) end of life 3.8 only security fixes type-bug An unexpected behavior, bug, or error

Comments

@akaptur
Copy link
Mannequin

akaptur mannequin commented Nov 15, 2012

BPO 16482
Nosy @jcea, @atsuoishimoto, @blueyed, @ned-deily, @asvetlov, @asmeurer, @xdegaye, @serhiy-storchaka, @asmeurer, @tirkarthi, @iritkatriel
PRs
  • bpo-24565: f->f_lineno is now -1 when tracing is not set #6233
  • 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 2022-01-18.09:39:06.890>
    created_at = <Date 2012-11-15.17:55:25.536>
    labels = ['3.8', 'type-bug', '3.7']
    title = 'pdb.set_trace() clobbering traceback on error'
    updated_at = <Date 2022-01-18.09:39:06.890>
    user = 'https://bugs.python.org/akaptur'

    bugs.python.org fields:

    activity = <Date 2022-01-18.09:39:06.890>
    actor = 'iritkatriel'
    assignee = 'none'
    closed = True
    closed_date = <Date 2022-01-18.09:39:06.890>
    closer = 'iritkatriel'
    components = []
    creation = <Date 2012-11-15.17:55:25.536>
    creator = 'akaptur'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 16482
    keywords = ['patch']
    message_count = 11.0
    messages = ['175630', '175968', '176271', '186583', '186642', '186643', '234999', '314514', '331064', '331068', '410264']
    nosy_count = 13.0
    nosy_names = ['jcea', 'ishimoto', 'blueyed', 'ned.deily', 'asvetlov', 'Aaron.Meurer', 'xdegaye', 'inglesp', 'serhiy.storchaka', 'akaptur', 'asmeurer', 'xtreak', 'iritkatriel']
    pr_nums = ['6233']
    priority = 'normal'
    resolution = 'out of date'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue16482'
    versions = ['Python 2.7', 'Python 3.6', 'Python 3.7', 'Python 3.8']

    @akaptur
    Copy link
    Mannequin Author

    akaptur mannequin commented Nov 15, 2012

    pdb.set_trace() is overwriting the actual traceback when exiting with an error. See minimal recreation here: https://gist.github.com/4079971

    @akaptur akaptur mannequin added the type-bug An unexpected behavior, bug, or error label Nov 15, 2012
    @xdegaye
    Copy link
    Mannequin

    xdegaye mannequin commented Nov 19, 2012

    The top level frame line number is not updated because it has a local
    trace function while the global trace function is None. This is
    related to bpo-7238.

    The following patch fixes the issue. The patch removes the local trace
    at the top level frame and makes sure it is not reinstalled when
    returning from the current trace function.

    diff --git a/Lib/bdb.py b/Lib/bdb.py
    --- a/Lib/bdb.py
    +++ b/Lib/bdb.py
    @@ -64,6 +64,10 @@
             if self.stop_here(frame) or self.break_here(frame):
                 self.user_line(frame)
                 if self.quitting: raise BdbQuit
    +            # Do not re-install the local trace when we are finished debugging,
    +            # see issues 16482 and 7238.
    +            if not sys.gettrace():
    +                return None
             return self.trace_dispatch
     
         def dispatch_call(self, frame, arg):
    @@ -231,8 +235,10 @@
                 # no breakpoints; run without debugger overhead
                 sys.settrace(None)
                 frame = sys._getframe().f_back
    -            while frame and frame is not self.botframe:
    +            while frame:
                     del frame.f_trace
    +                if frame is self.botframe:
    +                    break
                     frame = frame.f_back
     
         def set_quit(self):

    The following code is a minimum implementation of pdb with the patch
    applied and the associated code to test it.

    class Bdb:
    
        def trace_dispatch(self, frame, event, arg):
            self.set_continue()
            if sys.gettrace():
                return self.trace_dispatch
    
        def set_trace(self, frame):
            self.botframe = frame
            frame.f_trace = self.trace_dispatch
            sys.settrace(self.trace_dispatch)
    
        def set_continue(self):
            sys.settrace(None)
            del self.botframe.f_trace
    
    frame = sys._getframe()
    d = Bdb()
    d.set_trace(frame)
    
    y = "line of code not triggering an error"
    x = 1
    assert x != 1

    @xdegaye
    Copy link
    Mannequin

    xdegaye mannequin commented Nov 24, 2012

    @ned-deily
    Copy link
    Member

    See also bpo-17697. Xavier, would you be willing to submit a patch with a test?

    @xdegaye
    Copy link
    Mannequin

    xdegaye mannequin commented Apr 12, 2013

    The last patch proposed at bpo-17277 and named traced_frame.patch
    fixes also bpo-17697, and issues 7238, 16482. IMHO one should remove
    the assumption in PyFrame_GetLineNumber() that f_lineno is correct
    when f_trace is not NULL, as there does not seem to be a way to remove
    the local trace function of a generator frame. The traced_frame patch
    does that.

    @xdegaye
    Copy link
    Mannequin

    xdegaye mannequin commented Apr 12, 2013

    Forgot to say that traced_frame.patch includes a test.

    @inglesp
    Copy link
    Mannequin

    inglesp mannequin commented Jan 29, 2015

    I've just hit this. Is there anything I can do to help get this fixed?`

    @blueyed
    Copy link
    Mannequin

    blueyed mannequin commented Mar 27, 2018

    Just for reference: #6233 is about fixing this.

    @tirkarthi
    Copy link
    Member

    I too just hit this issue with pdb and checked out the PR locally. It works fine though it has merge conflicts with latest master. I am adding 3.8, 3.7 and 3.6 removing the older versions that don't support bug fixes. Since the PR has unknown repository I suppose @xdegaye has deleted their fork and I don't know if the PR can be revived without opening a new one.

    I have merged the latest master in my own fork resolving conflicts in case a new PR has to be generated : master...tirkarthi:[pr-6233](https://github.com/python/cpython/pull/6233)-latest. Adding Serhiy to the issue. The PR also solves many linked issues in the PR and I thought to add the update here since it seemed appropriate.

    Thanks much @xdegaye for the PR

    @tirkarthi tirkarthi added 3.7 (EOL) end of life 3.8 only security fixes labels Dec 4, 2018
    @asmeurer
    Copy link
    Mannequin

    asmeurer mannequin commented Dec 4, 2018

    You can download the branch for a pull request even if the repo is deleted using this https://stackoverflow.com/a/28622034/161801. That will let you keep the original commits intact.

    @iritkatriel
    Copy link
    Member

    iritkatriel@Irits-MBP cpython % cat pdb_traceback.py

    import pdb
    
    x = 0
    
    while True:
    	pdb.set_trace()
    	y = "line of code not triggering an error"
    	x += 1
    	assert x != 3
    iritkatriel@Irits-MBP cpython % cat pdb_traceback.py
    
    import pdb
    
    x = 0
    
    while True:
    	pdb.set_trace()
    	y = "line of code not triggering an error"
    	x += 1
    	assert x != 3
    iritkatriel@Irits-MBP cpython % ./python.exe pdb_traceback.py
    > /Users/iritkatriel/src/cpython/pdb_traceback.py(8)<module>()
    -> y = "line of code not triggering an error"
    (Pdb) c
    > /Users/iritkatriel/src/cpython/pdb_traceback.py(8)<module>()
    -> y = "line of code not triggering an error"
    (Pdb) c
    > /Users/iritkatriel/src/cpython/pdb_traceback.py(8)<module>()
    -> y = "line of code not triggering an error"
    (Pdb) c
    Traceback (most recent call last):
      File "/Users/iritkatriel/src/cpython/pdb_traceback.py", line 10, in <module>
        assert x != 3
        ^^^^^^^^^^^^^
    AssertionError

    @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 3.8 only security fixes type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants