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

sh hangs on an explicitly empty _in #427

Closed
jorisroovers opened this issue Apr 6, 2018 · 1 comment
Closed

sh hangs on an explicitly empty _in #427

jorisroovers opened this issue Apr 6, 2018 · 1 comment
Assignees

Comments

@jorisroovers
Copy link

Hi!

Thanks for providing sh, I'm a happy user of it in my own little hobby project (gitlint).

I've ran into the following issue.

When trying to pass explicitely empty stdin input like so:

from sh import cat

cat(_in="")
# or
cat(_in=None)

Then sh will hang (I suspect it's waiting for input).

I understand you can work around this like so:

from sh import cat

myvar="..." # result of fancy business logic
if myvar is None or myvar == "":
    cat()
else:
    cat(_in=myvar)

But I'd argue that sh should be able to deal with this natively, since I'd think it's quite common to do some other business logic that results in something passed into sh via _in. That input might be empty.

In addition, if the application that you're calling actually checks whether or not it's receiving data on stdin (e.g. len(stdin.read())==0), then you can't invoke this behavior with sh without using a work-around like so:

from sh import cat

class MockStdin():
    def read(self, size):
        # ignore size, just return empty stdin data
        return
cat(_in=MockStdin())

I've done a little bit of digging on how to solve this in sh, and I think the issue starts here:
https://github.com/amoffat/sh/blob/master/sh.py#L2023

since self.stdin = stdin or Queue() will always result in self.stdin=Queue() if stdin is None or an empty string.

However, just adding an extra if statement to deal with this case isn't enough to fix this - and I'm not familiar enough with the sh codebase to know what the best approach to this would be.

Interested to hear your thoughts :-)

@amoffat
Copy link
Owner

amoffat commented Apr 7, 2018

Makes sense 👍

@amoffat amoffat self-assigned this Apr 25, 2020
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

No branches or pull requests

2 participants