8000 object: add PyObject_Call for args,kwargs · foundations/go-python@7074fff · GitHub
[go: up one dir, main page]

Skip to content

Commit 7074fff

Browse files
committed
object: add PyObject_Call for args,kwargs
wrap PyObject_Call to handle functions with positional and keyword arguments. Fixes sbinet#30.
1 parent 91d8601 commit 7074fff

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

object.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,13 @@ func (self *PyObject) Check_Callable() bool {
291291
return int2bool(C.PyCallable_Check(self.ptr))
292292
}
293293

294+
// PyObject* PyObject_Call(PyObject *callable_object, PyObject *args, PyObject *kw)
295+
// Return value: New reference.
296+
// Call a callable Python object callable_object, with arguments given by the tuple args, and named arguments given by the dictionary kw. If no named arguments are needed, kw may be NULL. args must not be NULL, use an empty tuple if no arguments are needed. Returns the result of the call on success, or NULL on failure. This is the equivalent of the Python expression apply(callable_object, args, kw) or callable_object(*args, **kw).
297+
func (self *PyObject) Call(args, kw *PyObject) *PyObject {
298+
return togo(C.PyObject_Call(self.ptr, args.ptr, kw.ptr))
299+
}
300+
294301
// PyObject* PyObject_CallObject(PyObject *callable_object, PyObject *args)
295302
// Return value: New reference.
296303
// Call a callable Python object callable_object, with arguments given by the tuple args. If no arguments are needed, then args may be NULL. Returns the result of the call on success, or NULL on failure. This is the equivalent of the Python expression apply(callable_object, args) or callable_object(*args).

tests/kw-args/kwargs.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from __future__ import print_function
2+
3+
def foo(*args, **kwargs):
4+
s = "args=%s kwds=%s" % (args,kwargs)
5+
return s
6+
7+
if __name__ == "__main__":
8+
print(foo())
9+
print(foo(a=3))
10+
kw=dict(a=3)
11+
print(foo(**kw))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/sbinet/go-python"
8+
)
9+
10+
func init() {
11+
err := python.Initialize()
12+
if err != nil {
13+
panic(err.Error())
14+
}
15+
}
16+
17+
func main() {
18+
fmt.Printf("importing kwargs...\n")
19+
m := python.PyImport_ImportModule("kwargs")
20+
if m == nil {
21+
log.Fatalf("could not import 'kwargs'\n")
22+
}
23+
24+
foo := m.GetAttrString("foo")
25+
if foo == nil {
26+
log.Fatalf("could not getattr(kwargs, 'foo')\n")
27+
}
28+
29+
out := foo.CallFunction()
30+
if out == nil {
31+
log.Fatalf("error calling foo()\n")
32+
}
33+
str := python.PyString_AsString(out)
34+
fmt.Printf("%s\n", str)
35+
want := "args=() kwds={}"
36+
if str != want {
37+
log.Fatalf("error. got=%q want=%q\n", str, want)
38+
}
39+
40+
// keyword arguments
41+
kw := python.PyDict_New()
42+
err := python.PyDict_SetItem(
43+
kw,
44+
python.PyString_FromString("a"),
45+
python.PyInt_FromLong(3),
46+
)
47+
if err != nil {
48+
log.Fatalf("error: %v\n", err)
49+
}
50+
51+
args := python.PyTuple_New(0)
52+
out = foo.Call(args, kw)
53+
if out == nil {
54+
log.Fatalf("error calling foo(*args, **kwargs)\n")
55+
}
56+
57+
str = python.PyString_AsString(out)
58+
fmt.Printf("%s\n", str)
59+
want = "args=() kwds={'a': 3}"
60+
if str != want {
61+
log.Fatalf("error. got=%q. want=%q\n", str, want)
62+
}
63+
}
0