Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Support passing Rats to Python
  • Loading branch information
niner committed Oct 14, 2014
1 parent a65b7df commit 351e2b9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
7 changes: 7 additions & 0 deletions lib/Inline/Python.pm6
Expand Up @@ -46,6 +46,9 @@ sub py_int_as_long(OpaquePointer)
sub py_int_to_py(Int)
returns OpaquePointer { ... }
native(&py_int_to_py);
sub py_float_to_py(num64)
returns OpaquePointer { ... }
native(&py_float_to_py);
sub py_unicode_to_char_star(OpaquePointer)
returns Str { ... }
native(&py_unicode_to_char_star);
Expand Down Expand Up @@ -131,6 +134,10 @@ multi method p6_to_py(Int:D $value) returns OpaquePointer {
py_int_to_py($value);
}

multi method p6_to_py(Rat:D $value) returns OpaquePointer {
py_float_to_py($value.Num);
}

multi method p6_to_py(Str:D $value) returns OpaquePointer {
py_str_to_py($value.encode('UTF-8').bytes, $value);
}
Expand Down
4 changes: 4 additions & 0 deletions pyhelper.c
Expand Up @@ -68,6 +68,10 @@ PyObject *py_int_to_py(long num) {
return PyInt_FromLong(num);
}

PyObject *py_float_to_py(double num) {
return PyFloat_FromDouble(num);
}

PyObject *py_str_to_py(int len, char *str) {
return PyUnicode_DecodeUTF8(str, len, "replace");
}
Expand Down
7 changes: 4 additions & 3 deletions t/p6_to_py.t
Expand Up @@ -4,7 +4,7 @@ use v6;
use Test;
use Inline::Python;

plan 4;
plan 6;

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

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

Expand All @@ -40,6 +40,7 @@ def is_two_point_five(a):
return a == 2.5;
/);

ok($py.call('__main__', 'is_two_point_five', 2.5));
#ok($py.call('__main__', 'is_two_point_five', Num.new(2.5)));

# vim: ft=perl6

0 comments on commit 351e2b9

Please sign in to comment.