test_modules.py
Go to the documentation of this file.
1 import pytest
2 
3 import env
4 from pybind11_tests import ConstructorStats
5 from pybind11_tests import modules as m
6 from pybind11_tests.modules import subsubmodule as ms
7 
8 
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 
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 
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 
67 def 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 
79  """Registering two things with the same name"""
80 
81  assert m.duplicate_registration() == []
82 
83 
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  with pytest.raises(Exception):
111  # Seen with Python 3.9: SystemError: nameless module
112  # But we do not want to exercise the internals of PyModule_GetName(), which could
113  # change in future versions of Python, but a bad __name__ is very likely to cause
114  # some kind of failure indefinitely.
115  m.def_submodule(sm, b"SubSubModuleName")
116  finally:
117  # Clean up to ensure nothing gets upset by a module with an invalid __name__.
118  sm.__name__ = sm_name_orig # Purely precautionary.
119  # Meant to trigger PyImport_AddModule() failure:
120  with pytest.raises(UnicodeDecodeError):
121  m.def_submodule(sm, malformed_utf8)
bool hasattr(handle obj, handle name)
Definition: pytypes.h:728
def test_importing()
Definition: test_modules.py:58
def test_reference_internal()
Definition: test_modules.py:24
def test_def_submodule_failures()
Definition: test_modules.py:98
static ConstructorStats & get(std::type_index type)
Definition: pytypes.h:1403
def test_nested_modules()
Definition: test_modules.py:9
def test_pydoc()
Definition: test_modules.py:67
def test_duplicate_registration()
Definition: test_modules.py:78
def test_builtin_key_type()
Definition: test_modules.py:84
Definition: pytypes.h:1370


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:37:46