codon
codon copied to clipboard
Segmentation fault in invoking list(zip(,))
The following code triggers a segmentation fault when invoking list(zip(c, c)).
test.py
a = ['1', '2', '3']
b = [1, 2, 3]
c = zip(b, b)
print(list(c))
print(list(zip(c, c)))
Reproduce:
'codon/codon-linux-x86_64/codon-deploy/bin/codon' run -release test.py
Crash message:
[(1, 1), (2, 2), (3, 3)]
Segmentation fault (core dumped)
Behavior on CPython 3.10.8:
[(1, 1), (2, 2), (3, 3)]
[]
Environment: codon: v0.15.5 on Feb 6 Ubuntu 18.04
Do you know why your second print give a result of [] ? not [((1,1),(1,1)),((2,2),(2,2)),((3,3),(3,3))]
In Python 3, zip() returns an iterator, and we can only use iterators once. After we have used the iterator, it is exhausted and there is nothing left. So the second print gives a result of '[]'. If you want to print [((1,1),(1,1)),((2,2),(2,2)),((3,3),(3,3))], try "list(zip(zip(b,b), zip(b,b)))".
In Python 3, zip() returns an iterator, and we can only use iterators once. After we have used the iterator, it is exhausted and there is nothing left. So the second print gives a result of '[]'. If you want to print [((1,1),(1,1)),((2,2),(2,2)),((3,3),(3,3))], try "list(zip(zip(b,b), zip(b,b)))".
So I think that's the reason why you got Segmentation fault,you want to store the empty thing to an array.
@xiaxinmeng It works as expected in Codon and in Python.
b = [1, 2, 3]
c = list(zip(b, b))
print(c)
a = list(zip(c, c))
print(len(a))
print(list(zip(c, c)))
prints:
[(1, 1), (2, 2), (3, 3)]
3
[((1, 1), (1, 1)), ((2, 2), (2, 2)), ((3, 3), (3, 3))]
Here is the bug:
b = [1, 2, 3]
c = zip(b, b)
print(list(c)) # list(c) exhausts the "c" generator: [(1, 1), (2, 2), (3, 3)]
print(list(zip(c, c))) # bug: trying to zip() an exhausted generator!
@arshajii In Python raises "StopIteration" exception, and in Codon program exists with SIGSEGV: Linux Segmentation Fault | Signal 11. Here is the MRE:
b = [1, 2, 3]
c = zip(b, b)
print(next(c)) # (1, 1)
print(list(c)) # [(2, 2), (3, 3)]
print(next(c)) # Segmentation fault (core dumped)