go-python / gpython

gpython is a python interpreter written in go "batteries not included"
BSD 3-Clause "New" or "Revised" License
870 stars 95 forks source link

transforming generators into list trigger crashing #208

Open xiaxinmeng opened 1 year ago

xiaxinmeng commented 1 year ago

In the following example, we initialize a 'population' with a list then we define a generator 'gen'. When we transform the generator into list, it crashes gpython with message "panic: runtime error: makeslice: len out of range".

test.py

population = list(range(10))
gen = (-1 * population for w in range(10))
list(gen)

Output on go/wasm(https://gpython.org/?wasm):

Gpython 3.4.0 running in your browser with go/wasm
>>> population = list(range(10))
>>> gen = (-1 * population for w in range(10))
>>> list(gen)
panic: runtime error: makeslice: len out of range
goroutine 5 [running]:
github.com/go-python/gpython/py.NewListSized(...)
    /home/ncw/go/src/github.com/go-python/gpython/py/list.go:52
github.com/go-python/gpython/py.(*List).M__mul__(0xc05ea40, 0x9d6e0, 0xc01e660, 0x3eca0, 0x59a80, 0xffffffffffffff01, 0x2c932218)
    /home/ncw/go/src/github.com/go-python/gpython/py/list.go:239 +0x13
github.com/go-python/gpython/py.(*List).M__rmul__(0xc05ea40, 0x9d6e0, 0xc01e660, 0x2c932218, 0xc05ea40, 0x1, 0x0)
    /home/ncw/go/src/github.com/go-python/gpython/py/list.go:249 +0x2
github.com/go-python/gpython/py.Mul(0x9d6e0, 0xc01e660, 0x9d340, 0xc05ea40, 0xc0de420, 0xc01e660, 0x0, 0x0)
    /home/ncw/go/src/github.com/go-python/gpython/py/arithmetic.go:274 +0x14
github.com/go-python/gpython/vm.do_BINARY_MULTIPLY(0xc074a10, 0x0, 0x0, 0x0)
    /home/ncw/go/src/github.com/go-python/gpython/vm/eval.go:248 +0xc
github.com/go-python/gpython/vm.RunFrame(0xc0de420, 0x4a408, 0x4a3f8, 0x10860032, 0x366168)
    /home/ncw/go/src/github.com/go-python/gpython/vm/eval.go:1785 +0x3f
github.com/go-python/gpython/py.(*Generator).Send(0xc0632f0, 0x9d720, 0x3661e8, 0x3e720, 0x4a3c0, 0x1, 0x2c9321b8)
    /home/ncw/go/src/github.com/go-python/gpython/py/generator.go:100 +0xb
github.com/go-python/gpython/py.(*Generator).M__next__(0xc0632f0, 0x9d300, 0xc0632f0, 0x2c9321b8, 0xc0632f0)
    /home/ncw/go/src/github.com/go-python/gpython/py/generator.go:71 +0x2

output with gopherjs(https://gpython.org/):

[USER]: https://gpython.org/gpython.js: runtime error: makeslice: len out of range
$callDeferred@https://gpython.org/gpython.js:4:22511
$panic@https://gpython.org/gpython.js:4:22957
AK@https://gpython.org/gpython.js:10:2429
$makeSlice@https://gpython.org/gpython.js:4:20053
GF@https://gpython.org/gpython.js:41:276509
GB.ptr.prototype.M__mul__@https://gpython.org/gpython.js:41:286563
GB.ptr.prototype.M__rmul__@https://gpython.org/gpython.js:41:286854
AG@https://gpython.org/gpython.js:41:47063
W@https://gpython.org/gpython.js:44:19407
DO@https://gpython.org/gpython.js:44:91592
EJ.ptr.prototype.Send@https://gpython.org/gpython.js:41:212721
EJ.ptr.prototype.M__next__@https://gpython.org/gpython.js:41:211418
LD@https://gpython.org/gpython.js:41:316574
LE@https://gpython.org/gpython.js:41:319732
GB.ptr.prototype.ExtendSequence@https://gpython.org/gpython.js:41:277441
LC@https://gpython.org/gpython.js:41:315768
GC@https://gpython.org/gpython.js:41:276033
MH.ptr.prototype.M__call__@https://gpython.org/gpython.js:41:366934
FK@https://gpython.org/gpython.js:41:255589
DN@https://gpython.org/gpython.js:44:82459
EE.ptr.prototype.Call@https://gpython.org/gpython.js:44:87109
DC@https://gpython.org/gpython.js:44:77098
DO@https://gpython.org/gpython.js:44:91592
DU@https://gpython.org/gpython.js:44:107450
DW@https://gpython.org/gpython.js:44:109015
G.ptr.prototype.Run@https://gpython.org/gpython.js:56:3575
$b@https://gpython.org/gpython.js:60:3908
$b@https://gpython.org/gpython.js:59:2625
r@https://gpython.org/gpython.js:4:23443
$runScheduled@https://gpython.org/gpython.js:4:24007
$schedule@https://gpython.org/gpython.js:4:24184
$go@https://gpython.org/gpython.js:4:23907
I/$packages["github.com/gopherjs/gopherwasm/js"]<@https://gpython.org/gpython.js:59:2240
$externalizeFunction/e.$externalizeWrapper@https://gpython.org/gpython.js:4:28925
a@https://cdnjs.cloudflare.com/ajax/libs/jquery.terminal/1.23.2/js/jquery.terminal.min.js:40:82615
k@https://cdnjs.cloudflare.com/ajax/libs/jquery.terminal/1.23.2/js/jquery.terminal.min.js:40:83463
ENTER@https://cdnjs.cloudflare.com/ajax/libs/jquery.terminal/1.23.2/js/jquery.terminal.min.js:40:14915
$e@https://cdnjs.cloudflare.com/ajax/libs/jquery.terminal/1.23.2/js/jquery.terminal.min.js:40:28362
dispatch@https://code.jquery.com/jquery-latest.js:4641:9
add/elemData.handle@https://code.jquery.com/jquery-latest.js:4309:28
ncw commented 1 year ago

This one actually works for me with gpython master

$ gpython
Python 3.4.0 (none, unknown)
[Gpython dev]
- os/arch: linux/amd64
- go version: go1.19.1
>>> population = list(range(10))
>>> gen = (-1 * population for w in range(10))
>>> list(gen)
[[], [], [], [], [], [], [], [], [], []]
>>>