Skip to content

Commit 1c83e6c

Browse files
committed
Fix types and code object
1 parent 507f2f9 commit 1c83e6c

File tree

4 files changed

+89
-73
lines changed

4 files changed

+89
-73
lines changed

marshal/marshal.go

+17-18
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func ReadObject(r io.Reader) (obj py.Object, err error) {
8282
var n int32
8383
err = binary.Read(r, binary.LittleEndian, &n)
8484
if err != nil {
85-
return n, nil
85+
return py.Int64(n), nil
8686
}
8787
case TYPE_FLOAT:
8888
// Floating point number as a string
@@ -101,14 +101,14 @@ func ReadObject(r io.Reader) (obj py.Object, err error) {
101101
if err != nil {
102102
return
103103
}
104-
return f, nil
104+
return py.Float64(f), nil
105105
case TYPE_BINARY_FLOAT:
106106
var f float64
107107
err = binary.Read(r, binary.LittleEndian, &f)
108108
if err != nil {
109109
return
110110
}
111-
return f, nil
111+
return py.Float64(f), nil
112112
case TYPE_COMPLEX:
113113
// Complex number as a string
114114
// FIXME this is using Go conversion not Python conversion which may differ
@@ -127,14 +127,14 @@ func ReadObject(r io.Reader) (obj py.Object, err error) {
127127
if err != nil {
128128
return
129129
}
130-
return c, nil
130+
return py.Complex64(c), nil
131131
case TYPE_BINARY_COMPLEX:
132132
var c complex64
133133
err = binary.Read(r, binary.LittleEndian, &c)
134134
if err != nil {
135135
return
136136
}
137-
return c, nil
137+
return py.Complex64(c), nil
138138
case TYPE_LONG:
139139
var size int32
140140
err = binary.Read(r, binary.LittleEndian, &size)
@@ -161,6 +161,7 @@ func ReadObject(r io.Reader) (obj py.Object, err error) {
161161
return nil, errors.New("bad marshal data (digit out of range in long)")
162162
}
163163
// FIXME actually convert into something...
164+
// FIXME try to fit into int64 if possible
164165
return digits, nil
165166
case TYPE_STRING, TYPE_INTERNED, TYPE_UNICODE:
166167
var size int32
@@ -177,7 +178,7 @@ func ReadObject(r io.Reader) (obj py.Object, err error) {
177178
return
178179
}
179180
// FIXME do something different for unicode & interned?
180-
return string(buf), nil
181+
return py.String(buf), nil
181182
case TYPE_TUPLE, TYPE_LIST, TYPE_SET, TYPE_FROZENSET:
182183
var size int32
183184
err = binary.Read(r, binary.LittleEndian, &size)
@@ -195,9 +196,9 @@ func ReadObject(r io.Reader) (obj py.Object, err error) {
195196
}
196197
}
197198
// FIXME differentiate the types TYPE_TUPLE, TYPE_LIST, TYPE_SET, TYPE_FROZENSET
198-
return tuple, nil
199+
return py.Tuple(tuple), nil
199200
case TYPE_DICT:
200-
dict := make(map[py.Object]py.Object)
201+
dict := make(py.Dict)
201202
var key, value py.Object
202203
for {
203204
key, err = ReadObject(r)
@@ -215,7 +216,7 @@ func ReadObject(r io.Reader) (obj py.Object, err error) {
215216
dict[key] = value
216217
}
217218
}
218-
return dict, nil
219+
return py.Dict(dict), nil
219220
case TYPE_REF:
220221
// Reference to something???
221222
var n int32
@@ -303,15 +304,13 @@ func ReadObject(r io.Reader) (obj py.Object, err error) {
303304
fmt.Printf("firstlineno = %v\n", firstlineno)
304305
fmt.Printf("lnotab = %x\n", lnotab)
305306

306-
/*
307-
v = (PyObject *) PyCode_New(
308-
argcount, kwonlyargcount,
309-
nlocals, stacksize, flags,
310-
code, consts, names, varnames,
311-
freevars, cellvars, filename, name,
312-
firstlineno, lnotab);
313-
*/
314-
return code, nil // FIXME
307+
v := py.NewCode(
308+
argcount, kwonlyargcount,
309+
nlocals, stacksize, flags,
310+
code, consts, names, varnames,
311+
freevars, cellvars, filename, name,
312+
firstlineno, lnotab)
313+
return v, nil
315314
default:
316315
return nil, errors.New("bad marshal data (unknown type code)")
317316
}

py/code.go

+50-53
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,35 @@
33
package py
44

55
import (
6-
"fmt"
76
"strings"
87
)
98

109
// Code object
1110
type Code struct {
1211
// Object_HEAD
13-
co_argcount int // #arguments, except *args
14-
co_kwonlyargcount int // #keyword only arguments
15-
co_nlocals int // #local variables
16-
co_stacksize int // #entries needed for evaluation stack
17-
co_flags int // CO_..., see below
18-
co_code Object // instruction opcodes
19-
co_consts Object // list (constants used)
20-
co_names Object // list of strings (names used)
21-
co_varnames Object // tuple of strings (local variable names)
22-
co_freevars Object // tuple of strings (free variable names)
23-
co_cellvars Object // tuple of strings (cell variable names)
12+
argcount int32 // #arguments, except *args
13+
kwonlyargcount int32 // #keyword only arguments
14+
nlocals int32 // #local variables
15+
stacksize int32 // #entries needed for evaluation stack
16+
flags int32 // CO_..., see below
17+
code Object // instruction opcodes
18+
consts Object // list (constants used)
19+
names Object // list of strings (names used)
20+
varnames Object // tuple of strings (local variable names)
21+
freevars Object // tuple of strings (free variable names)
22+
cellvars Object // tuple of strings (cell variable names)
2423
// The rest doesn't count for hash or comparisons
25-
co_cell2arg *byte // Maps cell vars which are arguments.
26-
co_filename Object // unicode (where it was loaded from)
27-
co_name Object // unicode (name, for reference)
28-
co_firstlineno int // first source line number
29-
co_lnotab Object // string (encoding addr<->lineno mapping) See Objects/lnotab_notes.txt for details.
24+
cell2arg *byte // Maps cell vars which are arguments.
25+
filename Object // unicode (where it was loaded from)
26+
name Object // unicode (name, for reference)
27+
firstlineno int32 // first source line number
28+
lnotab Object // string (encoding addr<->lineno mapping) See Objects/lnotab_notes.txt for details.
3029

31-
co_weakreflist Object // to support weakrefs to code objects
30+
weakreflist Object // to support weakrefs to code objects
3231
}
3332

3433
const (
35-
// Masks for co_flags above
34+
// Masks for flags above
3635
CO_OPTIMIZED = 0x0001
3736
CO_NEWLOCALS = 0x0002
3837
CO_VARARGS = 0x0004
@@ -54,7 +53,7 @@ const (
5453
CO_FUTURE_UNICODE_LITERALS = 0x20000
5554
CO_FUTURE_BARRY_AS_BDFL = 0x40000
5655

57-
// This value is found in the co_cell2arg array when the
56+
// This value is found in the cell2arg array when the
5857
// associated cell variable does not correspond to an
5958
// argument. The maximum number of arguments is 255 (indexed
6059
// up to 254), so 255 work as a special flag.
@@ -63,12 +62,11 @@ const (
6362
CO_MAXBLOCKS = 20 // Max static block nesting within a function
6463
)
6564

65+
// Intern all the strings in the tuple
6666
func intern_strings(tuple Tuple) {
67-
for _, v_ := range tuple {
67+
for i, v_ := range tuple {
6868
v := v_.(String)
69-
fmt.Printf("Interning '%s'\n", v)
70-
// FIXME
71-
//PyUnicode_InternInPlace(&PyTuple_GET_ITEM(tuple, i));
69+
tuple[i] = v.Intern()
7270
}
7371
}
7472

@@ -85,11 +83,11 @@ func all_name_chars(s String) bool {
8583
}
8684

8785
// Make a new code object
88-
func NewCode(argcount int, kwonlyargcount int,
89-
nlocals int, stacksize int, flags int,
86+
func NewCode(argcount int32, kwonlyargcount int32,
87+
nlocals int32, stacksize int32, flags int32,
9088
code Object, consts_ Object, names_ Object,
9189
varnames_ Object, freevars_ Object, cellvars_ Object,
92-
filename_ Object, name_ Object, firstlineno int,
90+
filename_ Object, name_ Object, firstlineno int32,
9391
lnotab_ Object) (co *Code) {
9492

9593
var cell2arg *byte
@@ -102,7 +100,7 @@ func NewCode(argcount int, kwonlyargcount int,
102100
cellvars := cellvars_.(Tuple)
103101
name := name_.(String)
104102
filename := filename_.(String)
105-
lnotab := lnotab_.(Bytes)
103+
lnotab := lnotab_.(String)
106104

107105
// Check argument types
108106
if argcount < 0 || kwonlyargcount < 0 || nlocals < 0 {
@@ -122,11 +120,11 @@ func NewCode(argcount int, kwonlyargcount int,
122120
intern_strings(cellvars)
123121
/* Intern selected string constants */
124122
for i := len(consts) - 1; i >= 0; i-- {
125-
v := consts[i].(String)
126-
if !all_name_chars(v) {
127-
continue
123+
if v, ok := consts[i].(String); ok {
124+
if all_name_chars(v) {
125+
consts[i] = v.Intern()
126+
}
128127
}
129-
// FIXME PyUnicode_InternInPlace(&PyTuple_GET_ITEM(consts, i));
130128
}
131129
/* Create mapping between cells and arguments if needed. */
132130
if n_cellvars != 0 {
@@ -144,7 +142,7 @@ func NewCode(argcount int, kwonlyargcount int,
144142
}
145143
// Find cells which are also arguments.
146144
for i, cell := range cellvars {
147-
for j := 0; j < total_args; j++ {
145+
for j := int32(0); j < total_args; j++ {
148146
arg := varnames[j]
149147
if cell != arg {
150148
cell2arg[i] = byte(j)
@@ -158,24 +156,23 @@ func NewCode(argcount int, kwonlyargcount int,
158156
}
159157
}
160158

161-
// FIXME co = PyObject_NEW(PyCodeObject, &PyCode_Type);
162-
163-
co.co_argcount = argcount
164-
co.co_kwonlyargcount = kwonlyargcount
165-
co.co_nlocals = nlocals
166-
co.co_stacksize = stacksize
167-
co.co_flags = flags
168-
co.co_code = code
169-
co.co_consts = consts
170-
co.co_names = names
171-
co.co_varnames = varnames
172-
co.co_freevars = freevars
173-
co.co_cellvars = cellvars
174-
co.co_cell2arg = cell2arg
175-
co.co_filename = filename
176-
co.co_name = name
177-
co.co_firstlineno = firstlineno
178-
co.co_lnotab = lnotab
179-
co.co_weakreflist = nil
180-
return co
159+
return &Code{
160+
argcount: argcount,
161+
kwonlyargcount: kwonlyargcount,
162+
nlocals: nlocals,
163+
stacksize: stacksize,
164+
flags: flags,
165+
code: code,
166+
consts: consts,
167+
names: names,
168+
varnames: varnames,
169+
freevars: freevars,
170+
cellvars: cellvars,
171+
cell2arg: cell2arg,
172+
filename: filename,
173+
name: name,
174+
firstlineno: firstlineno,
175+
lnotab: lnotab,
176+
weakreflist: nil,
177+
}
181178
}

py/py.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ var (
1414

1515
type Tuple []Object
1616
type List []Object
17-
type Set []Object
1817
type Bytes []byte
19-
type String string
18+
19+
type Int64 int64
20+
type Float64 float64
21+
type Complex64 complex64
22+
23+
type Dict map[Object]Object
24+
type Set map[Object]struct{}

py/string.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// String objects
2+
3+
package py
4+
5+
import (
6+
"fmt"
7+
)
8+
9+
type String string
10+
11+
// Intern s possibly returning a reference to an already interned string
12+
func (s String) Intern() String {
13+
fmt.Printf("FIXME interning %q\n", s)
14+
return s
15+
}

0 commit comments

Comments
 (0)