File indexing completed on 2025-01-18 10:17:54
0001 import pytest
0002
0003 import env
0004 from pybind11_tests import ConstructorStats
0005 from pybind11_tests import modules as m
0006 from pybind11_tests.modules import subsubmodule as ms
0007
0008
0009 def test_nested_modules():
0010 import pybind11_tests
0011
0012 assert pybind11_tests.__name__ == "pybind11_tests"
0013 assert pybind11_tests.modules.__name__ == "pybind11_tests.modules"
0014 assert (
0015 pybind11_tests.modules.subsubmodule.__name__
0016 == "pybind11_tests.modules.subsubmodule"
0017 )
0018 assert m.__name__ == "pybind11_tests.modules"
0019 assert ms.__name__ == "pybind11_tests.modules.subsubmodule"
0020
0021 assert ms.submodule_func() == "submodule_func()"
0022
0023
0024 def test_reference_internal():
0025 b = ms.B()
0026 assert str(b.get_a1()) == "A[1]"
0027 assert str(b.a1) == "A[1]"
0028 assert str(b.get_a2()) == "A[2]"
0029 assert str(b.a2) == "A[2]"
0030
0031 b.a1 = ms.A(42)
0032 b.a2 = ms.A(43)
0033 assert str(b.get_a1()) == "A[42]"
0034 assert str(b.a1) == "A[42]"
0035 assert str(b.get_a2()) == "A[43]"
0036 assert str(b.a2) == "A[43]"
0037
0038 astats, bstats = ConstructorStats.get(ms.A), ConstructorStats.get(ms.B)
0039 assert astats.alive() == 2
0040 assert bstats.alive() == 1
0041 del b
0042 assert astats.alive() == 0
0043 assert bstats.alive() == 0
0044 assert astats.values() == ["1", "2", "42", "43"]
0045 assert bstats.values() == []
0046 assert astats.default_constructions == 0
0047 assert bstats.default_constructions == 1
0048 assert astats.copy_constructions == 0
0049 assert bstats.copy_constructions == 0
0050
0051
0052 assert astats.copy_assignments == 2
0053 assert bstats.copy_assignments == 0
0054 assert astats.move_assignments == 0
0055 assert bstats.move_assignments == 0
0056
0057
0058 def test_importing():
0059 from collections import OrderedDict
0060
0061 from pybind11_tests.modules import OD
0062
0063 assert OD is OrderedDict
0064 assert str(OD([(1, "a"), (2, "b")])) == "OrderedDict([(1, 'a'), (2, 'b')])"
0065
0066
0067 def test_pydoc():
0068 """Pydoc needs to be able to provide help() for everything inside a pybind11 module"""
0069 import pydoc
0070
0071 import pybind11_tests
0072
0073 assert pybind11_tests.__name__ == "pybind11_tests"
0074 assert pybind11_tests.__doc__ == "pybind11 test module"
0075 assert pydoc.text.docmodule(pybind11_tests)
0076
0077
0078 def test_duplicate_registration():
0079 """Registering two things with the same name"""
0080
0081 assert m.duplicate_registration() == []
0082
0083
0084 def test_builtin_key_type():
0085 """Test that all the keys in the builtin modules have type str.
0086
0087 Previous versions of pybind11 would add a unicode key in python 2.
0088 """
0089 if hasattr(__builtins__, "keys"):
0090 keys = __builtins__.keys()
0091 else:
0092 keys = __builtins__.__dict__.keys()
0093
0094 assert {type(k) for k in keys} == {str}
0095
0096
0097 @pytest.mark.xfail("env.PYPY", reason="PyModule_GetName()")
0098 def test_def_submodule_failures():
0099 sm = m.def_submodule(m, b"ScratchSubModuleName")
0100 assert sm.__name__ == m.__name__ + "." + "ScratchSubModuleName"
0101 malformed_utf8 = b"\x80"
0102 if env.PYPY:
0103
0104 pytest.skip("Sufficiently exercised on platforms other than PyPy.")
0105 else:
0106
0107 sm_name_orig = sm.__name__
0108 sm.__name__ = malformed_utf8
0109 try:
0110 with pytest.raises(Exception):
0111
0112
0113
0114
0115 m.def_submodule(sm, b"SubSubModuleName")
0116 finally:
0117
0118 sm.__name__ = sm_name_orig
0119
0120 with pytest.raises(UnicodeDecodeError):
0121 m.def_submodule(sm, malformed_utf8)