Skip to content

Commit 46c420b

Browse files
committed
Support passing arrays to Python
1 parent 8849d3f commit 46c420b

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

lib/Inline/Python.pm6

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ sub py_tuple_new(Int)
7373
sub py_tuple_set_item(OpaquePointer, Int, OpaquePointer)
7474
{ ... }
7575
native(&py_tuple_set_item);
76+
sub py_list_new(Int)
77+
returns OpaquePointer { ... }
78+
native(&py_list_new);
79+
sub py_list_set_item(OpaquePointer, Int, OpaquePointer)
80+
{ ... }
81+
native(&py_list_set_item);
7682
sub py_call_function(Str, Str, int, CArray[OpaquePointer])
7783
returns OpaquePointer { ... }
7884
native(&py_call_function);
@@ -163,6 +169,14 @@ multi method p6_to_py(blob8:D $value) returns OpaquePointer {
163169
py_buf_to_py($value.elems, $array);
164170
}
165171

172+
multi method p6_to_py(Positional:D $value) returns OpaquePointer {
173+
my $array = py_list_new($value.elems);
174+
for @$value.kv -> $i, $item {
175+
py_list_set_item($array, $i, self.p6_to_py($item));
176+
}
177+
return $array;
178+
}
179+
166180
method !setup_arguments(@args) {
167181
my $len = @args.elems;
168182
my $tuple = py_tuple_new($len);

pyhelper.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,14 @@ void py_tuple_set_item(PyObject *tuple, int i, PyObject *item) {
129129
PyTuple_SetItem(tuple, i, item);
130130
}
131131

132+
PyObject *py_list_new(int len) {
133+
return PyList_New(len);
134+
}
135+
136+
void py_list_set_item(PyObject *list, int i, PyObject *item) {
137+
PyList_SetItem(list, i, item);
138+
}
139+
132140
void py_dec_ref(PyObject *obj) {
133141
Py_DECREF(obj);
134142
}

t/p6_to_py.t

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use v6;
44
use Test;
55
use Inline::Python;
66

7-
plan 8;
7+
plan 9;
88

99
my $py = Inline::Python.new();
1010
$py.run(q[
@@ -14,8 +14,8 @@ def identity(a):
1414

1515
class Foo {
1616
}
17-
# , [1, 2], { a => 1, b => 2}, Any, Foo.new
18-
for ('abcö', Buf.new('äbc'.encode('latin-1')), 24, 2.4.Num) -> $obj {
17+
# , { a => 1, b => 2}, Any, Foo.new
18+
for ('abcö', Buf.new('äbc'.encode('latin-1')), 24, 2.4.Num, [1, 2]) -> $obj {
1919
is_deeply $py.call('__main__', 'identity', $obj), $obj, "Can round-trip " ~ $obj.^name;
2020
}
2121

0 commit comments

Comments
 (0)