Skip to content

Commit

Permalink
Merge pull request #9 from sbinet-xyz/issue-2
Browse files Browse the repository at this point in the history
all: add support for unpickling array.array values
  • Loading branch information
matteo-grella authored Nov 20, 2023
2 parents 40d0170 + 0577853 commit 7bc7213
Show file tree
Hide file tree
Showing 5 changed files with 365 additions and 1 deletion.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
module github.com/nlpodyssey/gopickle

go 1.15

require golang.org/x/text v0.14.0
32 changes: 32 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
6 changes: 6 additions & 0 deletions pickle/pickle.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ func (u *Unpickler) findClass(module, name string) (interface{}, error) {
case "object":
return &types.ObjectClass{}, nil
}
case "array":
switch name {
case "_array_reconstructor":
return &types.Array{}, nil
}
case "copy_reg":
switch name {
case "_reconstructor":
Expand All @@ -145,6 +150,7 @@ func (u *Unpickler) findClass(module, name string) (interface{}, error) {
}
return types.NewGenericClass(module, name), nil
}

func (u *Unpickler) read(n int) ([]byte, error) {
buf := make([]byte, n)

Expand Down
107 changes: 106 additions & 1 deletion pickle/pickle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ package pickle

import (
"fmt"
"github.com/nlpodyssey/gopickle/types"
"math/big"
"reflect"
"strings"
"testing"

"github.com/nlpodyssey/gopickle/types"
)

func TestNoneP1(t *testing.T) {
Expand Down Expand Up @@ -649,6 +650,110 @@ func TestFindClass(t *testing.T) {
}
}

func TestP4Carray(t *testing.T) {
for _, tc := range []struct {
name string
pkl string
want interface{}
}{
{
// pickle.dumps(array.array("b", [0,1,2,-3], protocol=4)
name: "b",
pkl: "\x80\x04\x95F\x00\x00\x00\x00\x00\x00\x00\x8c\x05array\x94\x8c\x14_array_reconstructor\x94\x93\x94(\x8c\x05array\x94\x8c\x05array\x94\x93\x94\x8c\x01b\x94K\x01C\x04\x00\x01\x02\xfd\x94t\x94R\x94.",
want: []int8{0, 1, 2, -3},
},
{
// pickle.dumps(array.array("h", [0,1,2,-3], protocol=4)
name: "h",
pkl: "\x80\x04\x95J\x00\x00\x00\x00\x00\x00\x00\x8c\x05array\x94\x8c\x14_array_reconstructor\x94\x93\x94(\x8c\x05array\x94\x8c\x05array\x94\x93\x94\x8c\x01h\x94K\x04C\x08\x00\x00\x01\x00\x02\x00\xfd\xff\x94t\x94R\x94.",
want: []int16{0, 1, 2, -3},
},
{
// pickle.dumps(array.array("i", [0,1,2,-3], protocol=4)
name: "i",
pkl: "\x80\x04\x95R\x00\x00\x00\x00\x00\x00\x00\x8c\x05array\x94\x8c\x14_array_reconstructor\x94\x93\x94(\x8c\x05array\x94\x8c\x05array\x94\x93\x94\x8c\x01i\x94K\x08C\x10\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\xfd\xff\xff\xff\x94t\x94R\x94.",
want: []int32{0, 1, 2, -3},
},
{
// pickle.dumps(array.array("l", [0,1,2,-3], protocol=4)
name: "l",
pkl: "\x80\x04\x95b\x00\x00\x00\x00\x00\x00\x00\x8c\x05array\x94\x8c\x14_array_reconstructor\x94\x93\x94(\x8c\x05array\x94\x8c\x05array\x94\x93\x94\x8c\x01l\x94K\x0cC \x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\xfd\xff\xff\xff\xff\xff\xff\xff\x94t\x94R\x94.",
want: []int64{0, 1, 2, -3},
},
{
// pickle.dumps(array.array("q", [0,1,2,-3], protocol=4)
name: "q",
pkl: "\x80\x04\x95b\x00\x00\x00\x00\x00\x00\x00\x8c\x05array\x94\x8c\x14_array_reconstructor\x94\x93\x94(\x8c\x05array\x94\x8c\x05array\x94\x93\x94\x8c\x01q\x94K\x0cC \x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\xfd\xff\xff\xff\xff\xff\xff\xff\x94t\x94R\x94.",
want: []int64{0, 1, 2, -3},
},
{
// pickle.dumps(array.array("B", [0,1,2,3], protocol=4)
name: "B",
pkl: "\x80\x04\x95F\x00\x00\x00\x00\x00\x00\x00\x8c\x05array\x94\x8c\x14_array_reconstructor\x94\x93\x94(\x8c\x05array\x94\x8c\x05array\x94\x93\x94\x8c\x01B\x94K\x00C\x04\x00\x01\x02\x03\x94t\x94R\x94.",
want: []uint8{0, 1, 2, 3},
},
{
// pickle.dumps(array.array("H", [0,1,2,3], protocol=4)
name: "H",
pkl: "\x80\x04\x95J\x00\x00\x00\x00\x00\x00\x00\x8c\x05array\x94\x8c\x14_array_reconstructor\x94\x93\x94(\x8c\x05array\x94\x8c\x05array\x94\x93\x94\x8c\x01H\x94K\x02C\x08\x00\x00\x01\x00\x02\x00\x03\x00\x94t\x94R\x94.",
want: []uint16{0, 1, 2, 3},
},
{
// pickle.dumps(array.array("I", [0,1,2,3], protocol=4)
name: "I",
pkl: "\x80\x04\x95R\x00\x00\x00\x00\x00\x00\x00\x8c\x05array\x94\x8c\x14_array_reconstructor\x94\x93\x94(\x8c\x05array\x94\x8c\x05array\x94\x93\x94\x8c\x01I\x94K\x06C\x10\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x94t\x94R\x94.",
want: []uint32{0, 1, 2, 3},
},
{
// pickle.dumps(array.array("L", [0,1,2,3], protocol=4)
name: "L",
pkl: "'\x80\x04\x95b\x00\x00\x00\x00\x00\x00\x00\x8c\x05array\x94\x8c\x14_array_reconstructor\x94\x93\x94(\x8c\x05array\x94\x8c\x05array\x94\x93\x94\x8c\x01L\x94K\nC \x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x94t\x94R\x94.",
want: []uint64{0, 1, 2, 3},
},
{
// pickle.dumps(array.array("Q", [0,1,2,3], protocol=4)
name: "Q",
pkl: "'\x80\x04\x95b\x00\x00\x00\x00\x00\x00\x00\x8c\x05array\x94\x8c\x14_array_reconstructor\x94\x93\x94(\x8c\x05array\x94\x8c\x05array\x94\x93\x94\x8c\x01Q\x94K\nC \x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x94t\x94R\x94.",
want: []uint64{0, 1, 2, 3},
},
{
// pickle.dumps(array.array("f", [0,1,2,3], protocol=4)
name: "f",
pkl: "\x80\x04\x95R\x00\x00\x00\x00\x00\x00\x00\x8c\x05array\x94\x8c\x14_array_reconstructor\x94\x93\x94(\x8c\x05array\x94\x8c\x05array\x94\x93\x94\x8c\x01f\x94K\x0eC\x10\x00\x00\x00\x00\x00\x00\x80?\x00\x00\x00@\x00\x00@@\x94t\x94R\x94.",
want: []float32{0, 1, 2, 3},
},
{
// pickle.dumps(array.array("d", [0,1,2,3], protocol=4)
name: "d",
pkl: "\x80\x04\x95b\x00\x00\x00\x00\x00\x00\x00\x8c\x05array\x94\x8c\x14_array_reconstructor\x94\x93\x94(\x8c\x05array\x94\x8c\x05array\x94\x93\x94\x8c\x01d\x94K\x10C \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x08@\x94t\x94R\x94.",
want: []float64{0, 1, 2, 3},
},
{
// pickle.dumps(array.array("u", "Hello, 世界".encode("utf-32")], protocol=4)
name: "u",
pkl: "\x80\x04\x95f\x00\x00\x00\x00\x00\x00\x00\x8c\x05array\x94\x8c\x14_array_reconstructor\x94\x93\x94(\x8c\x05array\x94\x8c\x05array\x94\x93\x94\x8c\x01u\x94K\x14C$H\x00\x00\x00e\x00\x00\x00l\x00\x00\x00l\x00\x00\x00o\x00\x00\x00,\x00\x00\x00 \x00\x00\x00\x16N\x00\x00Lu\x00\x00\x94t\x94R\x94.",
want: []rune("Hello, 世界"),
},
{
// pickle.dumps(array.array("u", "".encode("utf-32")], protocol=4)
name: "u-empty",
pkl: "\x80\x04\x95B\x00\x00\x00\x00\x00\x00\x00\x8c\x05array\x94\x8c\x14_array_reconstructor\x94\x93\x94(\x8c\x05array\x94\x8c\x05array\x94\x93\x94\x8c\x01u\x94K\x14C\x00\x94t\x94R\x94.",
want: []rune(""),
},
} {
t.Run(tc.name, func(t *testing.T) {
switch tc.name {
case "L", "Q":
t.SkipNow() // unknown opcode: 0x27 '''
}
got := loadsNoErr(t, tc.pkl)
if !reflect.DeepEqual(got, tc.want) {
t.Fatalf("got=%v, want=%v", got, tc.want)
}
})
}
}

// TODO: test BinPersId
// TODO: test Get
// TODO: test BinGet
Expand Down
Loading

0 comments on commit 7bc7213

Please sign in to comment.