From b2dbdc62726fce209b9de049bc15505c9b2fdf9a Mon Sep 17 00:00:00 2001 From: Jonathan MERCIER Date: Thu, 6 Feb 2020 11:47:08 +0100 Subject: [PATCH 1/5] fix #31650 right use of string and bytes objects --- tools/icu/icutrim.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/icu/icutrim.py b/tools/icu/icutrim.py index 25bd99d9cb260d..bc1ed8d2887c7c 100755 --- a/tools/icu/icutrim.py +++ b/tools/icu/icutrim.py @@ -285,7 +285,7 @@ def addTreeByType(tree, mytree): # read in the resource list for the tree treelistfile = os.path.join(options.tmpdir,"%s.lst" % tree) runcmd("iculslocs", "-i %s -N %s -T %s -l > %s" % (outfile, dataname, tree, treelistfile)) - fi = open(treelistfile, 'rb') + fi = open(treelistfile, 'r') treeitems = fi.readlines() trees[tree]["locs"] = [treeitems[i].strip() for i in range(len(treeitems))] fi.close() @@ -317,12 +317,12 @@ def removeList(count=0): erritems = fi.readlines() fi.close() #Item zone/zh_Hant_TW.res depends on missing item zone/zh_Hant.res - pat = re.compile("""^Item ([^ ]+) depends on missing item ([^ ]+).*""") + pat = re.compile(rb"^Item ([^ ]+) depends on missing item ([^ ]+).*") for i in range(len(erritems)): line = erritems[i].strip() m = pat.match(line) if m: - toDelete = m.group(1) + toDelete = m.group(1).decode("utf-8") if(options.verbose > 5): print("<< %s added to delete" % toDelete) remove.add(toDelete) From 4c49c5040341002c63d89b5d4da61470373ff157 Mon Sep 17 00:00:00 2001 From: Jonathan MERCIER Date: Wed, 26 Feb 2020 17:53:03 +0100 Subject: [PATCH 2/5] use of io.open function --- tools/icu/icutrim.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/tools/icu/icutrim.py b/tools/icu/icutrim.py index bc1ed8d2887c7c..62de77032e4c80 100755 --- a/tools/icu/icutrim.py +++ b/tools/icu/icutrim.py @@ -13,6 +13,7 @@ from __future__ import print_function +import io import json import optparse import os @@ -159,9 +160,8 @@ def runcmd(tool, cmd, doContinue=False): return rc ## STEP 0 - read in json config -fi= open(options.filterfile, "rb") -config=json.load(fi) -fi.close() +with io.open(options.filterfile, encoding='utf-8') as fi: + config=json.load(fi) if options.locales: config["variables"] = config.get("variables", {}) @@ -285,14 +285,14 @@ def addTreeByType(tree, mytree): # read in the resource list for the tree treelistfile = os.path.join(options.tmpdir,"%s.lst" % tree) runcmd("iculslocs", "-i %s -N %s -T %s -l > %s" % (outfile, dataname, tree, treelistfile)) - fi = open(treelistfile, 'r') - treeitems = fi.readlines() - trees[tree]["locs"] = [treeitems[i].strip() for i in range(len(treeitems))] - fi.close() - if tree not in config.get("trees", {}): - print(" Warning: filter file %s does not mention trees.%s - will be kept as-is" % (options.filterfile, tree)) - else: - queueForRemoval(tree) + with io.open(treelistfile, 'r', encoding='utf-8') as fi: + treeitems = fi.readlines() + trees[tree]["locs"] = [treeitems[i].strip() for i in range(len(treeitems))] + fi.close() + if tree not in config.get("trees", {}): + print(" Warning: filter file %s does not mention trees.%s - will be kept as-is" % (options.filterfile, tree)) + else: + queueForRemoval(tree) def removeList(count=0): # don't allow "keep" items to creep in here. @@ -317,7 +317,7 @@ def removeList(count=0): erritems = fi.readlines() fi.close() #Item zone/zh_Hant_TW.res depends on missing item zone/zh_Hant.res - pat = re.compile(rb"^Item ([^ ]+) depends on missing item ([^ ]+).*") + pat = re.compile(bytes(r"^Item ([^ ]+) depends on missing item ([^ ]+).*", 'utf-8')) for i in range(len(erritems)): line = erritems[i].strip() m = pat.match(line) From 17e6343117629933a0dfd94f95a946c9ba3c5b36 Mon Sep 17 00:00:00 2001 From: Jonathan MERCIER Date: Thu, 27 Feb 2020 18:39:16 +0100 Subject: [PATCH 3/5] Fix indent level --- tools/icu/icutrim.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tools/icu/icutrim.py b/tools/icu/icutrim.py index 62de77032e4c80..b11c3fa0ea66a9 100755 --- a/tools/icu/icutrim.py +++ b/tools/icu/icutrim.py @@ -288,11 +288,10 @@ def addTreeByType(tree, mytree): with io.open(treelistfile, 'r', encoding='utf-8') as fi: treeitems = fi.readlines() trees[tree]["locs"] = [treeitems[i].strip() for i in range(len(treeitems))] - fi.close() - if tree not in config.get("trees", {}): - print(" Warning: filter file %s does not mention trees.%s - will be kept as-is" % (options.filterfile, tree)) - else: - queueForRemoval(tree) + if tree not in config.get("trees", {}): + print(" Warning: filter file %s does not mention trees.%s - will be kept as-is" % (options.filterfile, tree)) + else: + queueForRemoval(tree) def removeList(count=0): # don't allow "keep" items to creep in here. From c816734be2304301b8b21ed970d3393b7d120c83 Mon Sep 17 00:00:00 2001 From: Jonathan MERCIER Date: Thu, 27 Feb 2020 18:40:46 +0100 Subject: [PATCH 4/5] Comprehension list more readable --- tools/icu/icutrim.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/icu/icutrim.py b/tools/icu/icutrim.py index b11c3fa0ea66a9..72cb772b57d3fa 100755 --- a/tools/icu/icutrim.py +++ b/tools/icu/icutrim.py @@ -287,7 +287,7 @@ def addTreeByType(tree, mytree): runcmd("iculslocs", "-i %s -N %s -T %s -l > %s" % (outfile, dataname, tree, treelistfile)) with io.open(treelistfile, 'r', encoding='utf-8') as fi: treeitems = fi.readlines() - trees[tree]["locs"] = [treeitems[i].strip() for i in range(len(treeitems))] + trees[tree]["locs"] = [line.strip() for line in treeitems] if tree not in config.get("trees", {}): print(" Warning: filter file %s does not mention trees.%s - will be kept as-is" % (options.filterfile, tree)) else: From b79b0d094baff842c0650c59996dd3a28632806b Mon Sep 17 00:00:00 2001 From: Jonathan MERCIER Date: Mon, 2 Mar 2020 09:19:59 +0100 Subject: [PATCH 5/5] Spaces around equal (PEP008) --- tools/icu/icutrim.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/icu/icutrim.py b/tools/icu/icutrim.py index 72cb772b57d3fa..f857e73bbce298 100755 --- a/tools/icu/icutrim.py +++ b/tools/icu/icutrim.py @@ -161,7 +161,7 @@ def runcmd(tool, cmd, doContinue=False): ## STEP 0 - read in json config with io.open(options.filterfile, encoding='utf-8') as fi: - config=json.load(fi) + config = json.load(fi) if options.locales: config["variables"] = config.get("variables", {})