Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unpickle dictionary with tuple as keys #4

Open
nicwest opened this issue Aug 8, 2015 · 7 comments
Open

unpickle dictionary with tuple as keys #4

nicwest opened this issue Aug 8, 2015 · 7 comments

Comments

@nicwest
Copy link

nicwest commented Aug 8, 2015

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!

@hydrogen18
Copy link
Owner

An edge case, but I can probably take a stab at it soon.

@nicwest
Copy link
Author

nicwest commented Aug 8, 2015

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?

@hydrogen18
Copy link
Owner

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
#4 (comment)
.

@nicwest
Copy link
Author

nicwest commented Aug 8, 2015

so it seems like slices are non comparable so they can't be used as keys in Go. So this might not be possible :(

@hydrogen18
Copy link
Owner

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.

@nicwest
Copy link
Author

nicwest commented Aug 9, 2015

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.

@hydrogen18
Copy link
Owner

After thinking about this more, I came up with a couple points

  1. The library shouldn't panic, like in your example.
  2. There are probably more corner cases around dictionary keys than just the one you found
  3. I need to refactor the way maps are built up while executing the pickle.
  4. We'll wind up with some special map like interface that allows keys such as this to be supported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants