Page MenuHomePhabricator

IndexError when showing an empty diff with a non-zero context
Closed, ResolvedPublicBUG REPORT

Description

Hello there,

showDiff throws an IndexError if used on texts with no difference but a non-zero context. Since this is not documented, I think this is a bug.

I can reproduce the bug with the latest git code (f2daf078):

$ python3
Python 3.7.7 (default, Mar 10 2020, 15:43:33)
[Clang 11.0.0 (clang-1100.0.33.17)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pywikibot
>>> pywikibot.showDiff("foo", "foo", context=2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/private/tmp/pywikibot/pywikibot/__init__.py", line 1314, in showDiff
    PatchManager(oldtext, newtext, context=context).print_hunks()
  File "/private/tmp/pywikibot/pywikibot/tools/__init__.py", line 1797, in wrapper
    return obj(*__args, **__kw)
  File "/private/tmp/pywikibot/pywikibot/diff.py", line 300, in __init__
    self._super_hunks = self._generate_super_hunks()
  File "/private/tmp/pywikibot/pywikibot/diff.py", line 361, in _generate_super_hunks
    return [_SuperHunk(sh) for sh in super_hunks]
  File "/private/tmp/pywikibot/pywikibot/diff.py", line 361, in <listcomp>
    return [_SuperHunk(sh) for sh in super_hunks]
  File "/private/tmp/pywikibot/pywikibot/diff.py", line 223, in __init__
    self.a_rng = (self._hunks[0].a_rng[0], self._hunks[-1].a_rng[1])
IndexError: list index out of range

Note: the length of the text doesn’t matter:

>>> text = "foobar\n" * 100
>>> pywikibot.showDiff(text, text, context=2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/private/tmp/pywikibot/pywikibot/__init__.py", line 1314, in showDiff
    PatchManager(oldtext, newtext, context=context).print_hunks()
  File "/private/tmp/pywikibot/pywikibot/tools/__init__.py", line 1797, in wrapper
    return obj(*__args, **__kw)
  File "/private/tmp/pywikibot/pywikibot/diff.py", line 300, in __init__
    self._super_hunks = self._generate_super_hunks()
  File "/private/tmp/pywikibot/pywikibot/diff.py", line 361, in _generate_super_hunks
    return [_SuperHunk(sh) for sh in super_hunks]
  File "/private/tmp/pywikibot/pywikibot/diff.py", line 361, in <listcomp>
    return [_SuperHunk(sh) for sh in super_hunks]
  File "/private/tmp/pywikibot/pywikibot/diff.py", line 223, in __init__
    self.a_rng = (self._hunks[0].a_rng[0], self._hunks[-1].a_rng[1])
IndexError: list index out of range

Expected output: pywikibot.showDiff("foo", "foo", context=2) should print nothing, just like pywikibot.showDiff("foo", "foo") does.
Actual output: see above.

Event Timeline

Xqt triaged this task as Medium priority.May 13 2020, 11:19 PM
Xqt changed the subtype of this task from "Task" to "Bug Report".
Xqt added a subscriber: Mpaa.

Change 596433 had a related patch set uploaded (by Xqt; owner: Xqt):
[pywikibot/core@master] [bugfix] IndexError when showing an empty diff with a non-zero context

https://gerrit.wikimedia.org/r/596433

Change 596433 merged by jenkins-bot:
[pywikibot/core@master] [bugfix] Solve IndexError when showing an empty diff with a non-zero context

https://gerrit.wikimedia.org/r/596433