Fix IndexError in apache_beam.utils.processes when pip subprocess fails with short command#37536
Fix IndexError in apache_beam.utils.processes when pip subprocess fails with short command#37536shaheeramjad wants to merge 3 commits intoapache:masterfrom
apache_beam.utils.processes when pip subprocess fails with short command#37536Conversation
…ommand - Add _pip_package_from_args() to safely get package string (avoids args[0][6] on short lists) - Guard pip branch with len(args[0]) > 2 before indexing - Use helper in call, check_call, and check_output - Add tests for short pip command path (no IndexError)
Summary of ChangesHello @shaheeramjad, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly improves the error handling within Apache Beam's Python SDK by addressing a critical Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
|
Hi @damccorm Can you review it |
|
Checks are failing. Will not request review until checks are succeeding. If you'd like to override that behavior, comment |
|
assign set of reviewers |
|
Assigning reviewers: R: @shunping for label python. Note: If you would like to opt out of this review, comment Available commands:
The PR bot will only process comments in the main thread (not review comments). |
Problem
When a pip subprocess fails and the command is passed as a short list (e.g.
['python', '-m', 'pip', 'install', 'pkg']), the exception handler insdks/python/apache_beam/utils/processes.pyraised IndexError instead of the intended RuntimeError with traceback and pip output.The pip-specific branch used a hardcoded index 6 for the "package name" when formatting the error message (
.format(..., args[0][6], ...)). Forpip install <pkg>the command list has only 5 elements (indices 0–4), soargs[0][6]caused IndexError and users saw a crash instead of a clear error message.Solution
Added
_pip_package_from_args(args)Returns a safe string for the "package" field: uses
args[0][6]only whenargsis a tuple,args[0]is a list/tuple, andlen(args[0]) > 6; otherwise returns"see output below".Guarded the pip branch
Replaced
(args[0][2] == "pip")withlen(args[0]) > 2 and args[0][2] == "pip"so we never indexargs[0][2]on a short list.Updated
call,check_call, andcheck_outputAll three now use
_pip_package_from_args(args)instead ofargs[0][6]when building the pip error message.Files changed
sdks/python/apache_beam/utils/processes.py_pip_package_from_args(args)(with docstring).call,check_call, andcheck_output: length check + use of helper for package string.sdks/python/apache_beam/utils/processes_test.pyTestErrorHandlingCheckCall.test_check_call_pip_short_command_no_index_errorTestErrorHandlingCheckOutput.test_check_output_pip_short_command_no_index_errorTestErrorHandlingCall.test_call_pip_short_command_no_index_errorEach test uses a short pip command (
['python', '-m', 'pip', 'install', 'nonexistent-package-xyz']), triggersCalledProcessError, and asserts that a RuntimeError is raised (not IndexError) and that the message contains the subprocess output and"see output below".Testing
apache_beam.utils.processes_testpass, including the 3 new tests and the existing ones (e.g. long pip command with package at index 6).python -m unittest apache_beam.utils.processes_test -vBackward compatibility
pip download --dest /var <pkg> ...still shows the package name at index 6).Related
Fixes #37515Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
addresses #123), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, commentfixes #<ISSUE NUMBER>instead.CHANGES.mdwith noteworthy changes.See the Contributor Guide for more tips on how to make review process smoother.
To check the build health, please visit https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md
GitHub Actions Tests Status (on master branch)
See CI.md for more information about GitHub Actions CI or the workflows README to see a list of phrases to trigger workflows.