This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Index error for empty lists in Difflib
Type: Stage:
Components: Library (Lib) Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: barry, brett.cannon, roccomoretti
Priority: normal Keywords:

Created on 2004-06-26 02:41 by roccomoretti, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (3)
msg21318 - (view) Author: Rocco Moretti (roccomoretti) Date: 2004-06-26 02:41
When using the context_diff and unified_diff functions
in difflib, submitting two empty lists to compare to
each other produces an IndexError, as opposed to the
expected empty diff. This error does not occur when
using ndiff, or when comparing a nonempty list to an
empty list. (In either direction)

<See patch manager for a potential fix.>

Example Behavior:

Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200
32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for
more information.
>>> import difflib
>>> difflib.unified_diff([],[])
<generator object at 0x008E0918>
>>> list(difflib.unified_diff([],[]))
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "C:\Python23\lib\difflib.py", line 1149, in
unified_diff
    for group in
SequenceMatcher(None,a,b).get_grouped_opcodes(n):
  File "C:\Python23\lib\difflib.py", line 574, in
get_grouped_opcodes
    if codes[0][0] == 'equal':
IndexError: list index out of range
>>> list(difflib.unified_diff([],['']))
['---  \n', '+++  \n', '@@ -1,0 +1,1 @@\n', '+']
>>> list(difflib.unified_diff([''],[]))
['---  \n', '+++  \n', '@@ -1,1 +1,0 @@\n', '-']
>>>
msg21319 - (view) Author: Barry A. Warsaw (barry) * (Python committer) Date: 2004-06-26 03:18
Logged In: YES 
user_id=12800

Probably a dup of 979794
msg21320 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2004-07-10 23:55
Logged In: YES 
user_id=357491

Fixed in rev. 1.21.  Not being backported in case someone actually 
expects this behavior.
History
Date User Action Args
2022-04-11 14:56:05adminsetgithub: 40461
2004-06-26 02:41:30roccomoretticreate