Skip to content

Commit fbcae47

Browse files
committed
Support int parameters for Python functions
1 parent 166a22d commit fbcae47

File tree

3 files changed

+36
-14
lines changed

3 files changed

+36
-14
lines changed

lib/Inline/Python.pm6

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,21 @@ sub py_string_check(OpaquePointer)
3737
sub py_int_as_long(OpaquePointer)
3838
returns Int { ... }
3939
native(&py_int_as_long);
40+
sub py_int_to_py(Int)
41+
returns OpaquePointer { ... }
42+
native(&py_int_to_py);
4043
sub py_unicode_to_char_star(OpaquePointer)
4144
returns Str { ... }
4245
native(&py_unicode_to_char_star);
4346
sub py_string_to_buf(OpaquePointer, CArray[CArray[int8]])
4447
returns Int { ... }
4548
native(&py_string_to_buf);
49+
sub py_tuple_new(Int)
50+
returns OpaquePointer { ... }
51+
native(&py_tuple_new);
52+
sub py_tuple_set_item(OpaquePointer, Int, OpaquePointer)
53+
{ ... }
54+
native(&py_tuple_set_item);
4655
sub py_call_function(Str, Str, int, CArray[OpaquePointer])
4756
returns OpaquePointer { ... }
4857
native(&py_call_function);
@@ -68,14 +77,17 @@ method py_to_p6(OpaquePointer $value) {
6877
return Any;
6978
}
7079

80+
multi method p6_to_py(Int:D $value) returns OpaquePointer {
81+
py_int_to_py($value);
82+
}
83+
7184
method !setup_arguments(@args) {
72-
# my $len = @args.elems;
73-
# my @svs := CArray[OpaquePointer].new();
74-
# loop (my Int $i = 0; $i < $len; $i = $i + 1) {
75-
# @svs[$i] = self.p6_to_py(@args[$i]);
76-
# }
77-
# return $len, @svs;
78-
return 0;
85+
my $len = @args.elems;
86+
my $tuple = py_tuple_new($len);
87+
loop (my Int $i = 0; $i < $len; $i = $i + 1) {
88+
py_tuple_set_item($tuple, $i, self.p6_to_py(@args[$i]));
89+
}
90+
return $tuple;
7991
}
8092

8193
method !unpack_return_values($av) {
@@ -111,7 +123,7 @@ multi method run($python, :$file) {
111123
}
112124

113125
method call(Str $package, Str $function, *@args) {
114-
my $array = py_call_function($package, $function, |self!setup_arguments(@args));
126+
my $array = py_call_function($package, $function, self!setup_arguments(@args));
115127
return Any;
116128
self!unpack_return_values($array);
117129
}

pyhelper.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ long py_int_as_long(PyObject *obj) {
5656
return PyInt_AsLong(obj);
5757
}
5858

59+
PyObject *py_int_to_py(long num) {
60+
return PyInt_FromLong(num);
61+
}
62+
5963
char *py_unicode_to_char_star(PyObject *obj) {
6064
PyObject * const string = PyUnicode_AsUTF8String(obj); /* new reference */
6165
if (!string) {
@@ -78,7 +82,15 @@ Py_ssize_t py_string_to_buf(PyObject *obj, char **buf) {
7882
return length;
7983
}
8084

81-
PyObject *py_call_function(char *pkg, char *name, int len, PyObject *args[]) {
85+
PyObject *py_tuple_new(int len) {
86+
return PyTuple_New(len);
87+
}
88+
89+
void py_tuple_set_item(PyObject *tuple, int i, PyObject *item) {
90+
PyTuple_SetItem(tuple, i, item);
91+
}
92+
93+
PyObject *py_call_function(char *pkg, char *name, PyObject *args) {
8294
int i;
8395
PyObject * const mod = PyImport_AddModule(pkg);
8496
PyObject * const dict = PyModule_GetDict(mod);
@@ -87,11 +99,9 @@ PyObject *py_call_function(char *pkg, char *name, int len, PyObject *args[]) {
8799
PyObject *py_retval = NULL;
88100
PyObject *tuple = NULL;
89101

90-
tuple = PyTuple_New(0);
91-
92-
py_retval = PyObject_CallObject(func, tuple);
102+
py_retval = PyObject_CallObject(func, args);
93103
Py_DECREF(func);
94-
Py_DECREF(tuple);
104+
Py_DECREF(args);
95105

96106
return py_retval;
97107
}

t/call.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def test():
1313
return;
1414
1515
def test_int_params(a, b):
16-
if a == 2 and b == 2:
16+
if a == 2 and b == 1:
1717
print "ok 2 - int params";
1818
else:
1919
print "not ok 2 - int params";

0 commit comments

Comments
 (0)