μHAL (v2.8.17)
Part of the IPbus software repository
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
test_modules.py
Go to the documentation of this file.
1# -*- coding: utf-8 -*-
2from pybind11_tests import ConstructorStats
3from pybind11_tests import modules as m
4from pybind11_tests.modules import subsubmodule as ms
5
6
8 import pybind11_tests
9
10 assert pybind11_tests.__name__ == "pybind11_tests"
11 assert pybind11_tests.modules.__name__ == "pybind11_tests.modules"
12 assert (
13 pybind11_tests.modules.subsubmodule.__name__
14 == "pybind11_tests.modules.subsubmodule"
15 )
16 assert m.__name__ == "pybind11_tests.modules"
17 assert ms.__name__ == "pybind11_tests.modules.subsubmodule"
18
19 assert ms.submodule_func() == "submodule_func()"
20
21
23 b = ms.B()
24 assert str(b.get_a1()) == "A[1]"
25 assert str(b.a1) == "A[1]"
26 assert str(b.get_a2()) == "A[2]"
27 assert str(b.a2) == "A[2]"
28
29 b.a1 = ms.A(42)
30 b.a2 = ms.A(43)
31 assert str(b.get_a1()) == "A[42]"
32 assert str(b.a1) == "A[42]"
33 assert str(b.get_a2()) == "A[43]"
34 assert str(b.a2) == "A[43]"
35
36 astats, bstats = ConstructorStats.get(ms.A), ConstructorStats.get(ms.B)
37 assert astats.alive() == 2
38 assert bstats.alive() == 1
39 del b
40 assert astats.alive() == 0
41 assert bstats.alive() == 0
42 assert astats.values() == ["1", "2", "42", "43"]
43 assert bstats.values() == []
44 assert astats.default_constructions == 0
45 assert bstats.default_constructions == 1
46 assert astats.copy_constructions == 0
47 assert bstats.copy_constructions == 0
48 # assert astats.move_constructions >= 0 # Don't invoke any
49 # assert bstats.move_constructions >= 0 # Don't invoke any
50 assert astats.copy_assignments == 2
51 assert bstats.copy_assignments == 0
52 assert astats.move_assignments == 0
53 assert bstats.move_assignments == 0
54
55
57 from collections import OrderedDict
58
59 from pybind11_tests.modules import OD
60
61 assert OD is OrderedDict
62 assert str(OD([(1, "a"), (2, "b")])) == "OrderedDict([(1, 'a'), (2, 'b')])"
63
64
66 """Pydoc needs to be able to provide help() for everything inside a pybind11 module"""
67 import pydoc
68
69 import pybind11_tests
70
71 assert pybind11_tests.__name__ == "pybind11_tests"
72 assert pybind11_tests.__doc__ == "pybind11 test module"
73 assert pydoc.text.docmodule(pybind11_tests)
74
75
77 """Registering two things with the same name"""
78
79 assert m.duplicate_registration() == []
80
81
83 """Test that all the keys in the builtin modules have type str.
84
85 Previous versions of pybind11 would add a unicode key in python 2.
86 """
87 if hasattr(__builtins__, "keys"):
88 keys = __builtins__.keys()
89 else: # this is to make pypy happy since builtins is different there.
90 keys = __builtins__.__dict__.keys()
91
92 assert {type(k) for k in keys} == {str}
static ConstructorStats & get(std::type_index type)
Definition: pytypes.h:1200
Definition: pytypes.h:1167
bool hasattr(handle obj, handle name)
Definition: pytypes.h:517
def test_nested_modules()
Definition: test_modules.py:7
def test_reference_internal()
Definition: test_modules.py:22
def test_duplicate_registration()
Definition: test_modules.py:76
def test_pydoc()
Definition: test_modules.py:65
def test_importing()
Definition: test_modules.py:56
def test_builtin_key_type()
Definition: test_modules.py:82