[elbe-devel] [PATCH v2 08/27] shellhelper: allow stdin options to be string

Bastian Germann bage at linutronix.de
Thu Sep 24 17:04:29 CEST 2020


Am 24.09.20 um 16:56 schrieb Torben Hohn:
> subprocess.communicate() requires stdin to be bytes.
> 
> Allow for str, by conditionally calling encode().
> 
> Also add doctests for the str case.
> 
> Signed-off-by: Torben Hohn <torben.hohn at linutronix.de>

Reviewed-by: Bastian Germann <bage at linutronix.de>

> ---
>  elbepack/shellhelper.py | 21 +++++++++++++++++++++
>  1 file changed, 21 insertions(+)
> 
> diff --git a/elbepack/shellhelper.py b/elbepack/shellhelper.py
> index 21f234db7..83245886b 100644
> --- a/elbepack/shellhelper.py
> +++ b/elbepack/shellhelper.py
> @@ -76,6 +76,9 @@ def command_out(cmd, stdin=None, output=PIPE, env_add=None):
>      >>> command_out("2>&1 cat -", stdin=b"ELBE")
>      (0, 'ELBE')
>  
> +    >>> command_out("2>&1 cat -", stdin="ELBE")
> +    (0, 'ELBE')
> +
>      >>> command_out("false")
>      (1, '')
>  
> @@ -84,6 +87,9 @@ def command_out(cmd, stdin=None, output=PIPE, env_add=None):
>      if env_add:
>          new_env.update(env_add)
>  
> +    if type(stdin) == str:
> +        stdin = stdin.encode()
> +
>      if stdin is None:
>          p = Popen(cmd, shell=True,
>                    stdout=output, stderr=STDOUT, env=new_env)
> @@ -137,6 +143,9 @@ def command_out_stderr(cmd, stdin=None, env_add=None):
>      >>> command_out_stderr("1>&2 cat - && false", stdin=b"ELBE")
>      (1, '', 'ELBE')
>  
> +    >>> command_out_stderr("1>&2 cat - && false", stdin="ELBE")
> +    (1, '', 'ELBE')
> +
>      >>> command_out_stderr("true")
>      (0, '', '')
>  
> @@ -145,6 +154,9 @@ def command_out_stderr(cmd, stdin=None, env_add=None):
>      if env_add:
>          new_env.update(env_add)
>  
> +    if type(stdin) == str:
> +        stdin = stdin.encode()
> +
>      if stdin is None:
>          p = Popen(cmd, shell=True,
>                    stdout=PIPE, stderr=PIPE, env=new_env)
> @@ -178,6 +190,9 @@ def system_out_stderr(cmd, stdin=None, allow_fail=False, env_add=None):
>  
>      >>> system_out_stderr("1>&2 cat -", allow_fail=True, stdin=b"ELBE")
>      ('', 'ELBE')
> +
> +    >>> system_out_stderr("1>&2 cat -", allow_fail=True, stdin="ELBE")
> +    ('', 'ELBE')
>      """
>      code, out, err = command_out_stderr(cmd, stdin, env_add)
>  
> @@ -224,6 +239,9 @@ def do(cmd, allow_fail=False, stdin=None, env_add=None):
>      if env_add:
>          new_env.update(env_add)
>  
> +    if type(stdin) == str:
> +        stdin = stdin.encode()
> +
>      logging.info(cmd, extra={"context":"[CMD] "})
>  
>      r, w = os.pipe()
> @@ -297,6 +315,9 @@ def get_command_out(cmd, stdin=None, allow_fail=False, env_add=None):
>      if env_add:
>          new_env.update(env_add)
>  
> +    if type(stdin) == str:
> +        stdin = stdin.encode()
> +
>      logging.info(cmd, extra={"context":"[CMD] "})
>  
>      r, w = os.pipe()
> 


More information about the elbe-devel mailing list