On some pages Pywikibot freezes when doing removeEmptySections. It happens time to time:
Exception in thread Put-Thread:
Traceback (most recent call last):
File "/usr/lib/python3.7/threading.py", line 917, in _bootstrap_inner
self.run()
File "/usr/lib/python3.7/threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "/home/pavel/pywikibot/pywikibot/__init__.py", line 1404, in async_manager
request(*args, **kwargs)
File "/home/pavel/pywikibot/pywikibot/page.py", line 109, in handle
func(self, *args, **kwargs)
File "/home/pavel/pywikibot/pywikibot/page.py", line 1292, in _save
summary = self._cosmetic_changes_hook(summary)
File "/home/pavel/pywikibot/pywikibot/page.py", line 1338, in _cosmetic_changes_hook
self.text = cc_toolkit.change(old)
File "/home/pavel/pywikibot/pywikibot/cosmetic_changes.py", line 302, in change
new_text = self._change(text)
File "/home/pavel/pywikibot/pywikibot/cosmetic_changes.py", line 296, in _change
text = self.safe_execute(method, text)
File "/home/pavel/pywikibot/pywikibot/cosmetic_changes.py", line 283, in safe_execute
result = method(text)
File "/home/pavel/pywikibot/pywikibot/cosmetic_changes.py", line 778, in removeEmptySections
current_heading = sections[i][0]
IndexError: list index out of rangeThis is caused by the header regex in textlib, which can not work with line like this:
text <!-- comment -->== Heading == text
If we strip comments, it works, if we do not, it doesn't work. Therefore errors like this can happen. MediaWiki can parse heading like this correctly, therefore we should too.

