Author: Ronan Lamy <ronan.l...@gmail.com>
Branch: 
Changeset: r74313:a9747c9fca35
Date: 2014-10-31 16:14 +0000
http://bitbucket.org/pypy/pypy/changeset/a9747c9fca35/

Log:    Clean up rpython/annotator/description.py

diff --git a/rpython/annotator/description.py b/rpython/annotator/description.py
--- a/rpython/annotator/description.py
+++ b/rpython/annotator/description.py
@@ -1,6 +1,7 @@
 from __future__ import absolute_import
-import types, py
-from rpython.annotator.signature import enforce_signature_args, 
enforce_signature_return
+import types
+from rpython.annotator.signature import (
+    enforce_signature_args, enforce_signature_return)
 from rpython.flowspace.model import Constant, FunctionGraph
 from rpython.flowspace.bytecode import cpython_code_signature
 from rpython.annotator.argument import rawshape, ArgErr
@@ -15,10 +16,10 @@
     'd1~d2 if d1 and d2 might be called at the same call site'.
     """
     normalized = False
-    modified   = True
+    modified = True
 
     def __init__(self, desc):
-        self.descs = { desc: True }
+        self.descs = {desc: True}
         self.calltables = {}  # see calltable_lookup_row()
         self.total_calltable_size = 0
 
@@ -29,7 +30,7 @@
         for shape, table in other.calltables.items():
             for row in table:
                 self.calltable_add_row(shape, row)
-    absorb = update # UnionFind API
+    absorb = update  # UnionFind API
 
     def calltable_lookup_row(self, callshape, row):
         # this code looks up a table of which graph to
@@ -68,7 +69,7 @@
         self.descs.update(other.descs)
         self.read_locations.update(other.read_locations)
         self.attrs.update(other.attrs)
-    absorb = update # UnionFind API
+    absorb = update  # UnionFind API
 
     def get_s_value(self, attrname):
         try:
@@ -97,7 +98,7 @@
 
     def __init__(self, desc):
         from rpython.annotator.model import s_ImpossibleValue
-        self.descs = { desc: True }
+        self.descs = {desc: True}
         self.read_locations = {}     # set of position_keys
         self.s_value = s_ImpossibleValue    # union of possible values
 
@@ -106,7 +107,7 @@
         self.descs.update(other.descs)
         self.read_locations.update(other.read_locations)
         self.s_value = unionof(self.s_value, other.s_value)
-    absorb = update # UnionFind API
+    absorb = update  # UnionFind API
 
     def get_s_value(self, attrname):
         return self.s_value
@@ -164,9 +165,9 @@
     def bind_under(self, classdef, name):
         return self
 
+    @staticmethod
     def simplify_desc_set(descs):
         pass
-    simplify_desc_set = staticmethod(simplify_desc_set)
 
 
 class NoStandardGraph(Exception):
@@ -218,7 +219,7 @@
         [graph] = self._cache.values()
         relax_sig_check = getattr(self.pyobj, "relax_sig_check", False)
         if (graph.signature != self.signature or
-            graph.defaults  != self.defaults) and not relax_sig_check:
+                graph.defaults != self.defaults) and not relax_sig_check:
             raise NoStandardGraph(self)
         return graph
 
@@ -229,7 +230,7 @@
             def nameof(thing):
                 if isinstance(thing, str):
                     return thing
-                elif hasattr(thing, '__name__'): # mostly types and functions
+                elif hasattr(thing, '__name__'):  # mostly types and functions
                     return thing.__name__
                 elif hasattr(thing, 'name') and isinstance(thing.name, str):
                     return thing.name            # mostly ClassDescs
@@ -240,7 +241,7 @@
 
             if key is not None and alt_name is None:
                 postfix = valid_identifier(nameof(key))
-                alt_name = "%s__%s"%(self.name, postfix)
+                alt_name = "%s__%s" % (self.name, postfix)
             graph = self.buildgraph(alt_name, builder)
             self._cache[key] = graph
             return graph
@@ -268,7 +269,7 @@
 
     def specialize(self, inputcells, op=None):
         if (op is None and
-            getattr(self.bookkeeper, "position_key", None) is not None):
+                getattr(self.bookkeeper, "position_key", None) is not None):
             _, block, i = self.bookkeeper.position_key
             op = block.operations[i]
         if self.specializer is None:
@@ -280,15 +281,16 @@
         enforceargs = getattr(self.pyobj, '_annenforceargs_', None)
         signature = getattr(self.pyobj, '_signature_', None)
         if enforceargs and signature:
-            raise Exception("%r: signature and enforceargs cannot both be 
used" % (self,))
+            raise Exception("%r: signature and enforceargs cannot both be "
+                            "used" % (self,))
         if enforceargs:
             if not callable(enforceargs):
                 from rpython.annotator.signature import Sig
                 enforceargs = Sig(*enforceargs)
                 self.pyobj._annenforceargs_ = enforceargs
-            enforceargs(self, inputcells) # can modify inputcells in-place
+            enforceargs(self, inputcells)  # can modify inputcells in-place
         if signature:
-            enforce_signature_args(self, signature[0], inputcells) # mutates 
inputcells
+            enforce_signature_args(self, signature[0], inputcells)  # mutates 
inputcells
         if getattr(self.pyobj, '_annspecialcase_', 
'').endswith("call_location"):
             return self.specializer(self, inputcells, op)
         else:
@@ -309,7 +311,8 @@
             if signature:
                 sigresult = enforce_signature_return(self, signature[1], 
result)
                 if sigresult is not None:
-                    self.bookkeeper.annotator.addpendingblock(graph, 
graph.returnblock, [sigresult])
+                    self.bookkeeper.annotator.addpendingblock(
+                        graph, graph.returnblock, [sigresult])
                     result = sigresult
         # Some specializations may break the invariant of returning
         # annotations that are always more general than the previous time.
@@ -325,12 +328,13 @@
                                              None,       # selfclassdef
                                              name)
 
+    @staticmethod
     def consider_call_site(bookkeeper, family, descs, args, s_result, op):
         shape = rawshape(args)
         row = FunctionDesc.row_to_consider(descs, args, op)
         family.calltable_add_row(shape, row)
-    consider_call_site = staticmethod(consider_call_site)
 
+    @staticmethod
     def variant_for_call_site(bookkeeper, family, descs, args, op):
         shape = rawshape(args)
         bookkeeper.enter(None)
@@ -340,11 +344,11 @@
             bookkeeper.leave()
         index = family.calltable_lookup_row(shape, row)
         return shape, index
-    variant_for_call_site = staticmethod(variant_for_call_site)
 
     def rowkey(self):
         return self
 
+    @staticmethod
     def row_to_consider(descs, args, op):
         # see comments in CallFamily
         from rpython.annotator.model import s_ImpossibleValue
@@ -356,7 +360,6 @@
             desc.pycall(enlist, args, s_ImpossibleValue, op)
             assert row
         return row
-    row_to_consider = staticmethod(row_to_consider)
 
     def get_s_signatures(self, shape):
         family = self.getcallfamily()
@@ -417,14 +420,14 @@
 
         if cls.__dict__.get('_mixin_', False):
             raise AnnotatorError("cannot use directly the class %r because "
-                                    "it is a _mixin_" % (cls,))
+                                 "it is a _mixin_" % (cls,))
 
-        # special case: skip BaseException in Python 2.5, and pretend
+        # special case: skip BaseException, and pretend
         # that all exceptions ultimately inherit from Exception instead
         # of BaseException (XXX hack)
         if cls is Exception:
             baselist = []
-        elif baselist == [py.builtin.BaseException]:
+        elif baselist == [BaseException]:
             baselist = [Exception]
 
         mixins_before = []
@@ -470,7 +473,7 @@
             self.all_enforced_attrs = attrs
 
         if (self.is_builtin_exception_class() and
-            self.all_enforced_attrs is None):
+                self.all_enforced_attrs is None):
             from rpython.annotator import classdef
             if cls not in classdef.FORCE_ATTRIBUTES_INTO_CLASSES:
                 self.all_enforced_attrs = []    # no attribute allowed
@@ -479,7 +482,7 @@
         if isinstance(value, types.FunctionType):
             # for debugging
             if not hasattr(value, 'class_'):
-                value.class_ = self.pyobj # remember that this is really a 
method
+                value.class_ = self.pyobj
             if self.specialize:
                 # make a custom funcdesc that specializes on its first
                 # argument (i.e. 'self').
@@ -504,7 +507,7 @@
         if isinstance(value, staticmethod) and mixin:
             # make a new copy of staticmethod
             func = value.__get__(42)
-            value =  staticmethod(func_with_new_name(func, func.__name__))
+            value = staticmethod(func_with_new_name(func, func.__name__))
 
         if type(value) in MemberDescriptorTypes:
             # skip __slots__, showing up in the class as 'member' objects
@@ -536,8 +539,8 @@
         add(check_not_in)
         #
         for base in reversed(mro):
-            assert base.__dict__.get("_mixin_", False), ("Mixin class %r has 
non"
-                "mixin base class %r" % (mixins, base))
+            assert base.__dict__.get("_mixin_", False), (
+                "Mixin class %r has non mixin base class %r" % (mixins, base))
             for name, value in base.__dict__.items():
                 if name in skip:
                     continue
@@ -554,18 +557,18 @@
         try:
             return self._classdefs[key]
         except KeyError:
-            from rpython.annotator.classdef import ClassDef, 
FORCE_ATTRIBUTES_INTO_CLASSES
+            from rpython.annotator.classdef import (
+                ClassDef, FORCE_ATTRIBUTES_INTO_CLASSES)
             classdef = ClassDef(self.bookkeeper, self)
             self.bookkeeper.classdefs.append(classdef)
             self._classdefs[key] = classdef
 
             # forced attributes
-            if self.pyobj is not None:
-                cls = self.pyobj
-                if cls in FORCE_ATTRIBUTES_INTO_CLASSES:
-                    for name, s_value in 
FORCE_ATTRIBUTES_INTO_CLASSES[cls].items():
-                        classdef.generalize_attr(name, s_value)
-                        classdef.find_attribute(name).modified(classdef)
+            cls = self.pyobj
+            if cls in FORCE_ATTRIBUTES_INTO_CLASSES:
+                for name, s_value in 
FORCE_ATTRIBUTES_INTO_CLASSES[cls].items():
+                    classdef.generalize_attr(name, s_value)
+                    classdef.find_attribute(name).modified(classdef)
 
             # register all class attributes as coming from this ClassDesc
             # (as opposed to prebuilt instances)
@@ -632,8 +635,7 @@
         return s_instance
 
     def is_exception_class(self):
-        return self.pyobj is not None and issubclass(self.pyobj,
-                                                     py.builtin.BaseException)
+        return issubclass(self.pyobj, BaseException)
 
     def is_builtin_exception_class(self):
         if self.is_exception_class():
@@ -700,14 +702,12 @@
     def find_source_for(self, name):
         if name in self.classdict:
             return self
-        if self.pyobj is not None:
-            # check whether in the case the classdesc corresponds to a real 
class
-            # there is a new attribute
-            cls = self.pyobj
-            if name in cls.__dict__:
-                self.add_source_attribute(name, cls.__dict__[name])
-                if name in self.classdict:
-                    return self
+        # check whether there is a new attribute
+        cls = self.pyobj
+        if name in cls.__dict__:
+            self.add_source_attribute(name, cls.__dict__[name])
+            if name in self.classdict:
+                return self
         return None
 
     def maybe_return_immutable_list(self, attr, s_result):
@@ -715,7 +715,7 @@
         # either 'lst[*]' or 'lst?[*]'
         # should really return an immutable list as a result.  Implemented
         # by changing the result's annotation (but not, of course, doing an
-        # actual copy in the rtyper).  Tested in 
rpython.rtyper.test.test_rlist,
+        # actual copy in the rtyper). Tested in rpython.rtyper.test.test_rlist,
         # test_immutable_list_out_of_instance.
         if self._detect_invalid_attrs and attr in self._detect_invalid_attrs:
             raise Exception("field %r was migrated to %r from a subclass in "
@@ -727,7 +727,7 @@
         while cdesc is not None:
             if '_immutable_fields_' in cdesc.classdict:
                 if (search1 in cdesc.classdict['_immutable_fields_'].value or
-                    search2 in cdesc.classdict['_immutable_fields_'].value):
+                        search2 in 
cdesc.classdict['_immutable_fields_'].value):
                     s_result.listdef.never_resize()
                     s_copy = s_result.listdef.offspring()
                     s_copy.listdef.mark_as_immutable()
@@ -743,6 +743,7 @@
             cdesc = cdesc.basedesc
         return s_result     # common case
 
+    @staticmethod
     def consider_call_site(bookkeeper, family, descs, args, s_result, op):
         from rpython.annotator.model import SomeInstance, SomePBC, s_None
         if len(descs) == 1:
@@ -789,7 +790,6 @@
             initfamily = initdescs[0].getcallfamily()
             MethodDesc.consider_call_site(bookkeeper, initfamily, initdescs,
                                           args, s_None, op)
-    consider_call_site = staticmethod(consider_call_site)
 
     def getallbases(self):
         desc = self
@@ -797,6 +797,7 @@
             yield desc
             desc = desc.basedesc
 
+    @staticmethod
     def getcommonbase(descs):
         commondesc = descs[0]
         for desc in descs[1:]:
@@ -806,7 +807,6 @@
                 desc = desc.basedesc
             commondesc = desc
         return commondesc
-    getcommonbase = staticmethod(getcommonbase)
 
     def rowkey(self):
         return self
@@ -865,7 +865,7 @@
         from rpython.annotator.model import SomeInstance
         if self.selfclassdef is None:
             raise Exception("calling %r" % (self,))
-        s_instance = SomeInstance(self.selfclassdef, flags = self.flags)
+        s_instance = SomeInstance(self.selfclassdef, flags=self.flags)
         args = args.prepend(s_instance)
         return self.funcdesc.pycall(schedule, args, s_previous_result, op)
 
@@ -893,6 +893,7 @@
         # FunctionDesc to use as a key in that family.
         return self.funcdesc
 
+    @staticmethod
     def simplify_desc_set(descs):
         # Some hacking needed to make contains() happy on SomePBC: if the
         # set of MethodDescs contains some "redundant" ones, i.e. ones that
@@ -939,7 +940,6 @@
                         if cdef1 is not cdef2 and cdef1.issubclass(cdef2):
                             descs.remove(desc1)
                             break
-    simplify_desc_set = staticmethod(simplify_desc_set)
 
 
 def new_or_old_class(c):
@@ -957,7 +957,7 @@
         self._read_attribute = read_attribute
         self.attrcache = {}
         self.knowntype = new_or_old_class(pyobj)
-        assert bool(pyobj), "__nonzero__ unsupported on frozen PBC %r" 
%(pyobj,)
+        assert bool(pyobj), "__nonzero__ unsupported on frozen PBC %r" % 
(pyobj,)
 
     def has_attribute(self, attr):
         if attr in self.attrcache:
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to