Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Support passing Python dictionaries back to Perl 6.
  • Loading branch information
niner committed Oct 14, 2014
1 parent a64f55a commit 252bf41
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
18 changes: 18 additions & 0 deletions lib/Inline/Python.pm6
Expand Up @@ -37,6 +37,9 @@ sub py_string_check(OpaquePointer)
sub py_sequence_check(OpaquePointer)
returns int32 { ... }
native(&py_sequence_check);
sub py_mapping_check(OpaquePointer)
returns int32 { ... }
native(&py_mapping_check);
sub py_int_as_long(OpaquePointer)
returns Int { ... }
native(&py_int_as_long);
Expand Down Expand Up @@ -67,6 +70,9 @@ sub py_sequence_length(OpaquePointer)
sub py_sequence_get_item(OpaquePointer, int)
returns OpaquePointer { ... }
native(&py_sequence_get_item);
sub py_mapping_items(OpaquePointer)
returns OpaquePointer { ... }
native(&py_mapping_items);
sub py_dec_ref(OpaquePointer)
{ ... }
native(&py_dec_ref);
Expand All @@ -82,6 +88,15 @@ method py_array_to_array(OpaquePointer $py_array) {
return @array;
}

method py_dict_to_hash(OpaquePointer $py_dict) {
my %hash;
my $items = py_mapping_items($py_dict);
my @items = self.py_to_p6($items);
py_dec_ref($items);
%hash{$_[0]} = $_[1] for @items;
return %hash;
}

method py_to_p6(OpaquePointer $value) {
if py_int_check($value) {
return py_int_as_long($value);
Expand All @@ -103,6 +118,9 @@ method py_to_p6(OpaquePointer $value) {
elsif py_sequence_check($value) {
return self.py_array_to_array($value);
}
elsif py_mapping_check($value) {
return self.py_dict_to_hash($value);
}
return Any;
}

Expand Down
8 changes: 8 additions & 0 deletions pyhelper.c
Expand Up @@ -56,6 +56,10 @@ int py_sequence_check(PyObject *obj) {
return PySequence_Check(obj);
}

int py_mapping_check(PyObject *obj) {
return PyMapping_Check(obj);
}

long py_int_as_long(PyObject *obj) {
return PyInt_AsLong(obj);
}
Expand Down Expand Up @@ -97,6 +101,10 @@ PyObject *py_sequence_get_item(PyObject *obj, int item) {
return PySequence_GetItem(obj, item);
}

PyObject *py_mapping_items(PyObject *obj) {
return PyMapping_Items(obj);
}

PyObject *py_tuple_new(int len) {
return PyTuple_New(len);
}
Expand Down
7 changes: 3 additions & 4 deletions t/py_to_p6.t
Expand Up @@ -12,10 +12,9 @@ is $py.run('5.5', :eval), 5.5;
is $py.run('u"Python"', :eval), 'Python';
is_deeply $py.run('[1, 2]', :eval), [1, 2];
is_deeply $py.run('[1, [2, 3]]', :eval), [1, [2, 3]];
todo 'NYI', 3;
is_deeply $py.run('{"a": 1, "b": 2}', :eval), {a => 1, b => 2};
is_deeply $py.run('{"a": 1, "b": {"c": 3}}', :eval), {a => 1, b => {c => 3}};
is_deeply $py.run('[1, {"b": {"c": 3}}]', :eval), [1, {b => {c => 3}}];
is_deeply $py.run('{u"a": 1, u"b": 2}', :eval), {a => 1, b => 2};
is_deeply $py.run('{u"a": 1, u"b": {u"c": 3}}', :eval), {a => 1, b => {c => 3}};
is_deeply $py.run('[1, {u"b": {u"c": 3}}]', :eval), [1, {b => {c => 3}}];
ok $py.run('None', :eval) === Any, 'py None maps to p6 Any';

todo 'NYI';
Expand Down

0 comments on commit 252bf41

Please sign in to comment.