Skip to content

Commit

Permalink
Convert returned binary data from call() to string
Browse files Browse the repository at this point in the history
This changes is based on the same cause as
commit 6ea4b6f

subprocess.check_output() returns binary data as output. In python2,
binary is basically an alias of str so it can be directly used as a
normal string. However in python3 the binary data is different to a
string. Inserting binary data into a string in python3 generates
strings like '/some/path/with/b'binary'/data/inserted' which are not
correct file paths. We must decode() the returned data in order to use
it as a string.

This change also explicitly sets encoding="utf-8". In Python2,
default decoding is ASCII while in Python 3 it is "utf-8". Leaving
encoding parameters as defaults will break non-ASCII output on Python 2.
  • Loading branch information
ziransun authored and jgraham committed Feb 10, 2020
1 parent ba0fe09 commit 9ce6f13
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
8 changes: 4 additions & 4 deletions tools/wpt/browser.py
Expand Up @@ -1087,11 +1087,11 @@ def version(self, binary=None, webdriver_binary=None):
except subprocess.CalledProcessError:
self.logger.warning("Failed to call %s --version" % webdriver_binary)
return None
m = re.match(br"Included with Safari (.*)", version_string)
m = re.match(r"Included with Safari (.*)", version_string)
if not m:
self.logger.warning("Failed to extract version from: %s" % version_string)
return None
return m.group(1).decode()
return m.group(1)


class Servo(Browser):
Expand Down Expand Up @@ -1234,7 +1234,7 @@ def find_binary(self, venv_path=None, channel=None):
gcc = find_executable("gcc")
if gcc:
try:
triplet = call(gcc, "-dumpmachine").decode().strip()
triplet = call(gcc, "-dumpmachine").strip()
except subprocess.CalledProcessError:
pass
# Add Debian/Ubuntu path
Expand All @@ -1253,7 +1253,7 @@ def version(self, binary=None, webdriver_binary=None):
if binary is None:
return None
try: # WebKitGTK MiniBrowser before 2.26.0 doesn't support --version
output = call(binary, "--version").decode().strip()
output = call(binary, "--version").strip()
except subprocess.CalledProcessError:
return None
# Example output: "WebKitGTK 2.26.1"
Expand Down
2 changes: 1 addition & 1 deletion tools/wpt/utils.py
Expand Up @@ -46,7 +46,7 @@ def call(*args):
"""
logger.debug(" ".join(args))
try:
return subprocess.check_output(args)
return subprocess.check_output(args).decode('utf8')
except subprocess.CalledProcessError as e:
logger.critical("%s exited with return code %i" %
(e.cmd, e.returncode))
Expand Down

0 comments on commit 9ce6f13

Please sign in to comment.