Skip to content
This repository has been archived by the owner on Jul 24, 2023. It is now read-only.

Commit

Permalink
object: add PyObject_Call for args,kwargs
Browse files Browse the repository at this point in the history
wrap PyObject_Call to handle functions with positional and keyword arguments.

Fixes #30.
  • Loading branch information
sbinet committed Jan 4, 2016
1 parent 91d8601 commit 595aa16
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
7 changes: 7 additions & 0 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,13 @@ func (self *PyObject) Check_Callable() bool {
return int2bool(C.PyCallable_Check(self.ptr))
}

// PyObject* PyObject_Call(PyObject *callable_object, PyObject *args, PyObject *kw)
// Return value: New reference.
// 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).
func (self *PyObject) Call(args, kw *PyObject) *PyObject {
return togo(C.PyObject_Call(self.ptr, args.ptr, kw.ptr))
}

// PyObject* PyObject_CallObject(PyObject *callable_object, PyObject *args)
// Return value: New reference.
// 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).
Expand Down
11 changes: 11 additions & 0 deletions tests/kw-args/kwargs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from __future__ import print_function

def foo(*args, **kwargs):
s = "args=%s kwds=%s" % (args,kwargs)
return s

if __name__ == "__main__":
print(foo())
print(foo(a=3))
kw=dict(a=3)
print(foo(**kw))
63 changes: 63 additions & 0 deletions tests/kw-args/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package main

import (
"fmt"
"log"

"github.com/sbinet/go-python"
)

func init() {
err := python.Initialize()
if err != nil {
panic(err.Error())
}
}

func main() {
fmt.Printf("importing kwargs...\n")
m := python.PyImport_ImportModule("kwargs")
if m == nil {
log.Fatalf("could not import 'kwargs'\n")
}

foo := m.GetAttrString("foo")
if foo == nil {
log.Fatalf("could not getattr(kwargs, 'foo')\n")
}

out := foo.CallFunction()
if out == nil {
log.Fatalf("error calling foo()\n")
}
str := python.PyString_AsString(out)
fmt.Printf("%s\n", str)
want := "args=() kwds={}"
if str != want {
log.Fatalf("error. got=%q want=%q\n", str, want)
}

// keyword arguments
kw := python.PyDict_New()
err := python.PyDict_SetItem(
kw,
python.PyString_FromString("a"),
python.PyInt_FromLong(3),
)
if err != nil {
log.Fatalf("error: %v\n", err)
}

args := python.PyList_New(0)
out = foo.Call(args, kw)
if out == nil {
log.Fatalf("error calling foo(**kwargs)\n")
}

str = python.PyString_AsString(out)
fmt.Printf("%s\n", str)
want = "args=() kwds={'a': 3}"
if str != want {
log.Fatalf("error. got=%q. want=%q\n", str, want)
}
}

0 comments on commit 595aa16

Please sign in to comment.