Skip to content

Commit 0acb046

Browse files
committed
compile: Fix Varnames
1 parent c7d19e1 commit 0acb046

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

compile/compile.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func (c *compiler) Index(Id string, Names *[]string) uint32 {
223223
}
224224
}
225225
*Names = append(*Names, Id)
226-
return uint32(len(c.Code.Names) - 1)
226+
return uint32(len(*Names) - 1)
227227
}
228228

229229
// Compiles a python name
@@ -307,18 +307,18 @@ func (c *compiler) Stmt(stmt ast.Stmt) {
307307

308308
// Arguments
309309
for _, arg := range node.Args.Args {
310-
code.Varnames = append(code.Varnames, string(arg.Arg))
310+
c.Index(string(arg.Arg), &code.Varnames)
311311
}
312312
for _, arg := range node.Args.Kwonlyargs {
313-
code.Varnames = append(code.Varnames, string(arg.Arg))
313+
c.Index(string(arg.Arg), &code.Varnames)
314314
}
315315
if node.Args.Vararg != nil {
316316
code.Nlocals++
317-
code.Varnames = append(code.Varnames, string(node.Args.Vararg.Arg))
317+
c.Index(string(node.Args.Vararg.Arg), &code.Varnames)
318318
}
319319
if node.Args.Kwarg != nil {
320320
code.Nlocals++
321-
code.Varnames = append(code.Varnames, string(node.Args.Kwarg.Arg))
321+
c.Index(string(node.Args.Kwarg.Arg), &code.Varnames)
322322
code.Flags |= py.CO_VARKEYWORDS
323323
}
324324

compile/compile_data_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -2182,4 +2182,37 @@ var compileTestData = []struct {
21822182
Firstlineno: 1,
21832183
Lnotab: "",
21842184
}, nil, ""},
2185+
{"def fn(a,b): a+b+c+d", "exec", &py.Code{
2186+
Argcount: 0,
2187+
Kwonlyargcount: 0,
2188+
Nlocals: 0,
2189+
Stacksize: 2,
2190+
Flags: 64,
2191+
Code: "\x64\x00\x00\x64\x01\x00\x84\x00\x00\x5a\x00\x00\x64\x02\x00\x53",
2192+
Consts: []py.Object{&py.Code{
2193+
Argcount: 2,
2194+
Kwonlyargcount: 0,
2195+
Nlocals: 2,
2196+
Stacksize: 2,
2197+
Flags: 67,
2198+
Code: "\x7c\x00\x00\x7c\x01\x00\x17\x74\x00\x00\x17\x74\x01\x00\x17\x01\x64\x00\x00\x53",
2199+
Consts: []py.Object{py.None},
2200+
Names: []string{"c", "d"},
2201+
Varnames: []string{"a", "b"},
2202+
Freevars: []string{},
2203+
Cellvars: []string{},
2204+
Filename: "<string>",
2205+
Name: "fn",
2206+
Firstlineno: 1,
2207+
Lnotab: "",
2208+
}, py.String("fn"), py.None},
2209+
Names: []string{"fn"},
2210+
Varnames: []string{},
2211+
Freevars: []string{},
2212+
Cellvars: []string{},
2213+
Filename: "<string>",
2214+
Name: "<module>",
2215+
Firstlineno: 1,
2216+
Lnotab: "",
2217+
}, nil, ""},
21852218
}

compile/make_compile_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@
164164
('''def fn(a,*arg,b=1,c=2,**kwargs): pass''', "exec"),
165165
('''def fn(a:"a",*arg:"arg",b:"b"=1,c:"c"=2,**kwargs:"kw") -> "ret": pass''', "exec"),
166166
('''def fn(): a+b''', "exec"),
167-
#('''def fn(a,b): a+b+c+d''', "exec"),
167+
('''def fn(a,b): a+b+c+d''', "exec"),
168168

169169
]
170170

0 commit comments

Comments
 (0)