test_callbacks.py 3.17 KB
Newer Older
Dean Moldovan's avatar
Dean Moldovan committed
1
2
3
import pytest


4
5
def test_inheritance(msg):
    from pybind11_tests import Pet, Dog, Rabbit, dog_bark, pet_name_species
Dean Moldovan's avatar
Dean Moldovan committed
6
7
8

    roger = Rabbit('Rabbit')
    assert roger.name() + " is a " + roger.species() == "Rabbit is a parrot"
9
    assert pet_name_species(roger) == "Rabbit is a parrot"
Dean Moldovan's avatar
Dean Moldovan committed
10
11
12

    polly = Pet('Polly', 'parrot')
    assert polly.name() + " is a " + polly.species() == "Polly is a parrot"
13
    assert pet_name_species(polly) == "Polly is a parrot"
Dean Moldovan's avatar
Dean Moldovan committed
14
15
16

    molly = Dog('Molly')
    assert molly.name() + " is a " + molly.species() == "Molly is a dog"
17
    assert pet_name_species(molly) == "Molly is a dog"
Dean Moldovan's avatar
Dean Moldovan committed
18

19
    assert dog_bark(molly) == "Woof!"
Dean Moldovan's avatar
Dean Moldovan committed
20
21
22
23
24

    with pytest.raises(TypeError) as excinfo:
        dog_bark(polly)
    assert msg(excinfo.value) == """
        Incompatible function arguments. The following argument types are supported:
25
            1. (arg0: m.Dog) -> str
Dean Moldovan's avatar
Dean Moldovan committed
26
27
28
29
            Invoked with: <m.Pet object at 0>
    """


30
def test_callbacks():
Dean Moldovan's avatar
Dean Moldovan committed
31
32
33
34
35
    from functools import partial
    from pybind11_tests import (test_callback1, test_callback2, test_callback3,
                                test_callback4, test_callback5)

    def func1():
36
        return "func1"
Dean Moldovan's avatar
Dean Moldovan committed
37
38

    def func2(a, b, c, d):
39
        return "func2", a, b, c, d
Dean Moldovan's avatar
Dean Moldovan committed
40
41

    def func3(a):
42
43
44
45
46
47
48
        return "func3({})".format(a)

    assert test_callback1(func1) == "func1"
    assert test_callback2(func2) == ("func2", "Hello", "x", True, 5)
    assert test_callback1(partial(func2, 1, 2, 3, 4)) == ("func2", 1, 2, 3, 4)
    assert test_callback1(partial(func3, "partial")) == "func3(partial)"
    assert test_callback3(lambda i: i + 1) == "func(43) = 44"
Dean Moldovan's avatar
Dean Moldovan committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

    f = test_callback4()
    assert f(43) == 44
    f = test_callback5()
    assert f(number=43) == 44


def test_lambda_closure_cleanup():
    from pybind11_tests import test_cleanup, payload_cstats

    test_cleanup()
    cstats = payload_cstats()
    assert cstats.alive() == 0
    assert cstats.copy_constructions == 1
    assert cstats.move_constructions >= 1


66
def test_cpp_function_roundtrip():
Dean Moldovan's avatar
Dean Moldovan committed
67
68
69
    """Test if passing a function pointer from C++ -> Python -> C++ yields the original pointer"""
    from pybind11_tests import dummy_function, dummy_function2, test_dummy_function, roundtrip

70
71
72
73
    assert test_dummy_function(dummy_function) == "matches dummy_function: eval(1) = 2"
    assert test_dummy_function(roundtrip(dummy_function)) == "matches dummy_function: eval(1) = 2"
    assert roundtrip(None, expect_none=True) is None
    assert test_dummy_function(lambda x: x + 2) == "can't convert to function pointer: eval(1) = 3"
Dean Moldovan's avatar
Dean Moldovan committed
74

75
76
77
    with pytest.raises(TypeError) as excinfo:
        test_dummy_function(dummy_function2)
    assert "Incompatible function arguments" in str(excinfo.value)
Dean Moldovan's avatar
Dean Moldovan committed
78

79
80
81
82
    with pytest.raises(TypeError) as excinfo:
        test_dummy_function(lambda x, y: x + y)
    assert any(s in str(excinfo.value) for s in ("missing 1 required positional argument",
                                                 "takes exactly 2 arguments"))
Dean Moldovan's avatar
Dean Moldovan committed
83
84
85
86
87


def test_function_signatures(doc):
    from pybind11_tests import test_callback3, test_callback4

88
    assert doc(test_callback3) == "test_callback3(arg0: Callable[[int], int]) -> str"
Dean Moldovan's avatar
Dean Moldovan committed
89
    assert doc(test_callback4) == "test_callback4() -> Callable[[int], int]"