This repository has been archived by the owner on Jul 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
object: add PyObject_Call for args,kwargs
wrap PyObject_Call to handle functions with positional and keyword arguments. Fixes #30.
- Loading branch information
Showing
3 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |