Blame external/pybind11/tests/test_callbacks.py

Packit 534379
import pytest
Packit 534379
from pybind11_tests import callbacks as m
Packit 534379
from threading import Thread
Packit 534379
Packit 534379
Packit 534379
def test_callbacks():
Packit 534379
    from functools import partial
Packit 534379
Packit 534379
    def func1():
Packit 534379
        return "func1"
Packit 534379
Packit 534379
    def func2(a, b, c, d):
Packit 534379
        return "func2", a, b, c, d
Packit 534379
Packit 534379
    def func3(a):
Packit 534379
        return "func3({})".format(a)
Packit 534379
Packit 534379
    assert m.test_callback1(func1) == "func1"
Packit 534379
    assert m.test_callback2(func2) == ("func2", "Hello", "x", True, 5)
Packit 534379
    assert m.test_callback1(partial(func2, 1, 2, 3, 4)) == ("func2", 1, 2, 3, 4)
Packit 534379
    assert m.test_callback1(partial(func3, "partial")) == "func3(partial)"
Packit 534379
    assert m.test_callback3(lambda i: i + 1) == "func(43) = 44"
Packit 534379
Packit 534379
    f = m.test_callback4()
Packit 534379
    assert f(43) == 44
Packit 534379
    f = m.test_callback5()
Packit 534379
    assert f(number=43) == 44
Packit 534379
Packit 534379
Packit 534379
def test_bound_method_callback():
Packit 534379
    # Bound Python method:
Packit 534379
    class MyClass:
Packit 534379
        def double(self, val):
Packit 534379
            return 2 * val
Packit 534379
Packit 534379
    z = MyClass()
Packit 534379
    assert m.test_callback3(z.double) == "func(43) = 86"
Packit 534379
Packit 534379
    z = m.CppBoundMethodTest()
Packit 534379
    assert m.test_callback3(z.triple) == "func(43) = 129"
Packit 534379
Packit 534379
Packit 534379
def test_keyword_args_and_generalized_unpacking():
Packit 534379
Packit 534379
    def f(*args, **kwargs):
Packit 534379
        return args, kwargs
Packit 534379
Packit 534379
    assert m.test_tuple_unpacking(f) == (("positional", 1, 2, 3, 4, 5, 6), {})
Packit 534379
    assert m.test_dict_unpacking(f) == (("positional", 1), {"key": "value", "a": 1, "b": 2})
Packit 534379
    assert m.test_keyword_args(f) == ((), {"x": 10, "y": 20})
Packit 534379
    assert m.test_unpacking_and_keywords1(f) == ((1, 2), {"c": 3, "d": 4})
Packit 534379
    assert m.test_unpacking_and_keywords2(f) == (
Packit 534379
        ("positional", 1, 2, 3, 4, 5),
Packit 534379
        {"key": "value", "a": 1, "b": 2, "c": 3, "d": 4, "e": 5}
Packit 534379
    )
Packit 534379
Packit 534379
    with pytest.raises(TypeError) as excinfo:
Packit 534379
        m.test_unpacking_error1(f)
Packit 534379
    assert "Got multiple values for keyword argument" in str(excinfo.value)
Packit 534379
Packit 534379
    with pytest.raises(TypeError) as excinfo:
Packit 534379
        m.test_unpacking_error2(f)
Packit 534379
    assert "Got multiple values for keyword argument" in str(excinfo.value)
Packit 534379
Packit 534379
    with pytest.raises(RuntimeError) as excinfo:
Packit 534379
        m.test_arg_conversion_error1(f)
Packit 534379
    assert "Unable to convert call argument" in str(excinfo.value)
Packit 534379
Packit 534379
    with pytest.raises(RuntimeError) as excinfo:
Packit 534379
        m.test_arg_conversion_error2(f)
Packit 534379
    assert "Unable to convert call argument" in str(excinfo.value)
Packit 534379
Packit 534379
Packit 534379
def test_lambda_closure_cleanup():
Packit 534379
    m.test_cleanup()
Packit 534379
    cstats = m.payload_cstats()
Packit 534379
    assert cstats.alive() == 0
Packit 534379
    assert cstats.copy_constructions == 1
Packit 534379
    assert cstats.move_constructions >= 1
Packit 534379
Packit 534379
Packit 534379
def test_cpp_function_roundtrip():
Packit 534379
    """Test if passing a function pointer from C++ -> Python -> C++ yields the original pointer"""
Packit 534379
Packit 534379
    assert m.test_dummy_function(m.dummy_function) == "matches dummy_function: eval(1) = 2"
Packit 534379
    assert (m.test_dummy_function(m.roundtrip(m.dummy_function)) ==
Packit 534379
            "matches dummy_function: eval(1) = 2")
Packit 534379
    assert m.roundtrip(None, expect_none=True) is None
Packit 534379
    assert (m.test_dummy_function(lambda x: x + 2) ==
Packit 534379
            "can't convert to function pointer: eval(1) = 3")
Packit 534379
Packit 534379
    with pytest.raises(TypeError) as excinfo:
Packit 534379
        m.test_dummy_function(m.dummy_function2)
Packit 534379
    assert "incompatible function arguments" in str(excinfo.value)
Packit 534379
Packit 534379
    with pytest.raises(TypeError) as excinfo:
Packit 534379
        m.test_dummy_function(lambda x, y: x + y)
Packit 534379
    assert any(s in str(excinfo.value) for s in ("missing 1 required positional argument",
Packit 534379
                                                 "takes exactly 2 arguments"))
Packit 534379
Packit 534379
Packit 534379
def test_function_signatures(doc):
Packit 534379
    assert doc(m.test_callback3) == "test_callback3(arg0: Callable[[int], int]) -> str"
Packit 534379
    assert doc(m.test_callback4) == "test_callback4() -> Callable[[int], int]"
Packit 534379
Packit 534379
Packit 534379
def test_movable_object():
Packit 534379
    assert m.callback_with_movable(lambda _: None) is True
Packit 534379
Packit 534379
Packit 534379
def test_async_callbacks():
Packit 534379
    # serves as state for async callback
Packit 534379
    class Item:
Packit 534379
        def __init__(self, value):
Packit 534379
            self.value = value
Packit 534379
Packit 534379
    res = []
Packit 534379
Packit 534379
    # generate stateful lambda that will store result in `res`
Packit 534379
    def gen_f():
Packit 534379
        s = Item(3)
Packit 534379
        return lambda j: res.append(s.value + j)
Packit 534379
Packit 534379
    # do some work async
Packit 534379
    work = [1, 2, 3, 4]
Packit 534379
    m.test_async_callback(gen_f(), work)
Packit 534379
    # wait until work is done
Packit 534379
    from time import sleep
Packit 534379
    sleep(0.5)
Packit 534379
    assert sum(res) == sum([x + 3 for x in work])
Packit 534379
Packit 534379
Packit 534379
def test_async_async_callbacks():
Packit 534379
    t = Thread(target=test_async_callbacks)
Packit 534379
    t.start()
Packit 534379
    t.join()