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: generator expression produces incomplete list
Type: Stage:
Components: Interpreter Core Versions: Python 2.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: georg.brandl, wplapper
Priority: normal Keywords:

Created on 2007-04-05 20:59 by wplapper, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
test_bug.py wplapper, 2007-04-05 20:59 program which reproduces bug and correct behaviour.
Messages (2)
msg31710 - (view) Author: wplapper (wplapper) Date: 2007-04-05 20:59
(eliminate(values, s, d2) for d2 in values[s] if d2 != d)   
is producing incomplete results for the above statement.

If the statement split into two parts, the results are correct.
    xxx = [d2 for d2 in values[s] if d2 != d]
    zzz = (eliminate(values, s, d2) for d2 in xxx)
    return all(zzz)

First create temporary list xxx and then apply generator expression zzz.


The problem has been observed for python 2.4.3 and 2.5.0.0, on Windows, Solaris (x86) and on Linux. For me, the error pattern is consistent for all test cases I ran.

This is the output of the attach script:
wrong
assign.s    0 d 2 val [1, 2, 3, 4, 5, 6, 7, 8, 9]
eliminate.s 0 d 1 val [1, 2, 3, 4, 5, 6, 7, 8, 9]
eliminate.s 0 d 3 val [2, 3, 4, 5, 6, 7, 8, 9]
eliminate.s 0 d 5 val [2, 4, 5, 6, 7, 8, 9]
eliminate.s 0 d 7 val [2, 4, 6, 7, 8, 9]
eliminate.s 0 d 9 val [2, 4, 6, 8, 9]

right
assign.s    0 d 2 val [1, 2, 3, 4, 5, 6, 7, 8, 9]
eliminate.s 0 d 1 val [1, 2, 3, 4, 5, 6, 7, 8, 9]
eliminate.s 0 d 3 val [2, 3, 4, 5, 6, 7, 8, 9]
eliminate.s 0 d 4 val [2, 4, 5, 6, 7, 8, 9]
eliminate.s 0 d 5 val [2, 5, 6, 7, 8, 9]
eliminate.s 0 d 6 val [2, 6, 7, 8, 9]
eliminate.s 0 d 7 val [2, 7, 8, 9]
eliminate.s 0 d 8 val [2, 8, 9]
eliminate.s 0 d 9 val [2, 9]
 
The test program is supposed to remove ALL numbers from the list, apart from "2". In the wrong case, only odd numbers are removed.

Best regards,
Winfried
msg31711 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2007-04-05 21:09
This is a programming error.
In the "wrong" function, the "values" dict is mutated by the eliminate() function while it is iterated over.
This always gives unexpected results.
History
Date User Action Args
2022-04-11 14:56:23adminsetgithub: 44809
2007-04-05 20:59:26wplappercreate