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

Use subprocess.run with encoding="utf-8" #32

Open
hughdbrown opened this issue Apr 18, 2023 · 0 comments
Open

Use subprocess.run with encoding="utf-8" #32

hughdbrown opened this issue Apr 18, 2023 · 0 comments

Comments

@hughdbrown
Copy link

wolverine/wolverine.py

Lines 36 to 40 in 2f5a026

try:
result = subprocess.check_output(subprocess_args, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
return e.output.decode("utf-8"), e.returncode
return result.decode("utf-8"), 0

if you pass encoding='utf-8' to subprocess.run, your strings are automatically decoded:

>>> s = subprocess.run("/bin/echo 'hello'".split(" "), stdout=subprocess.PIPE)
>>> s.stdout
b"'hello'\n"
>>> s = subprocess.run("/bin/echo 'hello'".split(" "), stdout=subprocess.PIPE, encoding="utf-8")
>>> s.stdout
"'hello'\n"

So:

    try:
        result = subprocess.check_output(subprocess_args, stderr=subprocess.STDOUT, encoding="utf-8")
    except subprocess.CalledProcessError as e:
        return e.output, e.returncode
    return result, 0
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

1 participant