From bf825d06a910e4c2be8243987b182e51cb24087c Mon Sep 17 00:00:00 2001 From: Omer Karaduman Date: Sat, 17 Nov 2018 23:09:30 +0100 Subject: [PATCH] Fix issues related to Python 3 compatibility --- build.py | 6 ++++-- i18n/create_messages.py | 2 +- i18n/dedup_json.py | 4 ++-- i18n/json_to_js.py | 2 +- i18n/tests.py | 2 +- i18n/xliff_to_json.py | 10 +++++----- 6 files changed, 14 insertions(+), 12 deletions(-) diff --git a/build.py b/build.py index f36e5027d..1566ae3f0 100755 --- a/build.py +++ b/build.py @@ -336,7 +336,9 @@ class Gen_compressed(threading.Thread): conn = httplib.HTTPSConnection("closure-compiler.appspot.com") conn.request("POST", "/compile", urlencode(params), headers) response = conn.getresponse() - json_str = response.read() + + # Decode is necessary for Python 3.4 compatibility + json_str = response.read().decode("utf-8") conn.close() # Parse the JSON response. @@ -456,7 +458,7 @@ class Gen_langfiles(threading.Thread): # If a destination file was missing, rebuild. return True else: - print("Error checking file creation times: " + e) + print("Error checking file creation times: " + str(e)) def run(self): # The files msg/json/{en,qqq,synonyms}.json depend on msg/messages.js. diff --git a/i18n/create_messages.py b/i18n/create_messages.py index 403d4d51f..8a93fbbc7 100755 --- a/i18n/create_messages.py +++ b/i18n/create_messages.py @@ -87,7 +87,7 @@ def main(): # Read in synonyms file, which must be output in every language. synonym_defs = read_json_file(os.path.join( os.curdir, args.source_synonym_file)) - + # synonym_defs is also being sorted to ensure the same order is kept synonym_text = '\n'.join([u'Blockly.Msg["{0}"] = Blockly.Msg["{1}"];' .format(key, synonym_defs[key]) for key in sorted(synonym_defs)]) diff --git a/i18n/dedup_json.py b/i18n/dedup_json.py index 30e572dde..a27df50f7 100755 --- a/i18n/dedup_json.py +++ b/i18n/dedup_json.py @@ -51,9 +51,9 @@ def main(): try: with codecs.open(filename, 'r', 'utf-8') as infile: j = json.load(infile) - except ValueError, e: + except ValueError as e: print('Error reading ' + filename) - raise InputError(file, str(e)) + raise InputError(filename, str(e)) # Built up output strings as an array to make output of delimiters easier. output = [] diff --git a/i18n/json_to_js.py b/i18n/json_to_js.py index f8c20f6af..bf3fb38df 100755 --- a/i18n/json_to_js.py +++ b/i18n/json_to_js.py @@ -100,7 +100,7 @@ def _process_file(path_to_json, target_lang, key_dict): if key != '@metadata': try: identifier = key_dict[key] - except KeyError, e: + except KeyError as e: print('Key "%s" is in %s but not in %s' % (key, keyfile, args.key_file)) raise e diff --git a/i18n/tests.py b/i18n/tests.py index 7e4fc49aa..2de6fef60 100644 --- a/i18n/tests.py +++ b/i18n/tests.py @@ -37,7 +37,7 @@ class TestSequenceFunctions(unittest.TestCase): u'block of actions.'] for sentence in sentences: output = common.insert_breaks(sentence, 30, 50) - self.assert_(contains_all_chars(sentence, output), + self.assertTrue(contains_all_chars(sentence, output), u'Mismatch between:\n{0}\n{1}'.format( re.sub(spaces, '', sentence), re.sub(spaces, '', output))) diff --git a/i18n/xliff_to_json.py b/i18n/xliff_to_json.py index b38b4d6ec..c95d83366 100755 --- a/i18n/xliff_to_json.py +++ b/i18n/xliff_to_json.py @@ -65,7 +65,7 @@ def _parse_trans_unit(trans_unit): try: result['source'] = get_value('source') result['target'] = get_value('target') - except InputError, e: + except InputError as e: raise InputError(key, e.msg) # Get notes, using the from value as key and the data as value. @@ -112,8 +112,8 @@ def _process_file(filename): except IOError: # Don't get caught by below handler raise - except Exception, e: - print + except Exception as e: + print() raise InputError(filename, str(e)) # Make sure needed fields are present and non-empty. @@ -146,8 +146,8 @@ def _process_file(filename): results.append(unit) return results - except IOError, e: - print 'Error with file {0}: {1}'.format(filename, e.strerror) + except IOError as e: + print('Error with file {0}: {1}'.format(filename, e.strerror)) sys.exit(1)