Ejemplo n.º 1
0
def function_to_method(n, klass):
    if isinstance(n, Function):
        if n.type == 'classmethod':
            return BoundMethod(n, klass)
        if n.type != 'staticmethod':
            return UnboundMethod(n)
    return n
Ejemplo n.º 2
0
def _multiprocessing_transform():
    module = parse("""
    from multiprocessing.managers import SyncManager
    def Manager():
        return SyncManager()
    """)
    # Multiprocessing uses a getattr lookup inside contexts,
    # in order to get the attributes they need. Since it's extremely
    # dynamic, we use this approach to fake it.
    node = parse("""
    from multiprocessing.context import DefaultContext, BaseContext
    default = DefaultContext()
    base = BaseContext()
    """)
    try:
        context = next(node["default"].infer())
        base = next(node["base"].infer())
    except (InferenceError, StopIteration):
        return module

    for node in (context, base):
        for key, value in node.locals.items():
            if key.startswith("_"):
                continue

            value = value[0]
            if isinstance(value, FunctionDef):
                # We need to rebound this, since otherwise
                # it will have an extra argument (self).
                value = BoundMethod(value, node)
            module[key] = value
    return module
Ejemplo n.º 3
0
    def igetattr(self, name, context=None):
        """Retrieve the inferred values of the given attribute name."""

        local_name = self._model.get(name)
        if local_name:
            yield local_name
            return

        try:
            mro = self.super_mro()
        except (MroError, SuperError) as exc:
            # Don't let invalid MROs or invalid super calls
            # to leak out as is from this function.
            six.raise_from(NotFoundError, exc)

        found = False
        for cls in mro:
            if name not in cls._locals:
                continue

            found = True
            for infered in _infer_stmts([cls[name]], context, frame=self):
                if not isinstance(infered, FunctionDef):
                    yield infered
                    continue

                # We can obtain different descriptors from a super depending
                # on what we are accessing and where the super call is.
                if infered.type == 'classmethod':
                    yield BoundMethod(infered, cls)
                elif self._scope.type == 'classmethod' and infered.type == 'method':
                    yield infered
                elif self._class_based or infered.type == 'staticmethod':
                    yield infered
                elif _is_property(infered):
                    # TODO: support other descriptors as well.
                    for value in infered.infer_call_result(self, context):
                        yield value
                else:
                    yield BoundMethod(infered, cls)

        if not found:
            raise NotFoundError(name)