Skip to content

Commit 1eaa116

Browse files
authored
Add a minimal unit test for Python/frozen.c. (#2995)
If the marshal or bytecode formats get changed, frozen.c needs to be updated as well. It can be easy to miss this step and not doing so can cause test_importlib to crash in mysterious ways. Add an explict unit test to make it easier to track down the problem.
1 parent 13badcb commit 1eaa116

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Lib/test/test_frozen.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""Basic test of the frozen module (source is in Python/frozen.c)."""
2+
3+
# The Python/frozen.c source code contains a marshalled Python module
4+
# and therefore depends on the marshal format as well as the bytecode
5+
# format. If those formats have been changed then frozen.c needs to be
6+
# updated.
7+
#
8+
# The test_importlib also tests this module but because those tests
9+
# are much more complicated, it might be unclear why they are failing.
10+
# Invalid marshalled data in frozen.c could case the interpreter to
11+
# crash when __hello__ is imported.
12+
13+
import sys
14+
import unittest
15+
from test.support import captured_stdout
16+
from importlib import util
17+
18+
19+
class TestFrozen(unittest.TestCase):
20+
def test_frozen(self):
21+
name = '__hello__'
22+
if name in sys.modules:
23+
del sys.modules[name]
24+
with captured_stdout() as out:
25+
import __hello__
26+
self.assertEqual(out.getvalue(), 'Hello world!\n')
27+
28+
29+
if __name__ == '__main__':
30+
unittest.main()

0 commit comments

Comments
 (0)