| 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 importlib.machinery |
| 14 |
import sys |
| 15 |
import unittest |
| 16 |
from test.support import captured_stdout, import_helper |
| 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 import_helper.frozen_modules(): |
| 25 |
import __hello__ |
| 26 |
with captured_stdout() as out: |
| 27 |
__hello__.main() |
| 28 |
self.assertEqual(out.getvalue(), 'Hello world!\n') |
| 29 |
|
| 30 |
def test_frozen_submodule_in_unfrozen_package(self): |
| 31 |
with import_helper.CleanImport('__phello__', '__phello__.spam'): |
| 32 |
with import_helper.frozen_modules(enabled=False): |
| 33 |
import __phello__ |
| 34 |
with import_helper.frozen_modules(enabled=True): |
| 35 |
import __phello__.spam as spam |
| 36 |
self.assertIs(spam, __phello__.spam) |
| 37 |
self.assertIsNot(__phello__.__spec__.loader, |
| 38 |
importlib.machinery.FrozenImporter) |
| 39 |
self.assertIs(spam.__spec__.loader, |
| 40 |
importlib.machinery.FrozenImporter) |
| 41 |
|
| 42 |
def test_unfrozen_submodule_in_frozen_package(self): |
| 43 |
with import_helper.CleanImport('__phello__', '__phello__.spam'): |
| 44 |
with import_helper.frozen_modules(enabled=True): |
| 45 |
import __phello__ |
| 46 |
with import_helper.frozen_modules(enabled=False): |
| 47 |
import __phello__.spam as spam |
| 48 |
self.assertIs(spam, __phello__.spam) |
| 49 |
self.assertIs(__phello__.__spec__.loader, |
| 50 |
importlib.machinery.FrozenImporter) |
| 51 |
self.assertIsNot(spam.__spec__.loader, |
| 52 |
importlib.machinery.FrozenImporter) |
| 53 |
|
| 54 |
|
| 55 |
if __name__ == '__main__': |
| 56 |
unittest.main() |