Skip to content

Commit 00d856f

Browse files
committed
Fix integer unmarshal and add set & frozenset
1 parent adbc338 commit 00d856f

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

marshal/marshal.go

+21-4
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ const (
5050
// Reads an object from the input
5151
func ReadObject(r io.Reader) (obj py.Object, err error) {
5252
var code byte
53+
defer func() { fmt.Printf("ReadObject(%c) returning %#v with error %v\n", code, obj, err) }()
5354
err = binary.Read(r, binary.LittleEndian, &code)
5455
if err != nil {
5556
return
5657
}
5758

5859
//flag := code & FLAG_REF
5960
Type := code &^ FLAG_REF
60-
fmt.Printf("Type = %q\n", Type)
6161

6262
switch Type {
6363
case TYPE_NULL:
@@ -83,8 +83,9 @@ func ReadObject(r io.Reader) (obj py.Object, err error) {
8383
var n int32
8484
err = binary.Read(r, binary.LittleEndian, &n)
8585
if err != nil {
86-
return py.Int64(n), nil
86+
return
8787
}
88+
return py.Int64(n), nil
8889
case TYPE_FLOAT:
8990
// Floating point number as a string
9091
var length uint8
@@ -203,8 +204,23 @@ func ReadObject(r io.Reader) (obj py.Object, err error) {
203204
return
204205
}
205206
}
206-
// FIXME differentiate the types TYPE_TUPLE, TYPE_LIST, TYPE_SET, TYPE_FROZENSET
207-
return py.Tuple(tuple), nil
207+
switch Type {
208+
case TYPE_TUPLE:
209+
return py.Tuple(tuple), nil
210+
case TYPE_LIST:
211+
return py.List(tuple), nil
212+
}
213+
214+
set := make(py.Set, len(tuple))
215+
for _, obj := range tuple {
216+
set[obj] = py.SetValue{}
217+
}
218+
switch Type {
219+
case TYPE_SET:
220+
return py.Set(set), nil
221+
case TYPE_FROZENSET:
222+
return py.FrozenSet(set), nil
223+
}
208224
case TYPE_DICT:
209225
// FIXME should be py.Dict
210226
dict := py.NewStringDict()
@@ -234,6 +250,7 @@ func ReadObject(r io.Reader) (obj py.Object, err error) {
234250
if err != nil {
235251
return
236252
}
253+
fmt.Printf("FIXME unimplemented TYPE_REF in unmarshal\n")
237254
// FIXME
238255
case TYPE_CODE:
239256
var argcount int32

py/py.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,24 @@ func NewStringDict() StringDict {
9191

9292
var SetType = NewType("set")
9393

94-
type Set map[Object]struct{}
94+
type SetValue struct{}
95+
96+
type Set map[Object]SetValue
9597

9698
// Type of this Set object
9799
func (o Set) Type() *Type {
98100
return SetType
99101
}
100102

103+
var FrozenSetType = NewType("frozenset")
104+
105+
type FrozenSet map[Object]SetValue
106+
107+
// Type of this FrozenSet object
108+
func (o FrozenSet) Type() *Type {
109+
return FrozenSetType
110+
}
111+
101112
type BigInt big.Int
102113

103114
var BigIntType = NewType("bigint")

0 commit comments

Comments
 (0)