Deprecate execute(), allow it to return strings or bytes and make stripping whitespace from the output optional.

Review Request #13283 — Created Sept. 22, 2023 and updated

Information

RBTools
release-5.x

Reviewers

Our rbtools.hooks.common.execute() function uses subprocess.Popen and
Popen.communicate to execute commands and read their output. Between Python 2
and 3, subprocess.Popen(...).communicate() changed from returning a tuple of
strings to a tuple of bytes. This breaks some of our hook util methods in
rbtools.hooks.git where we try to perform string operations on the output
of execute(), but we receive bytes instead of strings.

Python 3.7 added a text parameter to Popen to control whether file
objects, stdin, stdout, and stderr are opened in text mode or binary mode.
We make use of that parameter by adding our own text parameter to execute()
to control whether to return the stdout output as a string or bytes. This
defaults to True to match the previous behavior.

This also makes it optional to strip whitespace from the output, instead of
always stripping it. This is useful when dealing with commands where we
need to preserve the whitespace in the output, such as with git diff.

This changes also deprecates the rbtools.hooks.common.execute() function
in favour of rbtools.utils.process.run_process(), which is more
future-proof and has better type safety.

  • Ran into an error when running the git pre-receive hook, which came
    from the clash between string and bytes. Applied this fix and ran
    the hook again, saw that the error was gone.
  • Ran unit tests.
Summary ID
Ensure that the execute method returns strings and not bytes.
424c2ac935f8cbca0dbf919ce4f2b2992739c159
Description From Last Updated

While we're here, let's change this from "Executes and returns" to "Execute and return"

daviddavid

Might want to specify here that this only applies when text=True.

daviddavid
david
  1. 
      
  2. rbtools/hooks/common.py (Diff revision 1)
     
     

    While we're here, let's change this from "Executes and returns" to "Execute and return"

  3. 
      
maubin
david
  1. Ship It!
  2. 
      
maubin
david
  1. 
      
  2. rbtools/hooks/common.py (Diff revision 3)
     
     
     

    Might want to specify here that this only applies when text=True.

  3. 
      
maubin
david
  1. Ship It!
  2. 
      
chipx86
  1. This change looks fine, but we should probably deprecate this method in favor of the modern run_process() code. We'll still need to keep this method around for existing hook scripts for now, though.

    1. Sounds good, I'll deprecate it in this change.

  2. 
      
maubin
Review request changed

Change Summary:

  • Deprecates the execute function.

Summary:

-Allow the execute method to return strings or bytes and make stripping whitespace from the output optional.
+Deprecate execute(), allow it to return strings or bytes and make stripping whitespace from the output optional.

Description:

~  

Our rbtools.hooks.common.execute() method uses subprocess.Popen and

  ~

Our rbtools.hooks.common.execute() function uses subprocess.Popen and

    Popen.communicate to execute commands and read their output. Between Python 2
    and 3, subprocess.Popen(...).communicate() changed from returning a tuple of
    strings to a tuple of bytes. This breaks some of our hook util methods in
    rbtools.hooks.git where we try to perform string operations on the output
    of execute(), but we receive bytes instead of strings.

   
   

Python 3.7 added a text parameter to Popen to control whether file

    objects, stdin, stdout, and stderr are opened in text mode or binary mode.
    We make use of that parameter by adding our own text parameter to execute()
    to control whether to return the stdout output as a string or bytes. This
    defaults to True to match the previous behavior.

   
   

This also makes it optional to strip whitespace from the output, instead of

    always stripping it. This is useful when dealing with commands where we
    need to preserve the whitespace in the output, such as with git diff.

  +
  +

This changes also deprecates the rbtools.hooks.common.execute() function

  + in favour of rbtools.utils.process.run_process(), which is more
  + future-proof and has better type safety.

Commits:

Summary ID
Ensure that the execute method returns strings and not bytes.
0d97af01419d46f88f103a45fd8368e58c092c6e
Ensure that the execute method returns strings and not bytes.
424c2ac935f8cbca0dbf919ce4f2b2992739c159

Diff:

Revision 5 (+106 -8)

Show changes

Checks run (2 succeeded)

flake8 passed.
JSHint passed.
Loading...