μ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.
1import pytest
2
3import env
4from pybind11_tests import ConstructorStats
5from pybind11_tests import modules as m
6from pybind11_tests.modules import subsubmodule as ms
7
8
9def test_nested_modules():
10 import pybind11_tests
11
12 assert pybind11_tests.__name__ == "pybind11_tests"
13 assert pybind11_tests.modules.__name__ == "pybind11_tests.modules"
14 assert (
15 pybind11_tests.modules.subsubmodule.__name__
16 == "pybind11_tests.modules.subsubmodule"
17 )
18 assert m.__name__ == "pybind11_tests.modules"
19 assert ms.__name__ == "pybind11_tests.modules.subsubmodule"
20
21 assert ms.submodule_func() == "submodule_func()"
22
23
24def test_reference_internal():
25 b = ms.B()
26 assert str(b.get_a1()) == "A[1]"
27 assert str(b.a1) == "A[1]"
28 assert str(b.get_a2()) == "A[2]"
29 assert str(b.a2) == "A[2]"
30
31 b.a1 = ms.A(42)
32 b.a2 = ms.A(43)
33 assert str(b.get_a1()) == "A[42]"
34 assert str(b.a1) == "A[42]"
35 assert str(b.get_a2()) == "A[43]"
36 assert str(b.a2) == "A[43]"
37
38 astats, bstats = ConstructorStats.get(ms.A), ConstructorStats.get(ms.B)
39 assert astats.alive() == 2
40 assert bstats.alive() == 1
41 del b
42 assert astats.alive() == 0
43 assert bstats.alive() == 0
44 assert astats.values() == ["1", "2", "42", "43"]
45 assert bstats.values() == []
46 assert astats.default_constructions == 0
47 assert bstats.default_constructions == 1
48 assert astats.copy_constructions == 0
49 assert bstats.copy_constructions == 0
50 # assert astats.move_constructions >= 0 # Don't invoke any
51 # assert bstats.move_constructions >= 0 # Don't invoke any
52 assert astats.copy_assignments == 2
53 assert bstats.copy_assignments == 0
54 assert astats.move_assignments == 0
55 assert bstats.move_assignments == 0
56
57
58def test_importing():
59 from collections import OrderedDict
60
61 from pybind11_tests.modules import OD
62
63 assert OD is OrderedDict
64 assert str(OD([(1, "a"), (2, "b")])) == "OrderedDict([(1, 'a'), (2, 'b')])"
65
66
67def test_pydoc():
68 """Pydoc needs to be able to provide help() for everything inside a pybind11 module"""
69 import pydoc
70
71 import pybind11_tests
72
73 assert pybind11_tests.__name__ == "pybind11_tests"
74 assert pybind11_tests.__doc__ == "pybind11 test module"
75 assert pydoc.text.docmodule(pybind11_tests)
76
77
78def test_duplicate_registration():
79 """Registering two things with the same name"""
80
81 assert m.duplicate_registration() == []
82
83
84def test_builtin_key_type():
85 """Test that all the keys in the builtin modules have type str.
86
87 Previous versions of pybind11 would add a unicode key in python 2.
88 """
89 if hasattr(__builtins__, "keys"):
90 keys = __builtins__.keys()
91 else: # this is to make pypy happy since builtins is different there.
92 keys = __builtins__.__dict__.keys()
93
94 assert {type(k) for k in keys} == {str}
95
96
97@pytest.mark.xfail("env.PYPY", reason="PyModule_GetName()")
99 sm = m.def_submodule(m, b"ScratchSubModuleName") # Using bytes to show it works.
100 assert sm.__name__ == m.__name__ + "." + "ScratchSubModuleName"
101 malformed_utf8 = b"\x80"
102 if env.PYPY:
103 # It is not worth the effort finding a trigger for a failure when running with PyPy.
104 pytest.skip("Sufficiently exercised on platforms other than PyPy.")
105 else:
106 # Meant to trigger PyModule_GetName() failure:
107 sm_name_orig = sm.__name__
108 sm.__name__ = malformed_utf8
109 try:
110 # We want to assert that a bad __name__ causes some kind of failure, although we do not want to exercise
111 # the internals of PyModule_GetName(). Currently all supported Python versions raise SystemError. If that
112 # changes in future Python versions, simply add the new expected exception types here.
113 with pytest.raises(SystemError):
114 m.def_submodule(sm, b"SubSubModuleName")
115 finally:
116 # Clean up to ensure nothing gets upset by a module with an invalid __name__.
117 sm.__name__ = sm_name_orig # Purely precautionary.
118 # Meant to trigger PyImport_AddModule() failure:
119 with pytest.raises(UnicodeDecodeError):
120 m.def_submodule(sm, malformed_utf8)
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_def_submodule_failures()
Definition: test_modules.py:98