Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle StopIteration in iterable_subprocess #562

Conversation

christian-monch
Copy link
Contributor

@christian-monch christian-monch commented Dec 8, 2023

[This has been put into draft-mode in favor of an alternative approach to make return codes available in case of exceptions in PR #565]

This PR implements the result of this discussion in PR #546

It modifies iterable_subprocess and therefore iter_subproc to prioritize IterableSubprocessError-exceptions over StopIteration-exception. The rationale for that is, that StopIteration and a non-zero exit value of a subprocess indicate that there was a short-read due to an error in the subprocess.

Because we require access to the return code of the subprocess, prioritizing IterableSubprocessError-exceptions over StopIteration-exceptions alleviates the need to catch StopIteration in the iter_subproc-context.

This commit modifies `iterable_subprocess` so that
it prioritizes `IterableSubprocessError` exceptions
over `StopIteration` exceptions, if a `StopIteration`
exception is raised in the body of an
`iterable_subprocess`-context and if the sub process
exits with a non-zero return code.

`StopIteration` in conjunction with a non-zero
return code indicates an unexpected short read
due to an early "error"-exit of the sub process.

This commit is a result of this discussion:
datalad#546 (comment)
This commit adds tests that verify the
intendend behavior of `iterable_subprocess`
if a `StopIteration`-exception is raised
in the body of an
`iterable_subprocess`-context.
This commit ensures that the `LANG` environment
variable is set to `C` in a tests that interprets
the output of `ls`.
This commit describes the handling of exceptions
in the docstring of `iter_subproc`. It highlights
the difference between `StopIteration` and other
exceptions, in connection with a non-zero subprocess
return code.
@christian-monch christian-monch requested a review from mih as a code owner December 8, 2023 13:52
@christian-monch
Copy link
Contributor Author

christian-monch commented Dec 8, 2023

I am wondering whether it would be better to change iterable_subprocess into a class that implements the context-manager interface?

The instances of this class could then store the return code of the subprocess and it would still be available to us, independent from the exceptions that were raised.

Converting this to Draft for now

@christian-monch christian-monch marked this pull request as draft December 8, 2023 13:56
Copy link

codecov bot commented Dec 8, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (bd59fce) 92.87% compared to head (5a05173) 92.89%.
Report is 8 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #562      +/-   ##
==========================================
+ Coverage   92.87%   92.89%   +0.01%     
==========================================
  Files         143      143              
  Lines       10365    10393      +28     
  Branches     1141     1147       +6     
==========================================
+ Hits         9627     9655      +28     
  Misses        714      714              
  Partials       24       24              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@christian-monch
Copy link
Contributor Author

Ok, I will create another PR that turns the as-variable into a generator that can hold the return ode

@mih
Copy link
Member

mih commented Dec 13, 2023

With #565 merged, we can close this one.

@mih mih closed this Dec 13, 2023
@christian-monch christian-monch deleted the handle-stop-iteration-in-iterable-subprocess branch July 16, 2024 10:12
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.

2 participants