shaheeramjad opened a new pull request, #37536:
URL: https://github.com/apache/beam/pull/37536

   ### 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 in 
`sdks/python/apache_beam/utils/processes.py` raised **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], ...)`). For `pip 
install <pkg>` the command list has only 5 elements (indices 0–4), so 
`args[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 when 
`args` is a tuple, `args[0]` is a list/tuple, and `len(args[0]) > 6`; otherwise 
returns `"see output below"`.
   
   - **Guarded the pip branch**  
     Replaced `(args[0][2] == "pip")` with `len(args[0]) > 2 and args[0][2] == 
"pip"` so we never index `args[0][2]` on a short list.
   
   - **Updated `call`, `check_call`, and `check_output`**  
     All three now use `_pip_package_from_args(args)` instead of `args[0][6]` 
when building the pip error message.
   
   ### Files changed
   
   - **`sdks/python/apache_beam/utils/processes.py`**
     - New helper `_pip_package_from_args(args)` (with docstring).
     - Pip branch in `call`, `check_call`, and `check_output`: length check + 
use of helper for package string.
   
   - **`sdks/python/apache_beam/utils/processes_test.py`**
     - 
`TestErrorHandlingCheckCall.test_check_call_pip_short_command_no_index_error`
     - 
`TestErrorHandlingCheckOutput.test_check_output_pip_short_command_no_index_error`
     - `TestErrorHandlingCall.test_call_pip_short_command_no_index_error`  
     Each test uses a short pip command (`['python', '-m', 'pip', 'install', 
'nonexistent-package-xyz']`), triggers `CalledProcessError`, and asserts that a 
**RuntimeError** is raised (not IndexError) and that the message contains the 
subprocess output and `"see output below"`.
   
   ### Testing
   
   - All **12** tests in `apache_beam.utils.processes_test` pass, including the 
3 new tests and the existing ones (e.g. long pip command with package at index 
6).
   - Run: `python -m unittest apache_beam.utils.processes_test -v`
   
   ### Backward compatibility
   
   - Behavior is unchanged for commands with 7+ elements (e.g. existing `pip 
download --dest /var <pkg> ...` still shows the package name at index 6).
   - Only the previously broken case (short command) is fixed; no API or 
contract changes.
   
   ## Related
   
   - Fixes: `Fixes #37515`
   - Component: Python SDK
   
   ------------------------
   
   Thank you for your contribution! Follow this checklist to help us 
incorporate your contribution quickly and easily:
   
    - [x] Mention the appropriate issue in your description (for example: 
`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, comment `fixes #<ISSUE NUMBER>` instead.
    - [x] Update `CHANGES.md` with noteworthy changes.
    - [ ] If this contribution is large, please file an Apache [Individual 
Contributor License Agreement](https://www.apache.org/licenses/icla.pdf).
   
   See the [Contributor Guide](https://beam.apache.org/contribute) for more 
tips on [how to make review process 
smoother](https://github.com/apache/beam/blob/master/CONTRIBUTING.md#make-the-reviewers-job-easier).
   
   To check the build health, please visit 
[https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md](https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md)
   
   GitHub Actions Tests Status (on master branch)
   
------------------------------------------------------------------------------------------------
   [![Build python source distribution and 
wheels](https://github.com/apache/beam/actions/workflows/build_wheels.yml/badge.svg?event=schedule&&?branch=master)](https://github.com/apache/beam/actions?query=workflow%3A%22Build+python+source+distribution+and+wheels%22+branch%3Amaster+event%3Aschedule)
   [![Python 
tests](https://github.com/apache/beam/actions/workflows/python_tests.yml/badge.svg?event=schedule&&?branch=master)](https://github.com/apache/beam/actions?query=workflow%3A%22Python+Tests%22+branch%3Amaster+event%3Aschedule)
   [![Java 
tests](https://github.com/apache/beam/actions/workflows/java_tests.yml/badge.svg?event=schedule&&?branch=master)](https://github.com/apache/beam/actions?query=workflow%3A%22Java+Tests%22+branch%3Amaster+event%3Aschedule)
   [![Go 
tests](https://github.com/apache/beam/actions/workflows/go_tests.yml/badge.svg?event=schedule&&?branch=master)](https://github.com/apache/beam/actions?query=workflow%3A%22Go+tests%22+branch%3Amaster+event%3Aschedule)
   
   See [CI.md](https://github.com/apache/beam/blob/master/CI.md) for more 
information about GitHub Actions CI or the [workflows 
README](https://github.com/apache/beam/blob/master/.github/workflows/README.md) 
to see a list of phrases to trigger workflows.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to