Commit ef7423bf authored by Raul Tambre's avatar Raul Tambre Committed by Commit Bot

ulp_serialized_to_static_c.py: Python 3 compatibility

* Normal strings can't be used for operations with a file opened in binary mode. Fixed by using bytestrings.
* Due to being opened in binary mode bytes are read instead of strings in Python 3. Thus use ord() only in Python 2.

Still works with Python 2.

Fixed errors:
Traceback (most recent call last):
  File "../../components/language/content/browser/ulp_language_code_locator/ulp_serialized_to_static_c.py", line 79, in <module>
    Main()
  File "../../components/language/content/browser/ulp_language_code_locator/ulp_serialized_to_static_c.py", line 72, in Main
    models = [ReadSerializedData(data_path) for data_path in args.data]
  File "../../components/language/content/browser/ulp_language_code_locator/ulp_serialized_to_static_c.py", line 72, in <listcomp>
    models = [ReadSerializedData(data_path) for data_path in args.data]
  File "../../components/language/content/browser/ulp_language_code_locator/ulp_serialized_to_static_c.py", line 28, in ReadSerializedData
    linebreak = data.index('\n')
TypeError: argument should be integer or bytes-like object, not 'str'

Traceback (most recent call last):
  File "../../components/language/content/browser/ulp_language_code_locator/ulp_serialized_to_static_c.py", line 82, in <module>
    Main()
  File "../../components/language/content/browser/ulp_language_code_locator/ulp_serialized_to_static_c.py", line 75, in Main
    models = [ReadSerializedData(data_path) for data_path in args.data]
  File "../../components/language/content/browser/ulp_language_code_locator/ulp_serialized_to_static_c.py", line 75, in <listcomp>
    models = [ReadSerializedData(data_path) for data_path in args.data]
  File "../../components/language/content/browser/ulp_language_code_locator/ulp_serialized_to_static_c.py", line 37, in ReadSerializedData
    tree_serialized = [
  File "../../components/language/content/browser/ulp_language_code_locator/ulp_serialized_to_static_c.py", line 38, in <listcomp>
    sum((ord(tree_bytes[i+b]) << (8*b)) if i+b < len(tree_bytes) else 0
  File "../../components/language/content/browser/ulp_language_code_locator/ulp_serialized_to_static_c.py", line 38, in <genexpr>
    sum((ord(tree_bytes[i+b]) << (8*b)) if i+b < len(tree_bytes) else 0
TypeError: ord() expected string of length 1, but int found

Bug: 941669
Change-Id: Ib540c21eb63fc9407596a49e5114b8ab7e9a5a10
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1857123
Auto-Submit: Raul Tambre <raul@tambre.ee>
Commit-Queue: anthonyvd <anthonyvd@chromium.org>
Reviewed-by: default avatarAlexandre Frechette <frechette@chromium.org>
Reviewed-by: default avataranthonyvd <anthonyvd@chromium.org>
Cr-Commit-Position: refs/heads/master@{#712138}
parent 2ee9691d
...@@ -25,16 +25,20 @@ def ReadSerializedData(input_path): ...@@ -25,16 +25,20 @@ def ReadSerializedData(input_path):
with open(input_path, 'rb') as input_file: with open(input_path, 'rb') as input_file:
data = input_file.read() data = input_file.read()
linebreak = data.index('\n') linebreak = data.index(b'\n')
# First line is comma-separated list of languages. # First line is comma-separated list of languages.
language_codes = data[:linebreak].strip().split(',') language_codes = data[:linebreak].strip().split(b',')
# Rest of the file is the serialized tree. # Rest of the file is the serialized tree.
tree_bytes = data[linebreak+1:] tree_bytes = data[linebreak+1:]
# Strings are read in Python 2 so we need to use ord() to convert to bytes.
to_bytes = ord if sys.version_info.major == 2 else lambda x: x
# We group the bytes in the string into 32 bits integers. # We group the bytes in the string into 32 bits integers.
tree_serialized = [ tree_serialized = [
sum((ord(tree_bytes[i+b]) << (8*b)) if i+b < len(tree_bytes) else 0 sum((to_bytes(tree_bytes[i+b]) << (8*b)) if i+b < len(tree_bytes) else 0
for b in xrange(4)) for b in range(4))
for i in xrange(0, len(tree_bytes), 4) for i in range(0, len(tree_bytes), 4)
] ]
return language_codes, tree_serialized return language_codes, tree_serialized
......
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