Skip to content

Guard against empty sys.argv in _get_debugger_cls - #298

Open
M-Colley wants to merge 1 commit into
gotcha:masterfrom
M-Colley:fix/issue-278-empty-sys-argv
Open

M-Colley wants to merge 1 commit into
gotcha:masterfrom
M-Colley:fix/issue-278-empty-sys-argv

Conversation

@M-Colley

@M-Colley M-Colley commented Nov 28, 2026 •

Copy link
Copy Markdown

Calling set_trace() when sys.argv is empty raises IndexError inside argparse.ArgumentParser. Specifically, _get_debugger_cls() calls TerminalIPythonApp.initialize(["--no-term-title"]), which reads sys.argv[0] by default. Fixes #278.

To prevent this crash, _get_debugger_cls() sets sys.argv to [""] during ipapp.initialize(["--no-term-title"]) when sys.argv is empty and restores the original list in a finally block. In addition, DebuggerClsTest.test_empty_sys_argv in tests/test_opts.py verifies that _get_debugger_cls() succeeds and preserves sys.argv.

Verified with pytest tests -o addopts= -W default -v (20 passed, 2 skipped).

Summary by CodeRabbit

  • Bug Fixes
    • Fixed a startup issue that could prevent the debugger from initializing when launched without command-line arguments. The argument list remains unchanged after initialization, including when startup encounters an error.

Fixes gotcha#278

Signed-off-by: Mark Colley <39366189+M-Colley@users.noreply.github.com>
@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

When sys.argv is empty, _get_debugger_cls temporarily supplies an argument during terminal app initialization and restores the original list. A test checks the returned class and confirms the list remains empty.

Changes: Debugger setup

Layer / File(s) Summary
Empty argument handling and test
ipdb/__main__.py, tests/test_opts.py
_get_debugger_cls sets sys.argv to [""] during initialization when the list is empty, then restores it in a finally block. The test checks that the function returns a class and leaves sys.argv empty.

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: adamchainz, mrmino

Merge Risk: Minimal · up to 6c6a8

This change prevents set_trace() from raising IndexError when sys.argv is empty and leaves other behavior unchanged. The fix is small and has a test, so merge risk is minimal.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the change: a guard against empty sys.argv in _get_debugger_cls. The raw summary confirms that _get_debugger_cls temporarily sets sys.argv to [""] during TerminalIPythonApp.initialize(…
Linked Issues check Passed [#278] Issue #278 reports an IndexError raised by argparse.ArgumentParser when set_trace() runs, from sys.argv[0] access in _get_debugger_cls() -> TerminalIPythonApp.initialize(["--no-term-title"]). a…
Out of Scope Changes check Passed The changes touch only ipdb/main.py (the guard in _get_debugger_cls) and tests/test_opts.py (the import of _get_debugger_cls and the new test). The guard directly implements the linked issue #278 …
Full details: Title check

Explanation

The title names the change: a guard against empty sys.argv in _get_debugger_cls. The raw summary confirms that _get_debugger_cls temporarily sets sys.argv to [""] during TerminalIPythonApp.initialize() and restores it in a finally block, and that a test covers the empty-argv case. The title is specific and matches the main change. It does not mention the user-facing symptom (IndexError in set_trace()), which is acceptable because the title need not cover every detail.

Full details: Linked Issues check

Explanation

[#278] Issue #278 reports an IndexError raised by argparse.ArgumentParser when set_trace() runs, from sys.argv[0] access in _get_debugger_cls() -> TerminalIPythonApp.initialize(["--no-term-title"]). argparse reads sys.argv[0] only when sys.argv is empty, so the reported trace implies an empty sys.argv. At the reviewed head, ipdb/main.py _get_debugger_cls() sets sys.argv to [""] when sys.argv is empty, calls ipapp.initialize(["--no-term-title"]) inside try, and restores the saved list in finally. This removes the reported failure path. The non-IPython branch is otherwise unchanged. The added DebuggerClsTest.test_empty_sys_argv in tests/test_opts.py covers the empty-argv case, per the PR description and the AI-generated summary. I did not read the test file directly because the diff inspection was unavailable; this test evidence relies on the summary. No other coding requirement appears in the issue.

Full details: Out of Scope Changes check

Explanation

The changes touch only ipdb/main.py (the guard in _get_debugger_cls) and tests/test_opts.py (the import of _get_debugger_cls and the new test). The guard directly implements the linked issue #278 requirement. The test verifies that guard. No unrelated functions or modules changed according to the summary. The public API is unchanged.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 7dcfa9be-fc28-4942-ba27-105ef5534c5b
Commits

Reviewing files that changed from the base of the PR and between be12fe3 and 6c6a8ca.

Files selected for processing: 2

  • ipdb/__main__.py
  • tests/test_opts.py

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

set_trace fails with IndexError

1 participant