Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Allow passing Any to Python methods
  • Loading branch information
niner committed Oct 14, 2014
1 parent 8d6a813 commit 8b44f1c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
8 changes: 7 additions & 1 deletion lib/Inline/Python.pm6
Expand Up @@ -48,6 +48,9 @@ sub py_sequence_check(OpaquePointer)
sub py_mapping_check(OpaquePointer)
returns int32 { ... }
native(&py_mapping_check);
sub py_is_none(OpaquePointer)
returns int32 { ... }
native(&py_is_none);
sub py_int_as_long(OpaquePointer)
returns Int { ... }
native(&py_int_as_long);
Expand Down Expand Up @@ -137,7 +140,10 @@ method py_dict_to_hash(OpaquePointer $py_dict) {

method py_to_p6(OpaquePointer $value) {
return Any unless defined $value;
if py_instance_check($value) {
if py_is_none($value) {
return Any;
}
elsif py_instance_check($value) {
py_inc_ref($value);
return PythonObject.new(python => self, ptr => $value);
}
Expand Down
4 changes: 4 additions & 0 deletions pyhelper.c
Expand Up @@ -68,6 +68,10 @@ int py_mapping_check(PyObject *obj) {
return PyMapping_Check(obj);
}

int py_is_none(PyObject *obj) {
return obj == Py_None;
}

long py_int_as_long(PyObject *obj) {
return PyInt_AsLong(obj);
}
Expand Down
4 changes: 2 additions & 2 deletions t/call.t
Expand Up @@ -3,7 +3,7 @@
use v6;
use Inline::Python;

say "1..9";
say "1..10";

my $py = Inline::Python.new();
$py.run('
Expand Down Expand Up @@ -124,14 +124,14 @@ else {
say "not ok 9 - Python method call with parameters";
}

if False {
if ($py.call('__main__', 'test_none', Any) == 1) {
say "ok 10 - Any converted to undef";
}
else {
say "not ok 10 - Any converted to undef";
}

if False {
if ($py.call('__main__', 'test_hash', {a => 2, b => {c => [4, 3]}}) == 1) {
say "ok 11 - Passing hashes to Python";
}
Expand Down

0 comments on commit 8b44f1c

Please sign in to comment.