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

Execution context (default arguments) for custom command? #559

Closed
jerkstorecaller opened this issue Mar 3, 2021 · 6 comments · Fixed by #573
Closed

Execution context (default arguments) for custom command? #559

jerkstorecaller opened this issue Mar 3, 2021 · 6 comments · Fixed by #573
Labels

Comments

@jerkstorecaller
Copy link

I want sh to output to always output stdout/stderr as if I'd run the commands in a bash script, for every single command. Using the doc at https://amoffat.github.io/sh/sections/default_arguments.html as guide, I created a new sh2 object with my default argument for _out. However, it doesn't work for custom Commands, only for built-in commands.

import sh
import sys

sh2 = sh(_out=sys.stdout, _err=sys.stderr)

#correctly prints the output of "ls /etc" to stdout
sh2.ls("/etc")

subgit = sh2.Command("tools/subgit-3.3.11/bin/subgit")
#prints nothing
subgit("--version")
#prints the version correctly
subgit("--version", _out=sys.stdout)

It's not specific to this tool, it's the same result if I use myls = sh2.Command("/usr/bin/ls") .

@amoffat
Copy link
Owner

amoffat commented Mar 3, 2021

Huh, I think you have found a bug, and I think it is reasonable to expect that Command instances should inherit the default arguments from sh2. Thanks for reporting

@amoffat amoffat added the bug label Mar 3, 2021
@ecederstrand
Copy link
Collaborator

I can't reproduce this on my own machine.

@amoffat The beginning of SelfWrapper.__call__ looks very innocent. You've added a warning about some fragile code in

sh/sh.py

Line 3527 in b6cb96e

# inspect the line in the parent frame that calls and assigns the new sh
and forward. I assume this is where things are going wrong. Do you have any ideas how to improve?

@amoffat
Copy link
Owner

amoffat commented May 31, 2021

@ecederstrand here is what I am using to reproduce:

import sh

sh2 = sh(_ok_code=1)

sh2.ls() # raises
ls = sh2.Command("ls")
ls() # doesn't raise

It seems that the arguments that should be "baked" into sh2 aren't propagating to instances of the Command class

@ecederstrand
Copy link
Collaborator

I created a minimal PR for this. The caveat is that sh.Command("ls") now also gets the _ok_code=1 call arg. That's one step forward and one step back, IMHO. I can't see how to fix that without also creating wrapper code for Command. Any ides?

@ecederstrand
Copy link
Collaborator

I've updated the PR without the above caveat and added a test case. Not entirely happy with the code - it seems there should be a less complicated way. Suggestions welcome :-)

@amoffat
Copy link
Owner

amoffat commented Jun 6, 2021

LGTM @ecederstrand. Scoping the baked args to the environment-specific Command class is exactly how I would have tried to do it. Good work 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants