stalecucumber
stalecucumber copied to clipboard
unpickle dictionary with tuple as keys
Hi!
I'm trying to unpickle a dictionary with tuples as keys and I'm getting the following error:
panic: runtime error: hash of unhashable type []interface {}
goroutine 1 [running]:
github.com/hydrogen18/stalecucumber.(*PickleMachine).opcode_SETITEMS(0xc208036000, 0x0, 0x0)
/home/nic/go/src/github.com/hydrogen18/stalecucumber/protocol_1.go:298 +0x461
github.com/hydrogen18/stalecucumber.(*PickleMachine).execute(0xc208036000, 0x0, 0x0)
/home/nic/go/src/github.com/hydrogen18/stalecucumber/pickle_machine.go:345 +0x127
github.com/hydrogen18/stalecucumber.Unpickle(0x7f0adb1be9f8, 0xc20804c018, 0x0, 0x0, 0x0, 0x0)
/home/nic/go/src/github.com/hydrogen18/stalecucumber/pickle_machine.go:273 +0x152
main.main()
/home/nic/go/src/github.com/nicwest/things/pick.go:21 +0x169
goroutine 2 [runnable]:
runtime.forcegchelper()
/home/nic/src/go/src/runtime/proc.go:90
runtime.goexit()
/home/nic/src/go/src/runtime/asm_amd64.s:2232 +0x1
goroutine 3 [runnable]:
runtime.bgsweep()
/home/nic/src/go/src/runtime/mgc0.go:82
runtime.goexit()
/home/nic/src/go/src/runtime/asm_amd64.s:2232 +0x1
goroutine 4 [runnable]:
runtime.runfinq()
/home/nic/src/go/src/runtime/malloc.go:712
runtime.goexit()
/home/nic/src/go/src/runtime/asm_amd64.s:2232 +0x1
import cPickle
with open('/home/nic/things/p.pickle', 'wb') as fh:
cPickle.dump({(2, 1234): 'foobar', (2, 5678): 'pewpew'}, fh)
with open('/home/nic/things/p.pickle', 'rb') as fh:
print cPickle.load(fh)
{(2, 5678): 'pewpew', (2, 1234): 'foobar'}
code:
package main
import (
"fmt"
"github.com/hydrogen18/stalecucumber"
"log"
"os"
)
func main() {
p := "/home/nic/things/p.pickle"
data, err := os.Open(p)
if err != nil {
log.Fatal(err)
}
unpickedData, err := stalecucumber.DictString(stalecucumber.Unpickle(data))
if err != nil {
log.Fatal(err)
}
fmt.Println(unpickedData)
}
Probably a bit of an edge case though!
An edge case, but I can probably take a stab at it soon.
I'm fairly new to Go, but would you accept a PR if I got it working?
I assume that the tuple type doesn't have a hashing function, and this is causing the problem?
Sure. Give it a go. Pun intended. On Aug 8, 2015 2:18 PM, "Nic West" [email protected] wrote:
I'm fairly new to Go, but would you accept a PR if I got it working?
I assume that the tuple type doesn't have a hashing function, and this is causing the problem?
— Reply to this email directly or view it on GitHub https://github.com/hydrogen18/stalecucumber/issues/4#issuecomment-129039175 .
so it seems like slices are non comparable so they can't be used as keys in Go. So this might not be possible :(
It's definitely doable, its just a matter of coming up with the right abstraction since this isn't supported by Golang very well in its native state.
I thought about copying the slice to an array, then it becomes immutable (which fits quite nicely with tuples) and comparable, the problem is you cant create an array of variable length.
Also I thought about just returning a string representation of the python tuple and that seems to work, but that's not exactly ideal.
After thinking about this more, I came up with a couple points
- The library shouldn't panic, like in your example.
- There are probably more corner cases around dictionary keys than just the one you found
- I need to refactor the way maps are built up while executing the pickle.
- We'll wind up with some special
map
like interface that allows keys such as this to be supported.