Inline Python, not able to read stdout/sdterr from python to perl

Viewed 51
use Test::Trap;
use Data::Dumper;

use Inline Python => <<END;
import logging
from os import sys
_logger = logging.getLogger()
_logger.setLevel(logging.INFO)
handler = logging.StreamHandler(sys.stderr)
_logger.addHandler(handler)

def fun():
    print("From print")
    logging.info("Inside fun")
END

trap { fun(); };
print Dumper($trap->stdout);
print Dumper($trap->stderr);


sub fun1 {
    print "From fun1";
}

trap {fun1();};
print "Trap stdout from perl subroutine call\n";
print Dumper($trap->stdout);
print Dumper($trap->stderr);

Output:

From print
Inside fun
$VAR1 = '';
$VAR1 = '';
Trap stdout from perl subroutine call
$VAR1 = 'From fun1'
$VAR1 = '';

The output of this program is printing "From print" and "Inside fun" on the screen , but $trap->stderr and $trap->stdout is printed as blank for fun()(python API called from perl) But for fun1() subroutine which is defined in Perl code, trap->stdout is printed.

Can anyone please suggest how to get stdout/stderr using trap for perl-python interface?

1 Answers

The default "capture strategy" only captures what's sent to the STDOUT and STDERR Perl file handles, but Python doesn't know anything about Perl file handles.

Replace

use Test::Trap;

with

use Test::Trap ':stdout(systemsafe):stderr(systemsafe)';

Output

$VAR1 = 'From print
';
$VAR1 = 'Inside fun
';
Trap stdout from perl subroutine call
$VAR1 = 'From fun1';
$VAR1 = '';
Related