1
1
import sys
2
2
import marshal
3
3
from distutils .version import StrictVersion
4
- from setuptools .extern import six
5
4
6
5
from .py33compat import Bytecode
7
6
8
- if six .PY2 :
9
- import imp
10
- from imp import PKG_DIRECTORY , PY_COMPILED , PY_SOURCE , PY_FROZEN
11
- else :
12
- import os .path
13
- from importlib .util import find_spec , spec_from_loader
14
- from importlib .machinery import SOURCE_SUFFIXES , BYTECODE_SUFFIXES , EXTENSION_SUFFIXES , BuiltinImporter , FrozenImporter
15
- PY_SOURCE = 1
16
- PY_COMPILED = 2
17
- C_EXTENSION = 3
18
- C_BUILTIN = 6
19
- PY_FROZEN = 7
7
+ from .py27compat import find_module , PY_COMPILED , PY_FROZEN , PY_SOURCE
8
+ from . import py27compat
20
9
21
10
22
11
__all__ = [
27
16
class Require :
28
17
"""A prerequisite to building or installing a distribution"""
29
18
30
- def __init__ (self , name , requested_version , module , homepage = '' ,
19
+ def __init__ (
20
+ self , name , requested_version , module , homepage = '' ,
31
21
attribute = None , format = None ):
32
22
33
23
if format is None and requested_version is not None :
@@ -91,63 +81,6 @@ def is_current(self, paths=None):
91
81
return self .version_ok (version )
92
82
93
83
94
- def find_module (module , paths = None ):
95
- """Just like 'imp.find_module()', but with package support"""
96
- if six .PY3 :
97
- spec = find_spec (module , paths )
98
- if spec is None :
99
- raise ImportError ("Can't find %s" % module )
100
- if not spec .has_location and hasattr (spec , 'submodule_search_locations' ):
101
- spec = spec_from_loader ('__init__.py' , spec .loader )
102
-
103
- kind = - 1
104
- file = None
105
- static = isinstance (spec .loader , type )
106
- if spec .origin == 'frozen' or static and issubclass (spec .loader , FrozenImporter ):
107
- kind = PY_FROZEN
108
- path = None # imp compabilty
109
- suffix = mode = '' # imp compability
110
- elif spec .origin == 'built-in' or static and issubclass (spec .loader , BuiltinImporter ):
111
- kind = C_BUILTIN
112
- path = None # imp compabilty
113
- suffix = mode = '' # imp compability
114
- elif spec .has_location :
115
- frozen = False
116
- path = spec .origin
117
- suffix = os .path .splitext (path )[1 ]
118
- mode = 'r' if suffix in SOURCE_SUFFIXES else 'rb'
119
-
120
- if suffix in SOURCE_SUFFIXES :
121
- kind = PY_SOURCE
122
- elif suffix in BYTECODE_SUFFIXES :
123
- kind = PY_COMPILED
124
- elif suffix in EXTENSION_SUFFIXES :
125
- kind = C_EXTENSION
126
-
127
- if kind in {PY_SOURCE , PY_COMPILED }:
128
- file = open (path , mode )
129
- else :
130
- path = None
131
- suffix = mode = ''
132
-
133
- return file , path , (suffix , mode , kind )
134
-
135
- else :
136
- parts = module .split ('.' )
137
- while parts :
138
- part = parts .pop (0 )
139
- f , path , (suffix , mode , kind ) = info = imp .find_module (part , paths )
140
-
141
- if kind == PKG_DIRECTORY :
142
- parts = parts or ['__init__' ]
143
- paths = [path ]
144
-
145
- elif parts :
146
- raise ImportError ("Can't find %r in %s" % (parts , module ))
147
-
148
- return info
149
-
150
-
151
84
def get_module_constant (module , symbol , default = - 1 , paths = None ):
152
85
"""Find 'module' by searching 'paths', and extract 'symbol'
153
86
@@ -156,35 +89,23 @@ def get_module_constant(module, symbol, default=-1, paths=None):
156
89
constant. Otherwise, return 'default'."""
157
90
158
91
try :
159
- f , path , (suffix , mode , kind ) = find_module (module , paths )
92
+ f , path , (suffix , mode , kind ) = info = find_module (module , paths )
160
93
except ImportError :
161
94
# Module doesn't exist
162
95
return None
163
96
164
- if six .PY3 :
165
- spec = find_spec (module , paths )
166
- if hasattr (spec , 'submodule_search_locations' ):
167
- spec = spec_from_loader ('__init__.py' , spec .loader )
168
-
169
97
try :
170
98
if kind == PY_COMPILED :
171
99
f .read (8 ) # skip magic & date
172
100
code = marshal .load (f )
173
101
elif kind == PY_FROZEN :
174
- if six .PY2 :
175
- code = imp .get_frozen_object (module )
176
- else :
177
- code = spec .loader .get_code (module )
102
+ code = py27compat .get_frozen_object (module , paths )
178
103
elif kind == PY_SOURCE :
179
104
code = compile (f .read (), path , 'exec' )
180
105
else :
181
106
# Not something we can parse; we'll have to import it. :(
182
- if module not in sys .modules :
183
- if six .PY2 :
184
- imp .load_module (module , f , path , (suffix , mode , kind ))
185
- else :
186
- sys .modules [module ] = module_from_spec (spec )
187
- return getattr (sys .modules [module ], symbol , None )
107
+ imported = py27compat .get_module (module , paths , info )
108
+ return getattr (imported , symbol , None )
188
109
189
110
finally :
190
111
if f :
0 commit comments