Skip to content

Commit 49b9bcd

Browse files
committed
Support: use foo:from<Python>
1 parent fbee34c commit 49b9bcd

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

lib/Inline/Python.pm6

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,3 +583,48 @@ multi sub EVAL(
583583
CATCH { note $_ }
584584
$py.run($code, |($mode eq 'eval' ?? :eval !! :file));
585585
}
586+
587+
CompUnit::RepositoryRegistry.use-repository(
588+
class :: does CompUnit::Repository {
589+
method need(
590+
CompUnit::DependencySpecification $spec,
591+
CompUnit::PrecompilationRepository $precomp?,
592+
)
593+
returns CompUnit:D
594+
{
595+
if $spec.from eq 'Python' {
596+
my $python = Inline::Python.default_python;
597+
598+
if $*RAKUDO_MODULE_DEBUG -> $RMD {
599+
$RMD("Loading {$spec.short-name} via Inline::Python");
600+
}
601+
my $handle := $python.import(
602+
$spec.short-name,
603+
);
604+
return CompUnit.new(
605+
:short-name($spec.short-name),
606+
:handle(CompUnit::Handle.from-unit(Stash.new)),
607+
:repo(self),
608+
:repo-id($spec.short-name),
609+
:from($spec.from),
610+
);
611+
}
612+
613+
return self.next-repo.need($spec, |($precomp ?? $precomp !! ())) if self.next-repo;
614+
X::CompUnit::UnsatisfiedDependency.new(:specification($spec)).throw;
615+
}
616+
617+
method loaded() {
618+
[]
619+
}
620+
621+
method id() {
622+
'Python'
623+
}
624+
625+
method path-spec() {
626+
'python#'
627+
}
628+
}.new(),
629+
:current($*REPO),
630+
)

t/import.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 Inline::Python;
5+
use Test;
6+
7+
use string:from<Python>;
8+
9+
my $py = Inline::Python.default_python;
10+
is($py.call('string', 'capwords', 'foo bar'), 'Foo Bar');
11+
12+
done-testing;

0 commit comments

Comments
 (0)