MCPcopy Create free account
hub / github.com/pybind/pybind11 / test_def_submodule_failures

Function test_def_submodule_failures

tests/test_modules.py:124–146  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

122
123@pytest.mark.xfail("env.PYPY", reason="PyModule_GetName()")
124def test_def_submodule_failures():
125 sm = m.def_submodule(m, b"ScratchSubModuleName") # Using bytes to show it works.
126 assert sm.__name__ == m.__name__ + "." + "ScratchSubModuleName"
127 malformed_utf8 = b"\x80"
128 if env.PYPY or env.GRAALPY:
129 # It is not worth the effort finding a trigger for a failure when running with PyPy.
130 pytest.skip("Sufficiently exercised on platforms other than PyPy/GraalPy.")
131 else:
132 # Meant to trigger PyModule_GetName() failure:
133 sm_name_orig = sm.__name__
134 sm.__name__ = malformed_utf8
135 try:
136 # We want to assert that a bad __name__ causes some kind of failure, although we do not want to exercise
137 # the internals of PyModule_GetName(). Currently all supported Python versions raise SystemError. If that
138 # changes in future Python versions, simply add the new expected exception types here.
139 with pytest.raises(SystemError):
140 m.def_submodule(sm, b"SubSubModuleName")
141 finally:
142 # Clean up to ensure nothing gets upset by a module with an invalid __name__.
143 sm.__name__ = sm_name_orig # Purely precautionary.
144 # Meant to trigger PyImport_AddModule() failure:
145 with pytest.raises(UnicodeDecodeError):
146 m.def_submodule(sm, malformed_utf8)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected