| 1 | Restores compatibility with Python >=3.9,
|
| 2 | which removed the custom .error() method in
|
| 3 | https://github.com/python/cpython/commit/e34bbfd61f405eef89e8aa50672b0b25022de320
|
| 4 |
|
| 5 | Despite the big diff, only a try…except clause is added.
|
| 6 |
|
| 7 | |
| 8 | |
| 9 | @@ -101,113 +101,116 @@
|
| 10 | """Handle the remaining data."""
|
| 11 | self.goahead(1)
|
| 12 |
|
| 13 | - def error(self, message):
|
| 14 | - raise SGMLParseError(message)
|
| 15 | -
|
| 16 | # Internal -- handle data as far as reasonable. May leave state
|
| 17 | # and data to be processed by a subsequent call. If 'end' is
|
| 18 | # true, force handling all data as if followed by EOF marker.
|
| 19 | def goahead(self, end):
|
| 20 | - rawdata = self.rawdata
|
| 21 | - i = 0
|
| 22 | - n = len(rawdata)
|
| 23 | - while i < n:
|
| 24 | - if self.nomoretags:
|
| 25 | - self.handle_data(rawdata[i:n])
|
| 26 | - i = n
|
| 27 | - break
|
| 28 | - match = interesting.search(rawdata, i)
|
| 29 | - if match: j = match.start()
|
| 30 | - else: j = n
|
| 31 | - if i < j:
|
| 32 | - self.handle_data(rawdata[i:j])
|
| 33 | - i = j
|
| 34 | - if i == n: break
|
| 35 | - if rawdata[i] == '<':
|
| 36 | - if starttagopen.match(rawdata, i):
|
| 37 | + try:
|
| 38 | + rawdata = self.rawdata
|
| 39 | + i = 0
|
| 40 | + n = len(rawdata)
|
| 41 | + while i < n:
|
| 42 | + if self.nomoretags:
|
| 43 | + self.handle_data(rawdata[i:n])
|
| 44 | + i = n
|
| 45 | + break
|
| 46 | + match = interesting.search(rawdata, i)
|
| 47 | + if match: j = match.start()
|
| 48 | + else: j = n
|
| 49 | + if i < j:
|
| 50 | + self.handle_data(rawdata[i:j])
|
| 51 | + i = j
|
| 52 | + if i == n: break
|
| 53 | + if rawdata[i] == '<':
|
| 54 | + if starttagopen.match(rawdata, i):
|
| 55 | + if self.literal:
|
| 56 | + self.handle_data(rawdata[i])
|
| 57 | + i = i+1
|
| 58 | + continue
|
| 59 | + k = self.parse_starttag(i)
|
| 60 | + if k < 0: break
|
| 61 | + i = k
|
| 62 | + continue
|
| 63 | + if rawdata.startswith("</", i):
|
| 64 | + k = self.parse_endtag(i)
|
| 65 | + if k < 0: break
|
| 66 | + i = k
|
| 67 | + self.literal = 0
|
| 68 | + continue
|
| 69 | + if self.literal:
|
| 70 | + if n > (i + 1):
|
| 71 | + self.handle_data("<")
|
| 72 | + i = i+1
|
| 73 | + else:
|
| 74 | + # incomplete
|
| 75 | + break
|
| 76 | + continue
|
| 77 | + if rawdata.startswith("<!--", i):
|
| 78 | + # Strictly speaking, a comment is --.*--
|
| 79 | + # within a declaration tag <!...>.
|
| 80 | + # This should be removed,
|
| 81 | + # and comments handled only in parse_declaration.
|
| 82 | + k = self.parse_comment(i)
|
| 83 | + if k < 0: break
|
| 84 | + i = k
|
| 85 | + continue
|
| 86 | + if rawdata.startswith("<?", i):
|
| 87 | + k = self.parse_pi(i)
|
| 88 | + if k < 0: break
|
| 89 | + i = i+k
|
| 90 | + continue
|
| 91 | + if rawdata.startswith("<!", i):
|
| 92 | + # This is some sort of declaration; in "HTML as
|
| 93 | + # deployed," this should only be the document type
|
| 94 | + # declaration ("<!DOCTYPE html...>").
|
| 95 | + k = self.parse_declaration(i)
|
| 96 | + if k < 0: break
|
| 97 | + i = k
|
| 98 | + continue
|
| 99 | + elif rawdata[i] == '&':
|
| 100 | if self.literal:
|
| 101 | self.handle_data(rawdata[i])
|
| 102 | i = i+1
|
| 103 | continue
|
| 104 | - k = self.parse_starttag(i)
|
| 105 | - if k < 0: break
|
| 106 | - i = k
|
| 107 | - continue
|
| 108 | - if rawdata.startswith("</", i):
|
| 109 | - k = self.parse_endtag(i)
|
| 110 | - if k < 0: break
|
| 111 | - i = k
|
| 112 | - self.literal = 0
|
| 113 | - continue
|
| 114 | - if self.literal:
|
| 115 | - if n > (i + 1):
|
| 116 | - self.handle_data("<")
|
| 117 | - i = i+1
|
| 118 | - else:
|
| 119 | - # incomplete
|
| 120 | - break
|
| 121 | - continue
|
| 122 | - if rawdata.startswith("<!--", i):
|
| 123 | - # Strictly speaking, a comment is --.*--
|
| 124 | - # within a declaration tag <!...>.
|
| 125 | - # This should be removed,
|
| 126 | - # and comments handled only in parse_declaration.
|
| 127 | - k = self.parse_comment(i)
|
| 128 | - if k < 0: break
|
| 129 | - i = k
|
| 130 | - continue
|
| 131 | - if rawdata.startswith("<?", i):
|
| 132 | - k = self.parse_pi(i)
|
| 133 | - if k < 0: break
|
| 134 | - i = i+k
|
| 135 | - continue
|
| 136 | - if rawdata.startswith("<!", i):
|
| 137 | - # This is some sort of declaration; in "HTML as
|
| 138 | - # deployed," this should only be the document type
|
| 139 | - # declaration ("<!DOCTYPE html...>").
|
| 140 | - k = self.parse_declaration(i)
|
| 141 | - if k < 0: break
|
| 142 | - i = k
|
| 143 | - continue
|
| 144 | - elif rawdata[i] == '&':
|
| 145 | - if self.literal:
|
| 146 | + match = charref.match(rawdata, i)
|
| 147 | + if match:
|
| 148 | + name = match.group(1)
|
| 149 | + self.handle_charref(name)
|
| 150 | + i = match.end(0)
|
| 151 | + if rawdata[i-1] != ';': i = i-1
|
| 152 | + continue
|
| 153 | + match = entityref.match(rawdata, i)
|
| 154 | + if match:
|
| 155 | + name = match.group(1)
|
| 156 | + self.handle_entityref(name)
|
| 157 | + i = match.end(0)
|
| 158 | + if rawdata[i-1] != ';': i = i-1
|
| 159 | + continue
|
| 160 | + else:
|
| 161 | + self.error('neither < nor & ??')
|
| 162 | + # We get here only if incomplete matches but
|
| 163 | + # nothing else
|
| 164 | + match = incomplete.match(rawdata, i)
|
| 165 | + if not match:
|
| 166 | self.handle_data(rawdata[i])
|
| 167 | i = i+1
|
| 168 | continue
|
| 169 | - match = charref.match(rawdata, i)
|
| 170 | - if match:
|
| 171 | - name = match.group(1)
|
| 172 | - self.handle_charref(name)
|
| 173 | - i = match.end(0)
|
| 174 | - if rawdata[i-1] != ';': i = i-1
|
| 175 | - continue
|
| 176 | - match = entityref.match(rawdata, i)
|
| 177 | - if match:
|
| 178 | - name = match.group(1)
|
| 179 | - self.handle_entityref(name)
|
| 180 | - i = match.end(0)
|
| 181 | - if rawdata[i-1] != ';': i = i-1
|
| 182 | - continue
|
| 183 | - else:
|
| 184 | - self.error('neither < nor & ??')
|
| 185 | - # We get here only if incomplete matches but
|
| 186 | - # nothing else
|
| 187 | - match = incomplete.match(rawdata, i)
|
| 188 | - if not match:
|
| 189 | - self.handle_data(rawdata[i])
|
| 190 | - i = i+1
|
| 191 | - continue
|
| 192 | - j = match.end(0)
|
| 193 | - if j == n:
|
| 194 | - break # Really incomplete
|
| 195 | - self.handle_data(rawdata[i:j])
|
| 196 | - i = j
|
| 197 | - # end while
|
| 198 | - if end and i < n:
|
| 199 | - self.handle_data(rawdata[i:n])
|
| 200 | - i = n
|
| 201 | - self.rawdata = rawdata[i:]
|
| 202 | - # XXX if end: check for empty stack
|
| 203 | + j = match.end(0)
|
| 204 | + if j == n:
|
| 205 | + break # Really incomplete
|
| 206 | + self.handle_data(rawdata[i:j])
|
| 207 | + i = j
|
| 208 | + # end while
|
| 209 | + if end and i < n:
|
| 210 | + self.handle_data(rawdata[i:n])
|
| 211 | + i = n
|
| 212 | + self.rawdata = rawdata[i:]
|
| 213 | + # XXX if end: check for empty stack
|
| 214 | + except AssertionError as e:
|
| 215 | + # The .error() method, which threw the custom SGMLParseError was removed
|
| 216 | + # by https://github.com/python/cpython/issues/76025. So we have to catch
|
| 217 | + # _markupbase’s AssertionError and translate it into the old one.
|
| 218 | + raise SGMLParseError (e.args[0]) from e
|
| 219 |
|
| 220 | # Extensions for the DOCTYPE scanner:
|
| 221 | _decl_otherchars = '='
|