Skip to content

Commit 8b44f1c

Browse files
committed
Allow passing Any to Python methods
1 parent 8d6a813 commit 8b44f1c

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

lib/Inline/Python.pm6

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ sub py_sequence_check(OpaquePointer)
4848
sub py_mapping_check(OpaquePointer)
4949
returns int32 { ... }
5050
native(&py_mapping_check);
51+
sub py_is_none(OpaquePointer)
52+
returns int32 { ... }
53+
native(&py_is_none);
5154
sub py_int_as_long(OpaquePointer)
5255
returns Int { ... }
5356
native(&py_int_as_long);
@@ -137,7 +140,10 @@ method py_dict_to_hash(OpaquePointer $py_dict) {
137140

138141
method py_to_p6(OpaquePointer $value) {
139142
return Any unless defined $value;
140-
if py_instance_check($value) {
143+
if py_is_none($value) {
144+
return Any;
145+
}
146+
elsif py_instance_check($value) {
141147
py_inc_ref($value);
142148
return PythonObject.new(python => self, ptr => $value);
143149
}

pyhelper.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ int py_mapping_check(PyObject *obj) {
6868
return PyMapping_Check(obj);
6969
}
7070

71+
int py_is_none(PyObject *obj) {
72+
return obj == Py_None;
73+
}
74+
7175
long py_int_as_long(PyObject *obj) {
7276
return PyInt_AsLong(obj);
7377
}

t/call.t

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

6-
say "1..9";
6+
say "1..10";
77

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

127-
if False {
128127
if ($py.call('__main__', 'test_none', Any) == 1) {
129128
say "ok 10 - Any converted to undef";
130129
}
131130
else {
132131
say "not ok 10 - Any converted to undef";
133132
}
134133

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

0 commit comments

Comments
 (0)