Esempio n. 1
0
    def __getstate__(self):
        """
    The name of some attributes may start with a double underscore such as
    cif_types.comp_comp_id.__rotamer_info. Python name mangling will rename such
    an attribute to _comp_comp_id_rotamer_info. Our __getstate__ function would then
    complain that the __slots__ list contains the non-existent attribute __rotamer_info.
    To fix this we manually mangle attributes with the compiler.misc.mangle function
    which does the right name mangling.
    """
        import warnings
        warning_filters = warnings.filters[:]
        show_warning = warnings.showwarning

        try:
            # avoid printing deprecation warning to stderr when loading mangle
            warnings.simplefilter("ignore")
            from compiler.misc import mangle

        finally:
            warnings.showwarning = show_warning
            warnings.filters = warning_filters

        mnames = [
            mangle(name, self.__class__.__name__) for name in self.__slots__
        ]

        return dict([(name, getattr(self, name)) for name in mnames])
 def __call__(self, *args, **kw):
     obj = args[0]
     try:
         cache = obj.__dict__[mangle('__cache', self.__class__.__name__)]
     except KeyError:
         cache = obj.__cache = {}
     key = (self.func, args[1:], frozenset(kw.items()))
     try:
         res = cache[key]
     except KeyError:
         res = cache[key] = self.func(*args, **kw)
     return res
Esempio n. 3
0
"""Module symbol-table generator"""
Esempio n. 4
0
 def mangle(self, name):
     if self.class_name is not None:
         return misc.mangle(name, self.class_name)
     else:
         return name
Esempio n. 5
0
 def mangle(self, name):
     if self.klass is None:
         return name
     return mangle(name, self.klass)
Esempio n. 6
0
 def mangle(self, name):
     if self.klass is None:
         return name
     else:
         return mangle(name, self.klass)
Esempio n. 7
0
"""Module symbol-table generator"""
Esempio n. 8
0
    def check(self, file, checker):
        def visit_with_self(Visitor, method):
            if not method.node.argnames:
                return {}
            return walk(method.node, Visitor(method.node.argnames[0])).result

        # for all class scopes
        for node, scope in file.class_scopes():
            init_attributes = None  # attributes initilized in __init__
            attributes = {}  # "self.foo = " kinda things
            methods = {}  # methods -> scopes

            # get attributes defined on self
            for m in _get_methods(scope):
                defs = visit_with_self(GetDefs, m)
                if m.name == "__init__":
                    init_attributes = defs
                attributes.update(defs)
                methods[mangle(m.name, scope.name)] = m

            # complain about attributes not initialized in __init__
            if init_attributes is not None:
                for name, node in dict_minus(attributes, init_attributes).items():
                    file.warning(node, self.attributeInitialized, name)

            # collect inherited gunk: methods and attributes
            # check for non-conformant methods
            inherited_methods = scope.defs.copy()
            inherited_attributes = attributes.copy()
            for base in get_base_classes(scope, checker):
                for m in _get_methods(base):
                    inherited_attributes.update(visit_with_self(GetDefs, m))
                    mname = mangle(m.name, base.name)
                    if m.name != "__init__" and methods.has_key(mname) and not conformsTo(methods[mname], m):
                        file.warning(methods[mname].node, self.signatureChanged, m.name, base.name)
                    else:
                        methods[mname] = m
                inherited_methods.update(base.defs)

            # complain about attributes with the same name as methods
            both = dict_intersect(attributes, inherited_methods)
            for name, node in both.items():
                file.warning(node, self.methodRedefined, name, scope.name)

            # find refs on self
            refs = {}
            for m in _get_methods(scope):
                refs.update(visit_with_self(GetRefs, m))

            # Now complain about refs on self that aren't known
            unknown = dict_minus(refs, inherited_methods)
            unknown = dict_minus(unknown, _ignorable)
            unknown = dict_minus(unknown, scope.defs)
            unknown = dict_minus(unknown, inherited_attributes)
            for name, node in unknown.items():
                file.warning(node, self.unknownAttribute, scope.name, name)

            unused = dict_minus(attributes, refs)
            for name, node in unused.items():
                if name.startswith("__"):
                    file.warning(node, self.unusedAttribute, name, scope.name)
Esempio n. 9
0
import imp
Esempio n. 10
0
    def check(self, file, checker):
        def visit_with_self(Visitor, method):
            if not method.node.argnames:
                return {}
            return walk(method.node, Visitor(method.node.argnames[0])).result

        # for all class scopes
        for node, scope in file.class_scopes():
            init_attributes = None  # attributes initilized in __init__
            attributes = {}  # "self.foo = " kinda things
            methods = {}  # methods -> scopes

            # get attributes defined on self
            for m in _get_methods(scope):
                defs = visit_with_self(GetDefs, m)
                if m.name == '__init__':
                    init_attributes = defs
                attributes.update(defs)
                methods[mangle(m.name, scope.name)] = m

            # complain about attributes not initialized in __init__
            if init_attributes is not None:
                for name, node in dict_minus(attributes,
                                             init_attributes).items():
                    file.warning(node, self.attributeInitialized, name)

            # collect inherited gunk: methods and attributes
            # check for non-conformant methods
            inherited_methods = scope.defs.copy()
            inherited_attributes = attributes.copy()
            for base in get_base_classes(scope, checker):
                for m in _get_methods(base):
                    inherited_attributes.update(visit_with_self(GetDefs, m))
                    mname = mangle(m.name, base.name)
                    if m.name != "__init__" and \
                       methods.has_key(mname) and \
                       not conformsTo(methods[mname], m):
                        file.warning(methods[mname].node,
                                     self.signatureChanged, m.name, base.name)
                    else:
                        methods[mname] = m
                inherited_methods.update(base.defs)

            # complain about attributes with the same name as methods
            both = dict_intersect(attributes, inherited_methods)
            for name, node in both.items():
                file.warning(node, self.methodRedefined, name, scope.name)

            # find refs on self
            refs = {}
            for m in _get_methods(scope):
                refs.update(visit_with_self(GetRefs, m))

            # Now complain about refs on self that aren't known
            unknown = dict_minus(refs, inherited_methods)
            unknown = dict_minus(unknown, _ignorable)
            unknown = dict_minus(unknown, scope.defs)
            unknown = dict_minus(unknown, inherited_attributes)
            for name, node in unknown.items():
                file.warning(node, self.unknownAttribute, scope.name, name)

            unused = dict_minus(attributes, refs)
            for name, node in unused.items():
                if name.startswith('__'):
                    file.warning(node, self.unusedAttribute, name, scope.name)
Esempio n. 11
0
import imp
Esempio n. 12
0
 def mangle(self, name):
     return name if self.klass is None else mangle(name, self.klass)