1
1
class Inline::Python ;
2
2
3
+ has & ! call_object ;
3
4
has & ! call_method ;
4
5
5
6
use NativeCall ;
@@ -50,9 +51,12 @@ class ObjectKeeper {
50
51
}
51
52
}
52
53
53
- sub py_init_python (& call_method (Int , Str , OpaquePointer, OpaquePointer --> OpaquePointer))
54
+ sub py_init_python (& call_object ( Int , OpaquePointer, OpaquePointer --> OpaquePointer), & call_method (Int , Str , OpaquePointer, OpaquePointer --> OpaquePointer))
54
55
{ ... }
55
56
native(& py_init_python );
57
+ sub py_init_perl6object ()
58
+ { ... }
59
+ native(& py_init_perl6object );
56
60
sub py_eval (Str , Int )
57
61
returns OpaquePointer { ... }
58
62
native(& py_eval );
@@ -80,6 +84,9 @@ sub py_sequence_check(OpaquePointer)
80
84
sub py_mapping_check (OpaquePointer)
81
85
returns int32 { ... }
82
86
native(& py_mapping_check );
87
+ sub py_callable_check (OpaquePointer)
88
+ returns int32 { ... }
89
+ native(& py_callable_check );
83
90
sub py_is_none (OpaquePointer)
84
91
returns int32 { ... }
85
92
native(& py_is_none );
@@ -183,7 +190,7 @@ method py_to_p6(OpaquePointer $value) {
183
190
if py_is_none($ value ) {
184
191
return Any ;
185
192
}
186
- elsif py_instance_check($ value ) {
193
+ elsif py_instance_check($ value ) or py_callable_check( $ value ) {
187
194
if py_is_instance($ value , $ perl6object ) {
188
195
return $ objects . get (
189
196
py_int_as_long(
@@ -319,6 +326,18 @@ method invoke(OpaquePointer $obj, Str $method, *@args) {
319
326
}
320
327
321
328
method BUILD {
329
+ & ! call_object = sub (Int $ index , OpaquePointer $ args , OpaquePointer $ err ) returns OpaquePointer {
330
+ my $ p6obj = $ objects . get ($ index );
331
+ my \retvals = $ p6obj (| self . py_array_to_array($ args ));
332
+ return self . p6_to_py(retvals);
333
+ CATCH {
334
+ default {
335
+ warn $ _ ;
336
+ nativecast(CArray [OpaquePointer], $ err )[0 ] = self . p6_to_py($ _ );
337
+ return OpaquePointer;
338
+ }
339
+ }
340
+ }
322
341
& ! call_method = sub (Int $ index , Str $ name , OpaquePointer $ args , OpaquePointer $ err ) returns OpaquePointer {
323
342
my $ p6obj = $ objects . get ($ index );
324
343
my @ retvals = $ p6obj . " $ name" (| self . py_array_to_array($ args ));
@@ -331,18 +350,23 @@ method BUILD {
331
350
}
332
351
}
333
352
334
- py_init_python(& ! call_method );
353
+ py_init_python(& ! call_object , & ! call_method );
335
354
336
355
self . run (q :heredoc /PYTHON /);
337
- import perl6
338
- class Perl6Object:
339
- def __init__(self, index):
340
- self.index = index
341
- def get_perl6_object(self):
342
- return self.index
343
- def __getattr__(self, attr):
344
- return lambda *args: perl6.invoke(self.index, attr, args)
345
- PYTHON
356
+ import perl6
357
+ from logging import warn
358
+ class Perl6Object:
359
+ def __init__(self, index):
360
+ self.index = index
361
+ def get_perl6_object(self):
362
+ return self.index
363
+ def __call__(self, *args):
364
+ return perl6.call(self.index, args)
365
+ def __getattr__(self, attr):
366
+ return lambda *args: perl6.invoke(self.index, attr, args)
367
+ PYTHON
368
+
369
+ py_init_perl6object();
346
370
347
371
$ perl6object = py_eval(' Perl6Object' , 0 );
348
372
}
@@ -353,15 +377,42 @@ class PythonObject {
353
377
354
378
method sink () { self }
355
379
380
+ method postcircumfix :< ( )> (\args) {
381
+ $ . python . invoke($ . ptr , ' __call__' , args. list);
382
+ }
383
+
384
+ method invoke (\args) {
385
+ $ . python . invoke($ . ptr , ' __call__' , args. list);
386
+ }
387
+
356
388
method DESTROY {
357
389
$ ! python . py_dec_ref($ ! ptr ) if $ ! ptr ;
358
390
$ ! ptr = OpaquePointer;
359
391
}
360
392
}
361
393
394
+ role PythonParent [$ package , $ class ] {
395
+ has $ . parent ;
396
+ has $ . python ;
397
+
398
+ submethod BUILD (: $ python , : $ parent ? ) {
399
+ $ ! parent = $ parent // $ python . call($ package , $ class );
400
+ $ ! python = $ python ;
401
+ # $python.rebless($!parent);
402
+ }
403
+
404
+ ::? CLASS. HOW . add_fallback(::? CLASS, -> $ , $ { True },
405
+ method ($ name ) {
406
+ -> \self , | args {
407
+ $ . python . invoke(self , $ . parent . ptr, $ name , args. list);
408
+ }
409
+ }
410
+ );
411
+ }
412
+
362
413
BEGIN {
363
414
PythonObject.^ add_fallback(-> $ , $ { True },
364
- method ($ name ) {
415
+ method ($ name ) {
365
416
-> \self , | args {
366
417
$ . python . invoke($ . ptr , $ name , args. list);
367
418
}
0 commit comments