2929
3030In order to avoid one-off dependencies for this task, this script uses
3131a 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
3333non-well-formed HTML.
3434
3535# Commands
110110import re
111111import shlex
112112from collections import namedtuple
113- from HTMLParser import HTMLParser
113+ try :
114+ from html .parser import HTMLParser
115+ except ImportError :
116+ from HTMLParser import HTMLParser
114117from 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
118124entitydefs ['larrb' ] = u'\u21e4 '
119125entitydefs ['rarrb' ] = u'\u21e5 '
120126entitydefs ['nbsp' ] = ' '
123129VOID_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
127138class 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
215222def 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
420427def check (target , commands ):
421428 cache = CachedFiles (target )
0 commit comments