mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Make the syntax highlighter run_loop robust against a buggy syntax highlighter that would result in an infinite loop
This commit is contained in:
parent
f1f030d34e
commit
1dbe6f041d
@ -6,6 +6,8 @@ from __future__ import (unicode_literals, division, absolute_import,
|
|||||||
__license__ = 'GPL v3'
|
__license__ = 'GPL v3'
|
||||||
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
|
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
|
||||||
|
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
from PyQt4.Qt import (
|
from PyQt4.Qt import (
|
||||||
QTextCursor, pyqtSlot, QTextBlockUserData, QTextLayout)
|
QTextCursor, pyqtSlot, QTextBlockUserData, QTextLayout)
|
||||||
|
|
||||||
@ -15,11 +17,19 @@ from calibre.gui2.tweak_book.widgets import BusyCursor
|
|||||||
def run_loop(user_data, state_map, formats, text):
|
def run_loop(user_data, state_map, formats, text):
|
||||||
state = user_data.state
|
state = user_data.state
|
||||||
i = 0
|
i = 0
|
||||||
|
seen_states = defaultdict(set)
|
||||||
while i < len(text):
|
while i < len(text):
|
||||||
|
orig_i = i
|
||||||
|
seen_states[i].add(state.parse)
|
||||||
fmt = state_map[state.parse](state, text, i, formats, user_data)
|
fmt = state_map[state.parse](state, text, i, formats, user_data)
|
||||||
for num, f in fmt:
|
for num, f in fmt:
|
||||||
|
if num > 0:
|
||||||
yield i, num, f
|
yield i, num, f
|
||||||
i += num
|
i += num
|
||||||
|
if orig_i == i and state.parse in seen_states[i]:
|
||||||
|
# Something went wrong in the syntax highlighter
|
||||||
|
print ('Syntax highlighter returned a zero length format, parse state:', state.parse)
|
||||||
|
break
|
||||||
|
|
||||||
class SimpleState(object):
|
class SimpleState(object):
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user