Skip to content

Commit 4dc4a4f

Browse files
committed
PyPy only: Skip test with trigger for PyModule_GetName() failure.
1 parent b7e0969 commit 4dc4a4f

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

tests/test_modules.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pytest
22

3+
import env
34
from pybind11_tests import ConstructorStats
45
from pybind11_tests import modules as m
56
from pybind11_tests.modules import subsubmodule as ms
@@ -93,23 +94,28 @@ def test_builtin_key_type():
9394
assert {type(k) for k in keys} == {str}
9495

9596

97+
@pytest.mark.xfail("env.PYPY", reason="PyModule_GetName()")
9698
def test_def_submodule_failures():
9799
sm = m.def_submodule(m, b"ScratchSubModuleName") # Using bytes to show it works.
98100
assert sm.__name__ == m.__name__ + "." + "ScratchSubModuleName"
99101
malformed_utf8 = b"\x80"
100-
# Meant to exercise PyModule_GetName():
101-
sm_name_orig = sm.__name__
102-
sm.__name__ = malformed_utf8
103-
try:
104-
with pytest.raises(Exception):
105-
# Seen with Python 3.9: SystemError: nameless module
106-
# But we do not want to exercise the internals of PyModule_GetName(), which could
107-
# change in future versions of Python, but a bad __name__ is very likely to cause
108-
# some kind of failure indefinitely.
109-
m.def_submodule(sm, b"SubSubModuleName")
110-
finally:
111-
# Clean up to ensure nothing gets upset by a module with an invalid __name__.
112-
sm.__name__ = sm_name_orig # Purely precautionary.
113-
# Meant to exercise PyImport_AddModule():
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:
114120
with pytest.raises(UnicodeDecodeError):
115121
m.def_submodule(sm, malformed_utf8)

0 commit comments

Comments
 (0)