Skip to content

Commit c6e70cc

Browse files
Add tests.
1 parent cdbc611 commit c6e70cc

File tree

2 files changed

+43
-19
lines changed

2 files changed

+43
-19
lines changed

Lib/test/test_importlib/frozen/test_finder.py

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
from .. import abc
2-
import os.path
32
from .. import util
43

54
machinery = util.import_importlib('importlib.machinery')
65

6+
import _imp
7+
import marshal
8+
import os.path
79
import unittest
810
import warnings
911

10-
from test.support import import_helper
12+
from test.support import import_helper, REPO_ROOT
13+
14+
15+
def get_frozen_data(name, source=None):
16+
with import_helper.frozen_modules():
17+
return _imp.get_frozen_object(name)
1118

1219

1320
class FindSpecTests(abc.FinderTests):
@@ -19,39 +26,49 @@ def find(self, name, **kwargs):
1926
with import_helper.frozen_modules():
2027
return finder.find_spec(name, **kwargs)
2128

22-
def check(self, spec, name):
29+
def check(self, spec, name, orig=None):
2330
self.assertEqual(spec.name, name)
2431
self.assertIs(spec.loader, self.machinery.FrozenImporter)
2532
self.assertEqual(spec.origin, 'frozen')
2633
self.assertFalse(spec.has_location)
34+
self.assertIsNotNone(spec.loader_state)
35+
36+
def check_data(self, spec, source=None):
37+
expected = get_frozen_data(spec.name, source)
38+
data, = spec.loader_state
39+
code = marshal.loads(data)
40+
self.assertEqual(code, expected)
2741

2842
def test_module(self):
29-
names = [
30-
'__hello__',
31-
'__hello_alias__',
32-
'__hello_only__',
33-
'__phello__.__init__',
34-
'__phello__.spam',
35-
'__phello__.ham.__init__',
36-
'__phello__.ham.eggs',
37-
]
38-
for name in names:
43+
FROZEN_ONLY = os.path.join(REPO_ROOT, 'Tools', 'freeze', 'flag.py')
44+
modules = {
45+
'__hello__': None,
46+
'__phello__.__init__': None,
47+
'__phello__.spam': None,
48+
'__phello__.ham.__init__': None,
49+
'__phello__.ham.eggs': None,
50+
'__hello_alias__': '__hello__',
51+
'__hello_only__': FROZEN_ONLY,
52+
}
53+
for name in modules:
3954
with self.subTest(name):
4055
spec = self.find(name)
4156
self.check(spec, name)
4257
self.assertEqual(spec.submodule_search_locations, None)
58+
self.check_data(spec, modules[name])
4359

4460
def test_package(self):
45-
names = [
46-
'__phello__',
47-
'__phello__.ham',
48-
'__phello_alias__',
49-
]
50-
for name in names:
61+
modules = {
62+
'__phello__': None,
63+
'__phello__.ham': None,
64+
'__phello_alias__': '__hello__',
65+
}
66+
for name in modules:
5167
with self.subTest(name):
5268
spec = self.find(name)
5369
self.check(spec, name)
5470
self.assertEqual(spec.submodule_search_locations, [])
71+
self.check_data(spec, modules[name])
5572

5673
# These are covered by test_module() and test_package().
5774
test_module_in_package = None

Lib/test/test_importlib/frozen/test_loader.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
machinery = util.import_importlib('importlib.machinery')
55

66
from test.support import captured_stdout, import_helper
7+
import _imp
78
import contextlib
9+
import marshal
810
import types
911
import unittest
1012
import warnings
@@ -33,11 +35,14 @@ class ExecModuleTests(abc.LoaderTests):
3335
def exec_module(self, name):
3436
with import_helper.frozen_modules():
3537
is_package = self.machinery.FrozenImporter.is_package(name)
38+
code = _imp.get_frozen_object(name)
39+
data = marshal.dumps(code)
3640
spec = self.machinery.ModuleSpec(
3741
name,
3842
self.machinery.FrozenImporter,
3943
origin='frozen',
4044
is_package=is_package,
45+
loader_state=(data,),
4146
)
4247
module = types.ModuleType(name)
4348
module.__spec__ = spec
@@ -61,6 +66,7 @@ def test_module(self):
6166
self.assertEqual(getattr(module, attr), value)
6267
self.assertEqual(output, 'Hello world!\n')
6368
self.assertTrue(hasattr(module, '__spec__'))
69+
self.assertIsNone(module.__spec__.loader_state)
6470

6571
def test_package(self):
6672
name = '__phello__'
@@ -73,6 +79,7 @@ def test_package(self):
7379
name=name, attr=attr, given=attr_value,
7480
expected=value))
7581
self.assertEqual(output, 'Hello world!\n')
82+
self.assertIsNone(module.__spec__.loader_state)
7683

7784
def test_lacking_parent(self):
7885
name = '__phello__.spam'

0 commit comments

Comments
 (0)