CLI option parser passes empty option strings, causing startup failure#428
Open
huyhoang171106 wants to merge 1 commit intojoerick:mainfrom
Open
Conversation
…tup failure Signed-off-by: Nguyen Huy Hoang <181364121+huyhoang171106@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Fixes a CLI startup crash caused by invalid optparse option definitions, where empty option strings ("") are passed to OptionParser.add_option(...) and trigger OptionError during parser setup.
Changes:
- Removed empty option-string arguments from several
parser.add_option(...)calls inpyinstrument/__main__.py.
Comments suppressed due to low confidence (2)
pyinstrument/main.py:187
- This fix is incomplete: there are still several later
parser.add_option("", ...)calls (e.g. for--show-regex,--show-all,--unicode/--no-unicode,--color/--no-color,--use-timing-thread).optparsewill still raiseOptionErrorduring parser setup as long as any empty option string remains. Please remove the empty-string option token from those remaining calls as well somain()can start up reliably.
parser.add_option(
"--show",
dest="show_fnmatch",
action="store",
metavar="EXPR",
pyinstrument/main.py:72
- Please add a regression test that invokes
pyinstrument.__main__.main()(or at least builds the OptionParser) to assert it doesn't raiseoptparse.OptionErrorduring option registration. There are existing CLI tests undertest/test_cmdline_main.py, so a small test covering parser construction would prevent this startup-crash regression from returning.
parser.add_option(
"--load-prev",
dest="load_prev",
action="store",
metavar="IDENTIFIER",
help="instead of running a script, load a previous profile session as specified by an identifier",
)
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Several
optparse.OptionParser.add_option(...)calls include""as an option string (e.g.parser.add_option("", "--load-prev", ...),parser.add_option("-m", "", ...)).optparsevalidates every option token and rejects empty strings withOptionErrorduring parser setup, which can crash the CLI before argument parsing begins.Files changed
pyinstrument/__main__.py(modified)Testing