Skip to content

Add a commands argument to pdb.set_trace so the user can feed commands from source code #120254

@gaogaotiantian

Description

@gaogaotiantian

Feature or enhancement

Proposal:

I propose to add a commands argument to pdb.set_trace and pdb.Pdb.set_trace. The commands fed should be a list of pdb commands that will be executed the first time the debugger is brought up - it's very similar to .pdbrc.

import pdb
def f():
    x = 1
    # should print 1 and continue
    pdb.set_trace(commands=['p x', 'c'])
f()

There are many use cases

  1. Some users use .pdbrc as debugging scripts, and sometimes it's not the best way because that would be the same for the whole directory. Being able to feed pdb commands can save user a lot of time re-typing all the commands after they try some fix.
  2. It would be much easier to serve a repro regarding pdb on a silver platter. When user reports bugs about pdb, they had to give both the source code, and the commands they use to trigger the bug. With this command, the user can provide a simple one-piece script for the minimal repro.
  3. For cases where stdin is not available, or not super convenient, this could help. For example, in testing. Also, I talked with pyodide maintainer about supporting pdb on webbrowser, and one of the issue was the lack of stdin. They successfully made it work with some workaround, but this could help the case.
  4. It's useful for remote help. It's not uncommon that a more senior developer needs to help a newbee remotely. They could send a fully ready piece of code that contains the command and ask the person to simply put it in and get the result.

This feature is also trivial to implement and easy to maintain - all the pieces are already there.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions