Skip to content

Commit 6a72131

Browse files
committed
Allow htmldocck to run using Python 3.
1 parent a4d1149 commit 6a72131

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

src/etc/htmldocck.py

+20-13
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
3030
In order to avoid one-off dependencies for this task, this script uses
3131
a reasonably working HTML parser and the existing XPath implementation
32-
from Python 2's standard library. Hopefully we won't render
32+
from Python's standard library. Hopefully we won't render
3333
non-well-formed HTML.
3434
3535
# Commands
@@ -110,11 +110,17 @@
110110
import re
111111
import shlex
112112
from collections import namedtuple
113-
from HTMLParser import HTMLParser
113+
try:
114+
from html.parser import HTMLParser
115+
except ImportError:
116+
from HTMLParser import HTMLParser
114117
from xml.etree import cElementTree as ET
115118

116119
# ⇤/⇥ are not in HTML 4 but are in HTML 5
117-
from htmlentitydefs import entitydefs
120+
try:
121+
from html.entities import entitydefs
122+
except ImportError:
123+
from htmlentitydefs import entitydefs
118124
entitydefs['larrb'] = u'\u21e4'
119125
entitydefs['rarrb'] = u'\u21e5'
120126
entitydefs['nbsp'] = ' '
@@ -123,6 +129,11 @@
123129
VOID_ELEMENTS = set(['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'keygen',
124130
'link', 'menuitem', 'meta', 'param', 'source', 'track', 'wbr'])
125131

132+
# Python 2 -> 3 compatibility
133+
try:
134+
unichr
135+
except NameError:
136+
unichr = chr
126137

127138
class CustomHTMLParser(HTMLParser):
128139
"""simplified HTML parser.
@@ -184,12 +195,8 @@ def concat_multi_lines(f):
184195

185196
# strip the common prefix from the current line if needed
186197
if lastline is not None:
187-
maxprefix = 0
188-
for i in xrange(min(len(line), len(lastline))):
189-
if line[i] != lastline[i]:
190-
break
191-
maxprefix += 1
192-
line = line[maxprefix:].lstrip()
198+
common_prefix = os.path.commonprefix([line, lastline])
199+
line = line[len(common_prefix):].lstrip()
193200

194201
firstlineno = firstlineno or lineno
195202
if line.endswith('\\'):
@@ -213,7 +220,7 @@ def concat_multi_lines(f):
213220

214221

215222
def get_commands(template):
216-
with open(template, 'rUb') as f:
223+
with open(template, 'rU') as f:
217224
for lineno, line in concat_multi_lines(f):
218225
m = LINE_PATTERN.search(line)
219226
if not m:
@@ -372,7 +379,7 @@ def check_command(c, cache):
372379
cache.get_file(c.args[0])
373380
ret = True
374381
except FailedCheck as err:
375-
cerr = err.message
382+
cerr = str(err)
376383
ret = False
377384
elif len(c.args) == 2: # @has/matches <path> <pat> = string test
378385
cerr = "`PATTERN` did not match"
@@ -413,9 +420,9 @@ def check_command(c, cache):
413420

414421
except FailedCheck as err:
415422
message = '@{}{} check failed'.format('!' if c.negated else '', c.cmd)
416-
print_err(c.lineno, c.context, err.message, message)
423+
print_err(c.lineno, c.context, str(err), message)
417424
except InvalidCheck as err:
418-
print_err(c.lineno, c.context, err.message)
425+
print_err(c.lineno, c.context, str(err))
419426

420427
def check(target, commands):
421428
cache = CachedFiles(target)

0 commit comments

Comments
 (0)