3
3
package py
4
4
5
5
import (
6
- "fmt"
7
6
"strings"
8
7
)
9
8
10
9
// Code object
11
10
type Code struct {
12
11
// 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)
24
23
// 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.
30
29
31
- co_weakreflist Object // to support weakrefs to code objects
30
+ weakreflist Object // to support weakrefs to code objects
32
31
}
33
32
34
33
const (
35
- // Masks for co_flags above
34
+ // Masks for flags above
36
35
CO_OPTIMIZED = 0x0001
37
36
CO_NEWLOCALS = 0x0002
38
37
CO_VARARGS = 0x0004
@@ -54,7 +53,7 @@ const (
54
53
CO_FUTURE_UNICODE_LITERALS = 0x20000
55
54
CO_FUTURE_BARRY_AS_BDFL = 0x40000
56
55
57
- // This value is found in the co_cell2arg array when the
56
+ // This value is found in the cell2arg array when the
58
57
// associated cell variable does not correspond to an
59
58
// argument. The maximum number of arguments is 255 (indexed
60
59
// up to 254), so 255 work as a special flag.
@@ -63,12 +62,11 @@ const (
63
62
CO_MAXBLOCKS = 20 // Max static block nesting within a function
64
63
)
65
64
65
+ // Intern all the strings in the tuple
66
66
func intern_strings (tuple Tuple ) {
67
- for _ , v_ := range tuple {
67
+ for i , v_ := range tuple {
68
68
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 ()
72
70
}
73
71
}
74
72
@@ -85,11 +83,11 @@ func all_name_chars(s String) bool {
85
83
}
86
84
87
85
// 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 ,
90
88
code Object , consts_ Object , names_ Object ,
91
89
varnames_ Object , freevars_ Object , cellvars_ Object ,
92
- filename_ Object , name_ Object , firstlineno int ,
90
+ filename_ Object , name_ Object , firstlineno int32 ,
93
91
lnotab_ Object ) (co * Code ) {
94
92
95
93
var cell2arg * byte
@@ -102,7 +100,7 @@ func NewCode(argcount int, kwonlyargcount int,
102
100
cellvars := cellvars_ .(Tuple )
103
101
name := name_ .(String )
104
102
filename := filename_ .(String )
105
- lnotab := lnotab_ .(Bytes )
103
+ lnotab := lnotab_ .(String )
106
104
107
105
// Check argument types
108
106
if argcount < 0 || kwonlyargcount < 0 || nlocals < 0 {
@@ -122,11 +120,11 @@ func NewCode(argcount int, kwonlyargcount int,
122
120
intern_strings (cellvars )
123
121
/* Intern selected string constants */
124
122
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
+ }
128
127
}
129
- // FIXME PyUnicode_InternInPlace(&PyTuple_GET_ITEM(consts, i));
130
128
}
131
129
/* Create mapping between cells and arguments if needed. */
132
130
if n_cellvars != 0 {
@@ -144,7 +142,7 @@ func NewCode(argcount int, kwonlyargcount int,
144
142
}
145
143
// Find cells which are also arguments.
146
144
for i , cell := range cellvars {
147
- for j := 0 ; j < total_args ; j ++ {
145
+ for j := int32 ( 0 ) ; j < total_args ; j ++ {
148
146
arg := varnames [j ]
149
147
if cell != arg {
150
148
cell2arg [i ] = byte (j )
@@ -158,24 +156,23 @@ func NewCode(argcount int, kwonlyargcount int,
158
156
}
159
157
}
160
158
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
+ }
181
178
}
0 commit comments