| OLD | NEW |
| (Empty) |
| 1 """Astroid hooks for the Python 2 standard library. | |
| 2 | |
| 3 Currently help understanding of : | |
| 4 | |
| 5 * hashlib.md5 and hashlib.sha1 | |
| 6 """ | |
| 7 | |
| 8 import sys | |
| 9 from textwrap import dedent | |
| 10 | |
| 11 from astroid import ( | |
| 12 MANAGER, AsStringRegexpPredicate, | |
| 13 UseInferenceDefault, inference_tip, | |
| 14 YES, InferenceError) | |
| 15 from astroid import exceptions | |
| 16 from astroid import nodes | |
| 17 from astroid.builder import AstroidBuilder | |
| 18 | |
| 19 MODULE_TRANSFORMS = {} | |
| 20 PY3K = sys.version_info > (3, 0) | |
| 21 PY33 = sys.version_info >= (3, 3) | |
| 22 | |
| 23 # general function | |
| 24 | |
| 25 def infer_func_form(node, base_type, context=None, enum=False): | |
| 26 """Specific inference function for namedtuple or Python 3 enum. """ | |
| 27 def infer_first(node): | |
| 28 try: | |
| 29 value = node.infer(context=context).next() | |
| 30 if value is YES: | |
| 31 raise UseInferenceDefault() | |
| 32 else: | |
| 33 return value | |
| 34 except StopIteration: | |
| 35 raise InferenceError() | |
| 36 | |
| 37 # node is a CallFunc node, class name as first argument and generated class | |
| 38 # attributes as second argument | |
| 39 if len(node.args) != 2: | |
| 40 # something weird here, go back to class implementation | |
| 41 raise UseInferenceDefault() | |
| 42 # namedtuple or enums list of attributes can be a list of strings or a | |
| 43 # whitespace-separate string | |
| 44 try: | |
| 45 name = infer_first(node.args[0]).value | |
| 46 names = infer_first(node.args[1]) | |
| 47 try: | |
| 48 attributes = names.value.replace(',', ' ').split() | |
| 49 except AttributeError: | |
| 50 if not enum: | |
| 51 attributes = [infer_first(const).value for const in names.elts] | |
| 52 else: | |
| 53 # Enums supports either iterator of (name, value) pairs | |
| 54 # or mappings. | |
| 55 # TODO: support only list, tuples and mappings. | |
| 56 if hasattr(names, 'items') and isinstance(names.items, list): | |
| 57 attributes = [infer_first(const[0]).value | |
| 58 for const in names.items | |
| 59 if isinstance(const[0], nodes.Const)] | |
| 60 elif hasattr(names, 'elts'): | |
| 61 # Enums can support either ["a", "b", "c"] | |
| 62 # or [("a", 1), ("b", 2), ...], but they can't | |
| 63 # be mixed. | |
| 64 if all(isinstance(const, nodes.Tuple) | |
| 65 for const in names.elts): | |
| 66 attributes = [infer_first(const.elts[0]).value | |
| 67 for const in names.elts | |
| 68 if isinstance(const, nodes.Tuple)] | |
| 69 else: | |
| 70 attributes = [infer_first(const).value | |
| 71 for const in names.elts] | |
| 72 else: | |
| 73 raise AttributeError | |
| 74 if not attributes: | |
| 75 raise AttributeError | |
| 76 except (AttributeError, exceptions.InferenceError) as exc: | |
| 77 raise UseInferenceDefault() | |
| 78 # we want to return a Class node instance with proper attributes set | |
| 79 class_node = nodes.Class(name, 'docstring') | |
| 80 class_node.parent = node.parent | |
| 81 # set base class=tuple | |
| 82 class_node.bases.append(base_type) | |
| 83 # XXX add __init__(*attributes) method | |
| 84 for attr in attributes: | |
| 85 fake_node = nodes.EmptyNode() | |
| 86 fake_node.parent = class_node | |
| 87 class_node.instance_attrs[attr] = [fake_node] | |
| 88 return class_node, name, attributes | |
| 89 | |
| 90 | |
| 91 # module specific transformation functions ##################################### | |
| 92 | |
| 93 def transform(module): | |
| 94 try: | |
| 95 tr = MODULE_TRANSFORMS[module.name] | |
| 96 except KeyError: | |
| 97 pass | |
| 98 else: | |
| 99 tr(module) | |
| 100 MANAGER.register_transform(nodes.Module, transform) | |
| 101 | |
| 102 # module specific transformation functions ##################################### | |
| 103 | |
| 104 def hashlib_transform(module): | |
| 105 template = ''' | |
| 106 | |
| 107 class %s(object): | |
| 108 def __init__(self, value=''): pass | |
| 109 def digest(self): | |
| 110 return u'' | |
| 111 def update(self, value): pass | |
| 112 def hexdigest(self): | |
| 113 return u'' | |
| 114 ''' | |
| 115 | |
| 116 algorithms = ('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512') | |
| 117 classes = "".join(template % hashfunc for hashfunc in algorithms) | |
| 118 | |
| 119 fake = AstroidBuilder(MANAGER).string_build(classes) | |
| 120 | |
| 121 for hashfunc in algorithms: | |
| 122 module.locals[hashfunc] = fake.locals[hashfunc] | |
| 123 | |
| 124 def collections_transform(module): | |
| 125 fake = AstroidBuilder(MANAGER).string_build(''' | |
| 126 | |
| 127 class defaultdict(dict): | |
| 128 default_factory = None | |
| 129 def __missing__(self, key): pass | |
| 130 | |
| 131 class deque(object): | |
| 132 maxlen = 0 | |
| 133 def __init__(self, iterable=None, maxlen=None): pass | |
| 134 def append(self, x): pass | |
| 135 def appendleft(self, x): pass | |
| 136 def clear(self): pass | |
| 137 def count(self, x): return 0 | |
| 138 def extend(self, iterable): pass | |
| 139 def extendleft(self, iterable): pass | |
| 140 def pop(self): pass | |
| 141 def popleft(self): pass | |
| 142 def remove(self, value): pass | |
| 143 def reverse(self): pass | |
| 144 def rotate(self, n): pass | |
| 145 def __iter__(self): return self | |
| 146 | |
| 147 ''') | |
| 148 | |
| 149 for klass in ('deque', 'defaultdict'): | |
| 150 module.locals[klass] = fake.locals[klass] | |
| 151 | |
| 152 def pkg_resources_transform(module): | |
| 153 fake = AstroidBuilder(MANAGER).string_build(''' | |
| 154 | |
| 155 def resource_exists(package_or_requirement, resource_name): | |
| 156 pass | |
| 157 | |
| 158 def resource_isdir(package_or_requirement, resource_name): | |
| 159 pass | |
| 160 | |
| 161 def resource_filename(package_or_requirement, resource_name): | |
| 162 pass | |
| 163 | |
| 164 def resource_stream(package_or_requirement, resource_name): | |
| 165 pass | |
| 166 | |
| 167 def resource_string(package_or_requirement, resource_name): | |
| 168 pass | |
| 169 | |
| 170 def resource_listdir(package_or_requirement, resource_name): | |
| 171 pass | |
| 172 | |
| 173 def extraction_error(): | |
| 174 pass | |
| 175 | |
| 176 def get_cache_path(archive_name, names=()): | |
| 177 pass | |
| 178 | |
| 179 def postprocess(tempname, filename): | |
| 180 pass | |
| 181 | |
| 182 def set_extraction_path(path): | |
| 183 pass | |
| 184 | |
| 185 def cleanup_resources(force=False): | |
| 186 pass | |
| 187 | |
| 188 ''') | |
| 189 | |
| 190 for func_name, func in fake.locals.items(): | |
| 191 module.locals[func_name] = func | |
| 192 | |
| 193 | |
| 194 def subprocess_transform(module): | |
| 195 if PY3K: | |
| 196 communicate = (bytes('string', 'ascii'), bytes('string', 'ascii')) | |
| 197 init = """ | |
| 198 def __init__(self, args, bufsize=0, executable=None, | |
| 199 stdin=None, stdout=None, stderr=None, | |
| 200 preexec_fn=None, close_fds=False, shell=False, | |
| 201 cwd=None, env=None, universal_newlines=False, | |
| 202 startupinfo=None, creationflags=0, restore_signals=True, | |
| 203 start_new_session=False, pass_fds=()): | |
| 204 pass | |
| 205 """ | |
| 206 else: | |
| 207 communicate = ('string', 'string') | |
| 208 init = """ | |
| 209 def __init__(self, args, bufsize=0, executable=None, | |
| 210 stdin=None, stdout=None, stderr=None, | |
| 211 preexec_fn=None, close_fds=False, shell=False, | |
| 212 cwd=None, env=None, universal_newlines=False, | |
| 213 startupinfo=None, creationflags=0): | |
| 214 pass | |
| 215 """ | |
| 216 if PY33: | |
| 217 wait_signature = 'def wait(self, timeout=None)' | |
| 218 else: | |
| 219 wait_signature = 'def wait(self)' | |
| 220 fake = AstroidBuilder(MANAGER).string_build(''' | |
| 221 | |
| 222 class Popen(object): | |
| 223 returncode = pid = 0 | |
| 224 stdin = stdout = stderr = file() | |
| 225 | |
| 226 %(init)s | |
| 227 | |
| 228 def communicate(self, input=None): | |
| 229 return %(communicate)r | |
| 230 %(wait_signature)s: | |
| 231 return self.returncode | |
| 232 def poll(self): | |
| 233 return self.returncode | |
| 234 def send_signal(self, signal): | |
| 235 pass | |
| 236 def terminate(self): | |
| 237 pass | |
| 238 def kill(self): | |
| 239 pass | |
| 240 ''' % {'init': init, | |
| 241 'communicate': communicate, | |
| 242 'wait_signature': wait_signature}) | |
| 243 | |
| 244 for func_name, func in fake.locals.items(): | |
| 245 module.locals[func_name] = func | |
| 246 | |
| 247 | |
| 248 | |
| 249 MODULE_TRANSFORMS['hashlib'] = hashlib_transform | |
| 250 MODULE_TRANSFORMS['collections'] = collections_transform | |
| 251 MODULE_TRANSFORMS['pkg_resources'] = pkg_resources_transform | |
| 252 MODULE_TRANSFORMS['subprocess'] = subprocess_transform | |
| 253 | |
| 254 # namedtuple support ########################################################### | |
| 255 | |
| 256 def infer_named_tuple(node, context=None): | |
| 257 """Specific inference function for namedtuple CallFunc node""" | |
| 258 class_node, name, attributes = infer_func_form(node, nodes.Tuple._proxied, | |
| 259 context=context) | |
| 260 fake = AstroidBuilder(MANAGER).string_build(''' | |
| 261 class %(name)s(tuple): | |
| 262 _fields = %(fields)r | |
| 263 def _asdict(self): | |
| 264 return self.__dict__ | |
| 265 @classmethod | |
| 266 def _make(cls, iterable, new=tuple.__new__, len=len): | |
| 267 return new(cls, iterable) | |
| 268 def _replace(_self, **kwds): | |
| 269 result = _self._make(map(kwds.pop, %(fields)r, _self)) | |
| 270 if kwds: | |
| 271 raise ValueError('Got unexpected field names: %%r' %% list(kwds)) | |
| 272 return result | |
| 273 ''' % {'name': name, 'fields': attributes}) | |
| 274 class_node.locals['_asdict'] = fake.body[0].locals['_asdict'] | |
| 275 class_node.locals['_make'] = fake.body[0].locals['_make'] | |
| 276 class_node.locals['_replace'] = fake.body[0].locals['_replace'] | |
| 277 class_node.locals['_fields'] = fake.body[0].locals['_fields'] | |
| 278 # we use UseInferenceDefault, we can't be a generator so return an iterator | |
| 279 return iter([class_node]) | |
| 280 | |
| 281 def infer_enum(node, context=None): | |
| 282 """ Specific inference function for enum CallFunc node. """ | |
| 283 enum_meta = nodes.Class("EnumMeta", 'docstring') | |
| 284 class_node = infer_func_form(node, enum_meta, | |
| 285 context=context, enum=True)[0] | |
| 286 return iter([class_node.instanciate_class()]) | |
| 287 | |
| 288 def infer_enum_class(node, context=None): | |
| 289 """ Specific inference for enums. """ | |
| 290 names = set(('Enum', 'IntEnum', 'enum.Enum', 'enum.IntEnum')) | |
| 291 for basename in node.basenames: | |
| 292 # TODO: doesn't handle subclasses yet. | |
| 293 if basename not in names: | |
| 294 continue | |
| 295 if node.root().name == 'enum': | |
| 296 # Skip if the class is directly from enum module. | |
| 297 break | |
| 298 for local, values in node.locals.items(): | |
| 299 if any(not isinstance(value, nodes.AssName) | |
| 300 for value in values): | |
| 301 continue | |
| 302 parent = values[0].parent | |
| 303 real_value = parent.value | |
| 304 new_targets = [] | |
| 305 for target in parent.targets: | |
| 306 # Replace all the assignments with our mocked class. | |
| 307 classdef = dedent(''' | |
| 308 class %(name)s(object): | |
| 309 @property | |
| 310 def value(self): | |
| 311 return %(value)s | |
| 312 @property | |
| 313 def name(self): | |
| 314 return %(name)r | |
| 315 %(name)s = %(value)s | |
| 316 ''' % {'name': target.name, | |
| 317 'value': real_value.as_string()}) | |
| 318 fake = AstroidBuilder(MANAGER).string_build(classdef)[target.nam
e] | |
| 319 fake.parent = target.parent | |
| 320 for method in node.mymethods(): | |
| 321 fake.locals[method.name] = [method] | |
| 322 new_targets.append(fake.instanciate_class()) | |
| 323 node.locals[local] = new_targets | |
| 324 break | |
| 325 return node | |
| 326 | |
| 327 MANAGER.register_transform(nodes.CallFunc, inference_tip(infer_named_tuple), | |
| 328 AsStringRegexpPredicate('namedtuple', 'func')) | |
| 329 MANAGER.register_transform(nodes.CallFunc, inference_tip(infer_enum), | |
| 330 AsStringRegexpPredicate('Enum', 'func')) | |
| 331 MANAGER.register_transform(nodes.Class, infer_enum_class) | |
| OLD | NEW |