def test_try_not_caught(): with pf.Graph() as graph: a = pf.placeholder() b = pf.placeholder() c = pf.try_(a / b, [(ValueError, 'value-error')]) with pytest.raises(ZeroDivisionError): graph(c, {a: 1, b: 0})
def test_try(context, expected): finally_reached = [] with pf.Graph() as graph: a = pf.placeholder('a') b = pf.placeholder('b') c = pf.try_(a / b, [(ZeroDivisionError, 'zero-division')], pf.func_op(lambda: finally_reached.append('done'))) assert graph(c, context) == expected assert finally_reached
def test_try_callback(): with pf.Graph() as graph: a = pf.placeholder('a') b = pf.assert_((a > 0).set_name('condition'), value=a, name='b') c = pf.try_( b, [(AssertionError, (pf.constant(41, name='41') + 1).set_name('alternative'))]) tracer = pf.Profiler() graph(c, {a: 3}, callback=tracer) == 3 assert len(tracer.times) == 3 graph(c, {a: -2}, callback=tracer) == 42 assert len(tracer.times) == 5