[tools] More on py3 compatibility

This commit is contained in:
Ebrahim Byagowi 2020-02-23 23:58:39 +03:30
parent 0cf050a7b1
commit e17fd0d91c
4 changed files with 15 additions and 29 deletions

View File

@ -19,11 +19,6 @@ 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 ()
@ -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)
def expect (condition, message=None):
if not condition:

View File

@ -10,11 +10,6 @@ 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 ()

View File

@ -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:])

View File

@ -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: