Skip to content

Commit dac3ee9

Browse files
committed
Implement conversion of Python integer to Int
1 parent ef75a9f commit dac3ee9

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

lib/Inline/Python.pm6

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,26 @@ sub py_init_python()
2525
sub py_eval(Str, Int)
2626
returns OpaquePointer { ... }
2727
native(&py_eval);
28+
sub py_int_check(OpaquePointer)
29+
returns int32 { ... }
30+
native(&py_int_check);
31+
sub py_int_as_long(OpaquePointer)
32+
returns Int { ... }
33+
native(&py_int_as_long);
2834

2935
method py_to_p6(OpaquePointer $value) {
36+
if py_int_check($value) {
37+
return py_int_as_long($value);
38+
}
3039
return Any;
3140
}
3241

33-
method run($python) {
42+
multi method run($python, :$eval!) {
43+
my $res = py_eval($python, 0);
44+
self.py_to_p6($res);
45+
}
46+
47+
multi method run($python, :$file) {
3448
my $res = py_eval($python, 1);
3549
self.py_to_p6($res);
3650
}

pyhelper.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,11 @@ PyObject *py_eval(const char* p, int type) {
3939
}
4040
return py_result;
4141
}
42+
43+
int py_int_check(PyObject *obj) {
44+
return PyInt_Check(obj);
45+
}
46+
47+
long py_int_as_long(PyObject *obj) {
48+
return PyInt_AsLong(obj);
49+
}

t/eval.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ my $py = Inline::Python.new();
99

1010
$py.run('
1111
print "ok 1 - basic eval\n";
12-
');
12+
', :file);
1313

1414
# vim: ft=perl6

t/eval_return_values.t

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env perl6
2+
3+
use v6;
4+
use Test;
5+
use Inline::Python;
6+
7+
plan 1;
8+
9+
my $py = Inline::Python.new();
10+
is $py.run('5', :eval), 5;
11+
12+
# vim: ft=perl6

0 commit comments

Comments
 (0)