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


gtsam
Author(s):
autogenerated on Tue Jun 25 2024 03:05:28