1
1
from .. import abc
2
- import os .path
3
2
from .. import util
4
3
5
4
machinery = util .import_importlib ('importlib.machinery' )
6
5
6
+ import _imp
7
+ import marshal
8
+ import os .path
7
9
import unittest
8
10
import warnings
9
11
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 )
11
18
12
19
13
20
class FindSpecTests (abc .FinderTests ):
@@ -19,39 +26,49 @@ def find(self, name, **kwargs):
19
26
with import_helper .frozen_modules ():
20
27
return finder .find_spec (name , ** kwargs )
21
28
22
- def check (self , spec , name ):
29
+ def check (self , spec , name , orig = None ):
23
30
self .assertEqual (spec .name , name )
24
31
self .assertIs (spec .loader , self .machinery .FrozenImporter )
25
32
self .assertEqual (spec .origin , 'frozen' )
26
33
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 )
27
41
28
42
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 :
39
54
with self .subTest (name ):
40
55
spec = self .find (name )
41
56
self .check (spec , name )
42
57
self .assertEqual (spec .submodule_search_locations , None )
58
+ self .check_data (spec , modules [name ])
43
59
44
60
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 :
51
67
with self .subTest (name ):
52
68
spec = self .find (name )
53
69
self .check (spec , name )
54
70
self .assertEqual (spec .submodule_search_locations , [])
71
+ self .check_data (spec , modules [name ])
55
72
56
73
# These are covered by test_module() and test_package().
57
74
test_module_in_package = None
0 commit comments