From e17fd0d91cbd69fa9c01b20bd5c448d0a4fe0e67 Mon Sep 17 00:00:00 2001 From: Ebrahim Byagowi Date: Sun, 23 Feb 2020 23:58:39 +0330 Subject: [PATCH] [tools] More on py3 compatibility --- src/gen-tag-table.py | 23 +++++++---------------- src/gen-vowel-constraints.py | 13 ++++--------- test/shaping/hb-diff | 2 +- test/shaping/hb_test_tools.py | 6 +++--- 4 files changed, 15 insertions(+), 29 deletions(-) diff --git a/src/gen-tag-table.py b/src/gen-tag-table.py index f2ede064a..958a6e7c0 100755 --- a/src/gen-tag-table.py +++ b/src/gen-tag-table.py @@ -19,15 +19,10 @@ case, the fallback behavior will choose the right tag anyway. """ import collections -try: - from HTMLParser import HTMLParser - def write (s): - print (s.encode ('utf-8'), end='') -except ImportError: - from html.parser import HTMLParser - def write (s): - sys.stdout.flush () - sys.stdout.buffer.write (s.encode ('utf-8')) +from html.parser import HTMLParser +def write (s): + sys.stdout.flush () + sys.stdout.buffer.write (s.encode ('utf-8')) import io import itertools import re @@ -42,13 +37,9 @@ Input files, as of Unicode 12: * https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry''', file=sys.stderr) sys.exit (1) -try: - from html import unescape - def html_unescape (parser, entity): - return unescape (entity) -except ImportError: - def html_unescape (parser, entity): - return parser.unescape (entity) +from html import unescape +def html_unescape (parser, entity): + return unescape (entity) def expect (condition, message=None): if not condition: diff --git a/src/gen-vowel-constraints.py b/src/gen-vowel-constraints.py index 071864160..0a0337f27 100755 --- a/src/gen-vowel-constraints.py +++ b/src/gen-vowel-constraints.py @@ -10,15 +10,10 @@ This function should be used as the ``preprocess_text`` of an """ import collections -try: - from HTMLParser import HTMLParser - def write (s): - print (s.encode ('utf-8'), end='') -except ImportError: - from html.parser import HTMLParser - def write (s): - sys.stdout.flush () - sys.stdout.buffer.write (s.encode ('utf-8')) +from html.parser import HTMLParser +def write (s): + sys.stdout.flush () + sys.stdout.buffer.write (s.encode ('utf-8')) import itertools import io import sys diff --git a/test/shaping/hb-diff b/test/shaping/hb-diff index 3a79737d2..4c33bab53 100755 --- a/test/shaping/hb-diff +++ b/test/shaping/hb-diff @@ -4,7 +4,7 @@ from hb_test_tools import * import sys, os if len (sys.argv) < 2: - print "usage: %s FILES..." % sys.argv[0] + print ("usage: %s FILES..." % sys.argv[0]) sys.exit (1) ZipDiffer.diff_files (FileHelpers.open_file_or_stdin (f) for f in sys.argv[1:]) diff --git a/test/shaping/hb_test_tools.py b/test/shaping/hb_test_tools.py index b9c153c05..2af0d2f08 100644 --- a/test/shaping/hb_test_tools.py +++ b/test/shaping/hb_test_tools.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 -import sys, os, re, difflib, unicodedata, errno, cgi +import sys, os, re, difflib, unicodedata, errno, cgi, itertools from itertools import * try: import unicodedata2 as unicodedata @@ -139,7 +139,7 @@ class ZipDiffer: def diff_files (files, symbols=diff_symbols): files = tuple (files) # in case it's a generator, copy it try: - for lines in izip_longest (*files): + for lines in itertools.zip_longest (*files): if all (lines[0] == line for line in lines[1:]): sys.stdout.writelines ([" ", lines[0]]) continue @@ -433,7 +433,7 @@ class Unicode: s = re.sub (r"[<+>\\uU]", " ", s) s = re.sub (r"0[xX]", " ", s) s = [chr (int (x, 16)) for x in re.split ('[, \n]', s) if len (x)] - return ' + '.join (Unicode.pretty_name (x) for x in s).encode ('utf-8') + return ' + '.join (Unicode.pretty_name (x) for x in s) class FileHelpers: