Skip to content

set_trace at top level steps into internal code #17626

Closed
@cpita-mutt

Description

@cpita-mutt

Description

Whenever I try to set a breakpoint at the top level of a cell (either using pdb, pdb or IPython.core.debugger) the breakpoint is set at set_trace(), not my own code:

from IPython.core.debugger import set_trace
# from ipdb import set_trace
# from pdb import set_trace

answer = 42
set_trace()
print(answer)
> /tmp/ipykernel_33648/20523541.py(4)<module>()
      1 from pdb import set_trace
      2 
      3 answer = 42
----> 4 set_trace(frame=None)
      5 answer += x

If I press n at this point I get:

> /home/carlos/.venvs/base/lib/python3.12/site-packages/IPython/core/interactiveshell.py(3675)run_code()
   3673             finally:
   3674                 # Reset our crash handler in place
-> 3675                 sys.excepthook = old_excepthook
   3676         except SystemExit as e:
   3677             if result is not None:

Putting the code inside a function fixes the problem:

from IPython.core.debugger import set_trace
# from ipdb import set_trace
# from pdb import set_trace

def f():
    answer = 42
    set_trace()
    print(answer)

f()
> /tmp/ipykernel_33648/1216921156.py(8)f()
      6     answer = 42
      7     set_trace()
----> 8     print(answer)
      9 
     10 f()

I know I can use the builtin visual debugger but I'm used to set_trace() and I prefer it sometimes.

Reproduce

Just run the code above in a notebook.

Expected behavior

set_trace() at the top-level behaves the same than inside a function.

Context

  • Operating System and version: Ubuntu 20.04.1 (server), Windows 11 (client)
  • Browser and version: Chrome 136.0.7103.116
  • JupyterLab version:
IPython          : 7.34.0
ipykernel        : 6.29.5
ipywidgets       : 8.1.7
jupyter_client   : 8.6.3
jupyter_core     : 5.8.1
jupyter_server   : 2.16.0
jupyterlab       : 4.4.3
nbclient         : 0.10.2
nbconvert        : 7.16.6
nbformat         : 5.10.4
notebook         : 7.0.7
qtconsole        : not installed
traitlets        : 5.14.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions