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


gtsam
Author(s):
autogenerated on Sun Dec 22 2024 04:15:58