Skip to content
This repository has been archived by the owner on Jan 30, 2019. It is now read-only.

Commit

Permalink
Also trap serialization errors when calling exported functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
wavexx committed Jul 30, 2014
1 parent e033ad6 commit 1c1fba3
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 34 deletions.
24 changes: 13 additions & 11 deletions bond/PHP/prelude.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,20 @@ function __PY_BOND_get_error()


/// Recursive repl
function __PY_BOND_sendstate($state, $data)
{
$enc_ret = json_encode($data);
if(json_last_error())
{
$state = "ERROR";
$enc_ret = json_encode(@"cannot encode $data");
}
__PY_BOND_sendline("$state $enc_ret");
}

function __PY_BOND_remote($name, $args)
{
/// TODO: handle encoding errors
$json = json_encode(array($name, $args));
__PY_BOND_sendline("REMOTE $json");
__PY_BOND_sendstate("REMOTE", array($name, $args));
return __PY_BOND_repl();
}

Expand Down Expand Up @@ -201,14 +210,7 @@ function __PY_BOND_repl()
$ret = $err;
}

/// encode the result
$enc_ret = json_encode($ret);
if(json_last_error())
{
$state = "ERROR";
$enc_ret = json_encode(@"cannot encode $ret");
}
__PY_BOND_sendline("$state $enc_ret");
__PY_BOND_sendstate($state, $ret);
}
exit(0);
}
Expand Down
25 changes: 14 additions & 11 deletions bond/Perl/prelude.pl
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,22 @@ sub __PY_BOND_sendline


# Recursive repl
sub __PY_BOND_sendstate($$)
{
my ($state, $data) = @_;
my $enc_ret = eval { __PY_BOND_dumps($data); };
if($@)
{
$state = "ERROR";
$enc_ret = __PY_BOND_dumps("cannot encode $data");
}
__PY_BOND_sendline("$state $enc_ret");
}

sub __PY_BOND_remote($$)
{
my ($name, $args) = @_;
# TODO: handle encoding errors
my $json = __PY_BOND_dumps([$name, $args]);
__PY_BOND_sendline("REMOTE $json");
__PY_BOND_sendstate("REMOTE", [$name, $args]);
return __PY_BOND_repl();
}

Expand Down Expand Up @@ -149,14 +159,7 @@ ()
$ret = $err;
}

# encode the result
my $enc_ret = eval { __PY_BOND_dumps($ret); };
if($@)
{
$state = "ERROR";
$enc_ret = __PY_BOND_dumps("cannot encode $ret");
}
__PY_BOND_sendline("$state $enc_ret");
__PY_BOND_sendstate($state, $ret);
}
exit(0);
}
Expand Down
23 changes: 11 additions & 12 deletions bond/Python/prelude.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,17 @@ def __PY_BOND_loads(string):


# Recursive repl
def __PY_BOND_sendstate(state, data):
code = None
try:
code = __PY_BOND_dumps(data)
except:
state = "ERROR"
code = __PY_BOND_dumps("cannot encode {data}".format(data=str(data)))
__PY_BOND_sendline("{state} {code}".format(state=state, code=code))

def __PY_BOND_remote(name, args):
# TODO: handle encoding errors
code = __PY_BOND_dumps([name, args])
__PY_BOND_sendline("REMOTE {code}".format(code=code))
__PY_BOND_sendstate("REMOTE", [name, args])
return __PY_BOND_repl()

def __PY_BOND_export(name):
Expand Down Expand Up @@ -100,15 +107,7 @@ def __PY_BOND_repl():
state = "EXCEPT"
ret = err

# encode the result
code = None
try:
code = __PY_BOND_dumps(ret)
except:
state = "ERROR"
code = __PY_BOND_dumps("cannot encode {ret}".format(ret=str(ret)))

__PY_BOND_sendline("{state} {code}".format(state=state, code=code))
__PY_BOND_sendstate(state, ret)

# stream ended
exit(0)
Expand Down
20 changes: 20 additions & 0 deletions test/test_PHP.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,26 @@ def func_python_rec(arg):
assert(func_php_rec_2(0) == 5)


def test_export_ser_err():
def call_me(arg):
pass

php = PHP(timeout=1)
php.export(call_me, 'call_me')
php.eval_block('$fd = fopen("php://stdout", "w");')

failed = False
try:
php.eval('call_me($fd)')
except bond.SerializationException as e:
print(e)
failed = (e.side == "remote")
assert(failed)

# ensure the env didn't just die
assert(php.eval('1') == 1)


def test_output_redirect():
php = PHP(timeout=1);

Expand Down
23 changes: 23 additions & 0 deletions test/test_Perl.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,29 @@ def func_python_rec(arg):
assert(func_perl_rec_2(0) == 5)


def test_export_ser_err():
def call_me(arg):
pass

perl = Perl(timeout=1)
perl.export(call_me, 'call_me')
perl.eval_block(r'''
use IO::File;
$fd = IO::File->new();
''')

failed = False
try:
perl.eval('call_me($fd)')
except bond.SerializationException as e:
print(e)
failed = (e.side == "remote")
assert(failed)

# ensure the env didn't just die
assert(perl.eval('1') == 1)


def test_output_redirect():
perl = Perl(timeout=1)

Expand Down
19 changes: 19 additions & 0 deletions test/test_Python.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,25 @@ def func_local_rec(arg):
assert(func_remote_rec_2(0) == 5)


def test_export_ser_err():
def call_me(arg):
pass

py = Python(timeout=1)
py.export(call_me, 'call_me')

failed = False
try:
py.eval('call_me(lambda x: x)')
except bond.SerializationException as e:
print(e)
failed = (e.side == "remote")
assert(failed)

# ensure the env didn't just die
assert(py.eval('1') == 1)


def test_output_redirect():
py = Python(timeout=1)

Expand Down

0 comments on commit 1c1fba3

Please sign in to comment.