Commit 563c078e authored by Wojciech Bielawski's avatar Wojciech Bielawski Committed by Chromium LUCI CQ

Unicode compatibility with python 3.

Python 3 distinguish butes and strings and strings are always unicode.
That's why files need to be open in binary mode and proper encoding must
be applied before write

Bug: 1168119
Change-Id: I14b65600a736eda6ad263a011ab8886f1e521f64
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2642679Reviewed-by: default avatarYuki Shiino <yukishiino@chromium.org>
Reviewed-by: default avatarHitoshi Yoshida <peria@chromium.org>
Commit-Queue: Yuki Shiino <yukishiino@chromium.org>
Cr-Commit-Position: refs/heads/master@{#846004}
parent ac7fed6f
......@@ -179,4 +179,5 @@ def write_code_node_to_file(code_node, filepath):
filename=format_result.filename,
stderr=format_result.error_message))
web_idl.file_io.write_to_file_if_changed(filepath, format_result.contents)
web_idl.file_io.write_to_file_if_changed(
filepath, format_result.contents.encode('utf-8'))
......@@ -70,8 +70,13 @@ def gn_format(contents, filename=None):
def _invoke_format_command(command_line, filename, contents):
proc = subprocess.Popen(
command_line, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
kwargs = {}
if sys.version_info.major != 2:
kwargs['encoding'] = 'utf-8'
proc = subprocess.Popen(command_line,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
**kwargs)
stdout_output, stderr_output = proc.communicate(input=contents)
exit_code = proc.wait()
......
......@@ -210,8 +210,8 @@ class ComponentInfoProviderModules(ComponentInfoProvider):
def load_interfaces_info_overall_pickle(info_dir):
with open(os.path.join(info_dir,
'interfaces_info.pickle')) as interface_info_file:
with open(os.path.join(info_dir, 'interfaces_info.pickle'),
mode='rb') as interface_info_file:
return pickle.load(interface_info_file)
......@@ -237,23 +237,20 @@ def merge_dict_recursively(target, diff):
def create_component_info_provider_core(info_dir):
interfaces_info = load_interfaces_info_overall_pickle(info_dir)
with open(
os.path.join(info_dir, 'core',
'component_info_core.pickle')) as component_info_file:
with open(os.path.join(info_dir, 'core', 'component_info_core.pickle'),
mode='rb') as component_info_file:
component_info = pickle.load(component_info_file)
return ComponentInfoProviderCore(interfaces_info, component_info)
def create_component_info_provider_modules(info_dir):
interfaces_info = load_interfaces_info_overall_pickle(info_dir)
with open(
os.path.join(info_dir, 'core',
'component_info_core.pickle')) as component_info_file:
with open(os.path.join(info_dir, 'core', 'component_info_core.pickle'),
mode='rb') as component_info_file:
component_info_core = pickle.load(component_info_file)
with open(
os.path.join(
info_dir, 'modules',
'component_info_modules.pickle')) as component_info_file:
with open(os.path.join(info_dir, 'modules',
'component_info_modules.pickle'),
mode='rb') as component_info_file:
component_info_modules = pickle.load(component_info_file)
return ComponentInfoProviderModules(interfaces_info, component_info_core,
component_info_modules)
......@@ -348,7 +345,7 @@ def write_file(new_text, destination_filename):
def write_pickle_file(pickle_filename, data):
# If |data| is same with the file content, we skip updating.
if os.path.isfile(pickle_filename):
with open(pickle_filename) as pickle_file:
with open(pickle_filename, 'rb') as pickle_file:
try:
if pickle.load(pickle_file) == data:
return
......
......@@ -95,7 +95,7 @@ def main():
open(args.output_file, 'wb').write(
generate_gperf(gperf_path,
open(infile).read(), gperf_args))
open(infile).read(), gperf_args).encode('utf-8'))
if __name__ == '__main__':
......
......@@ -138,7 +138,7 @@ class RuntimeFeatureWriter(BaseRuntimeFeatureWriter):
except Exception:
# If trouble unpickling, overwrite
pass
with open(os.path.abspath(file_name), 'w') as pickle_file:
with open(os.path.abspath(file_name), 'wb') as pickle_file:
pickle.dump(features_map, pickle_file)
def _template_inputs(self):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment