Author: Ronan Lamy <[email protected]>
Branch: 
Changeset: r73839:ecea026eb7e3
Date: 2014-10-08 16:49 +0100
http://bitbucket.org/pypy/pypy/changeset/ecea026eb7e3/

Log:    hg merge rtyper-stuff

diff --git a/pypy/doc/whatsnew-head.rst b/pypy/doc/whatsnew-head.rst
--- a/pypy/doc/whatsnew-head.rst
+++ b/pypy/doc/whatsnew-head.rst
@@ -12,3 +12,6 @@
 Allocate by 4-byte chunks in rffi_platform,
 Skip testing objdump if it does not exist,
 and other small adjustments in own tests
+
+.. branch: rtyper-stuff
+Small internal refactorings in the rtyper.
diff --git a/rpython/rlib/_stacklet_n_a.py b/rpython/rlib/_stacklet_n_a.py
--- a/rpython/rlib/_stacklet_n_a.py
+++ b/rpython/rlib/_stacklet_n_a.py
@@ -1,33 +1,35 @@
 from rpython.rlib import _rffi_stacklet as _c
-from rpython.rlib import objectmodel, debug
+from rpython.rlib import debug
+from rpython.rlib.objectmodel import we_are_translated, specialize
 from rpython.rtyper.annlowlevel import llhelper
-from rpython.tool.staticmethods import StaticMethods
 
 
-class StackletGcRootFinder:
-    __metaclass__ = StaticMethods
-
+class StackletGcRootFinder(object):
+    @staticmethod
+    @specialize.arg(1)
     def new(thrd, callback, arg):
         h = _c.new(thrd._thrd, llhelper(_c.run_fn, callback), arg)
         if not h:
             raise MemoryError
         return h
-    new._annspecialcase_ = 'specialize:arg(1)'
 
+    @staticmethod
     def switch(h):
         h = _c.switch(h)
         if not h:
             raise MemoryError
         return h
 
+    @staticmethod
     def destroy(thrd, h):
         _c.destroy(thrd._thrd, h)
-        if objectmodel.we_are_translated():
+        if we_are_translated():
             debug.debug_print("not using a framework GC: "
                               "stacklet_destroy() may leak")
 
-    is_empty_handle = _c.is_empty_handle
+    is_empty_handle = staticmethod(_c.is_empty_handle)
 
+    @staticmethod
     def get_null_handle():
         return _c.null_handle
 
diff --git a/rpython/rlib/_stacklet_shadowstack.py 
b/rpython/rlib/_stacklet_shadowstack.py
--- a/rpython/rlib/_stacklet_shadowstack.py
+++ b/rpython/rlib/_stacklet_shadowstack.py
@@ -3,7 +3,6 @@
 from rpython.rtyper.annlowlevel import llhelper
 from rpython.rtyper.lltypesystem import lltype, llmemory
 from rpython.rtyper.lltypesystem.lloperation import llop
-from rpython.tool.staticmethods import StaticMethods
 
 
 NULL_SUSPSTACK = lltype.nullptr(llmemory.GCREF.TO)
@@ -68,9 +67,7 @@
     return oldsuspstack
 
 
-class StackletGcRootFinder:
-    __metaclass__ = StaticMethods
-
+class StackletGcRootFinder(object):
     def new(thrd, callback, arg):
         gcrootfinder.callback = callback
         thread_handle = thrd._thrd
@@ -78,6 +75,7 @@
         h = _c.new(thread_handle, llhelper(_c.run_fn, _new_callback), arg)
         return get_result_suspstack(h)
     new._dont_inline_ = True
+    new = staticmethod(new)
 
     def switch(suspstack):
         # suspstack has a handle to target, i.e. where to switch to
@@ -90,10 +88,13 @@
         h = _c.switch(h)
         return get_result_suspstack(h)
     switch._dont_inline_ = True
+    switch = staticmethod(switch)
 
+    @staticmethod
     def is_empty_handle(suspstack):
         return not suspstack
 
+    @staticmethod
     def get_null_handle():
         return NULL_SUSPSTACK
 
diff --git a/rpython/rtyper/lltypesystem/lltype.py 
b/rpython/rtyper/lltypesystem/lltype.py
--- a/rpython/rtyper/lltypesystem/lltype.py
+++ b/rpython/rtyper/lltypesystem/lltype.py
@@ -191,6 +191,11 @@
     def _is_varsize(self):
         return False
 
+    def _contains_value(self, value):
+        if self is Void:
+            return True
+        return isCompatibleType(typeOf(value), self)
+
 NFOUND = object()
 
 class ContainerType(LowLevelType):
diff --git a/rpython/rtyper/lltypesystem/rbytearray.py 
b/rpython/rtyper/lltypesystem/rbytearray.py
--- a/rpython/rtyper/lltypesystem/rbytearray.py
+++ b/rpython/rtyper/lltypesystem/rbytearray.py
@@ -38,12 +38,14 @@
             i += s.length()
         cls.ll_strsetitem_nonneg(s, i, item)
 
+    @staticmethod
     def ll_strsetitem_nonneg(s, i, item):
         chars = s.chars
         ll_assert(i >= 0, "negative str getitem index")
         ll_assert(i < len(chars), "str getitem index out of bound")
         chars[i] = chr(item)
 
+    @staticmethod
     def ll_stritem_nonneg(s, i):
         return ord(rstr.LLHelpers.ll_stritem_nonneg(s, i))
 
diff --git a/rpython/rtyper/lltypesystem/rstr.py 
b/rpython/rtyper/lltypesystem/rstr.py
--- a/rpython/rtyper/lltypesystem/rstr.py
+++ b/rpython/rtyper/lltypesystem/rstr.py
@@ -270,6 +270,7 @@
 class LLHelpers(AbstractLLHelpers):
     from rpython.rtyper.annlowlevel import llstr, llunicode
 
+    @staticmethod
     @jit.elidable
     def ll_str_mul(s, times):
         if times < 0:
@@ -292,6 +293,7 @@
             i += j
         return newstr
 
+    @staticmethod
     @jit.elidable
     def ll_char_mul(ch, times):
         if typeOf(ch) is Char:
@@ -308,9 +310,11 @@
             j += 1
         return newstr
 
+    @staticmethod
     def ll_strlen(s):
         return len(s.chars)
 
+    @staticmethod
     @signature(types.any(), types.int(), returns=types.any())
     def ll_stritem_nonneg(s, i):
         chars = s.chars
@@ -318,6 +322,7 @@
         ll_assert(i < len(chars), "str getitem index out of bound")
         return chars[i]
 
+    @staticmethod
     def ll_chr2str(ch):
         if typeOf(ch) is Char:
             malloc = mallocstr
@@ -328,6 +333,7 @@
         return s
 
     # @jit.look_inside_iff(lambda str: jit.isconstant(len(str.chars)) and 
len(str.chars) == 1)
+    @staticmethod
     @jit.oopspec("str.str2unicode(str)")
     def ll_str2unicode(str):
         lgt = len(str.chars)
@@ -338,6 +344,7 @@
             s.chars[i] = cast_primitive(UniChar, str.chars[i])
         return s
 
+    @staticmethod
     def ll_str2bytearray(str):
         from rpython.rtyper.lltypesystem.rbytearray import BYTEARRAY
 
@@ -347,6 +354,7 @@
             b.chars[i] = str.chars[i]
         return b
 
+    @staticmethod
     @jit.elidable
     def ll_strhash(s):
         # unlike CPython, there is no reason to avoid to return -1
@@ -362,13 +370,17 @@
             s.hash = x
         return x
 
+    @staticmethod
     def ll_length(s):
         return len(s.chars)
 
+    @staticmethod
     def ll_strfasthash(s):
         return s.hash     # assumes that the hash is already computed
 
+    @staticmethod
     @jit.elidable
+    @jit.oopspec('stroruni.concat(s1, s2)')
     def ll_strconcat(s1, s2):
         len1 = s1.length()
         len2 = s2.length()
@@ -386,8 +398,8 @@
         else:
             newstr.copy_contents(s2, newstr, 0, len1, len2)
         return newstr
-    ll_strconcat.oopspec = 'stroruni.concat(s1, s2)'
 
+    @staticmethod
     @jit.elidable
     def ll_strip(s, ch, left, right):
         s_len = len(s.chars)
@@ -408,6 +420,7 @@
         s.copy_contents(s, result, lpos, 0, r_len)
         return result
 
+    @staticmethod
     @jit.elidable
     def ll_strip_default(s, left, right):
         s_len = len(s.chars)
@@ -428,6 +441,7 @@
         s.copy_contents(s, result, lpos, 0, r_len)
         return result
 
+    @staticmethod
     @jit.elidable
     def ll_strip_multiple(s, s2, left, right):
         s_len = len(s.chars)
@@ -448,6 +462,7 @@
         s.copy_contents(s, result, lpos, 0, r_len)
         return result
 
+    @staticmethod
     @jit.elidable
     def ll_upper(s):
         s_chars = s.chars
@@ -462,6 +477,7 @@
             i += 1
         return result
 
+    @staticmethod
     @jit.elidable
     def ll_lower(s):
         s_chars = s.chars
@@ -476,6 +492,7 @@
             i += 1
         return result
 
+    @staticmethod
     def ll_join(s, length, items):
         s_chars = s.chars
         s_len = len(s_chars)
@@ -509,7 +526,9 @@
             i += 1
         return result
 
+    @staticmethod
     @jit.elidable
+    @jit.oopspec('stroruni.cmp(s1, s2)')
     def ll_strcmp(s1, s2):
         if not s1 and not s2:
             return True
@@ -531,9 +550,10 @@
                 return diff
             i += 1
         return len1 - len2
-    ll_strcmp.oopspec = 'stroruni.cmp(s1, s2)'
 
+    @staticmethod
     @jit.elidable
+    @jit.oopspec('stroruni.equal(s1, s2)')
     def ll_streq(s1, s2):
         if s1 == s2:       # also if both are NULLs
             return True
@@ -551,8 +571,8 @@
                 return False
             j += 1
         return True
-    ll_streq.oopspec = 'stroruni.equal(s1, s2)'
 
+    @staticmethod
     @jit.elidable
     def ll_startswith(s1, s2):
         len1 = len(s1.chars)
@@ -569,11 +589,13 @@
 
         return True
 
+    @staticmethod
     def ll_startswith_char(s, ch):
         if not len(s.chars):
             return False
         return s.chars[0] == ch
 
+    @staticmethod
     @jit.elidable
     def ll_endswith(s1, s2):
         len1 = len(s1.chars)
@@ -591,11 +613,13 @@
 
         return True
 
+    @staticmethod
     def ll_endswith_char(s, ch):
         if not len(s.chars):
             return False
         return s.chars[len(s.chars) - 1] == ch
 
+    @staticmethod
     @jit.elidable
     @signature(types.any(), types.any(), types.int(), types.int(), 
returns=types.int())
     def ll_find_char(s, ch, start, end):
@@ -608,6 +632,7 @@
             i += 1
         return -1
 
+    @staticmethod
     @jit.elidable
     def ll_rfind_char(s, ch, start, end):
         if end > len(s.chars):
@@ -619,6 +644,7 @@
                 return i
         return -1
 
+    @staticmethod
     @jit.elidable
     def ll_count_char(s, ch, start, end):
         count = 0
@@ -631,6 +657,7 @@
             i += 1
         return count
 
+    @staticmethod
     @signature(types.any(), types.any(), types.int(), types.int(), 
returns=types.int())
     def ll_find(s1, s2, start, end):
         if start < 0:
@@ -646,6 +673,7 @@
 
         return LLHelpers.ll_search(s1, s2, start, end, FAST_FIND)
 
+    @staticmethod
     @signature(types.any(), types.any(), types.int(), types.int(), 
returns=types.int())
     def ll_rfind(s1, s2, start, end):
         if start < 0:
@@ -681,6 +709,7 @@
             res = 0
         return res
 
+    @staticmethod
     @jit.elidable
     def ll_search(s1, s2, start, end, mode):
         count = 0
@@ -768,6 +797,7 @@
             return -1
         return count
 
+    @staticmethod
     @signature(types.int(), types.any(), returns=types.any())
     @jit.look_inside_iff(lambda length, items: jit.loop_unrolling_heuristic(
         items, length))
@@ -802,6 +832,7 @@
             i += 1
         return result
 
+    @staticmethod
     @jit.look_inside_iff(lambda length, chars, RES: jit.isconstant(length) and 
jit.isvirtual(chars))
     def ll_join_chars(length, chars, RES):
         # no need to optimize this, will be replaced by string builder
@@ -821,6 +852,7 @@
             i += 1
         return result
 
+    @staticmethod
     @jit.oopspec('stroruni.slice(s1, start, stop)')
     @signature(types.any(), types.int(), types.int(), returns=types.any())
     @jit.elidable
@@ -836,9 +868,11 @@
         s1.copy_contents(s1, newstr, start, 0, lgt)
         return newstr
 
+    @staticmethod
     def ll_stringslice_startonly(s1, start):
         return LLHelpers._ll_stringslice(s1, start, len(s1.chars))
 
+    @staticmethod
     @signature(types.any(), types.int(), types.int(), returns=types.any())
     def ll_stringslice_startstop(s1, start, stop):
         if jit.we_are_jitted():
@@ -851,10 +885,12 @@
                 stop = len(s1.chars)
         return LLHelpers._ll_stringslice(s1, start, stop)
 
+    @staticmethod
     def ll_stringslice_minusone(s1):
         newlen = len(s1.chars) - 1
         return LLHelpers._ll_stringslice(s1, 0, newlen)
 
+    @staticmethod
     def ll_split_chr(LIST, s, c, max):
         chars = s.chars
         strlen = len(chars)
@@ -889,6 +925,7 @@
         item.copy_contents(s, item, i, 0, j - i)
         return res
 
+    @staticmethod
     def ll_split(LIST, s, c, max):
         count = 1
         if max == -1:
@@ -920,6 +957,7 @@
         item.copy_contents(s, item, prev_pos, 0, last - prev_pos)
         return res
 
+    @staticmethod
     def ll_rsplit_chr(LIST, s, c, max):
         chars = s.chars
         strlen = len(chars)
@@ -955,6 +993,7 @@
         item.copy_contents(s, item, j, 0, i - j)
         return res
 
+    @staticmethod
     def ll_rsplit(LIST, s, c, max):
         count = 1
         if max == -1:
@@ -986,6 +1025,7 @@
         item.copy_contents(s, item, 0, 0, prev_pos)
         return res
 
+    @staticmethod
     @jit.elidable
     def ll_replace_chr_chr(s, c1, c2):
         length = len(s.chars)
@@ -1001,6 +1041,7 @@
             j += 1
         return newstr
 
+    @staticmethod
     @jit.elidable
     def ll_contains(s, c):
         chars = s.chars
@@ -1012,6 +1053,7 @@
             i += 1
         return False
 
+    @staticmethod
     @jit.elidable
     def ll_int(s, base):
         if not 2 <= base <= 36:
@@ -1068,23 +1110,29 @@
     #   ll_build_push(x, next_string, n-1)
     #   s = ll_build_finish(x)
 
+    @staticmethod
     def ll_build_start(parts_count):
         return malloc(TEMP, parts_count)
 
+    @staticmethod
     def ll_build_push(builder, next_string, index):
         builder[index] = next_string
 
+    @staticmethod
     def ll_build_finish(builder):
         return LLHelpers.ll_join_strs(len(builder), builder)
 
+    @staticmethod
     @specialize.memo()
     def ll_constant(s):
         return string_repr.convert_const(s)
 
+    @staticmethod
     @specialize.memo()
     def ll_constant_unicode(s):
         return unicode_repr.convert_const(s)
 
+    @classmethod
     def do_stringformat(cls, hop, sourcevarsrepr):
         s_str = hop.args_s[0]
         assert s_str.is_constant()
@@ -1150,8 +1198,8 @@
 
         hop.exception_cannot_occur()   # to ignore the ZeroDivisionError of '%'
         return hop.gendirectcall(cls.ll_join_strs, size, vtemp)
-    do_stringformat = classmethod(do_stringformat)
 
+    @staticmethod
     @jit.dont_look_inside
     def ll_string2list(RESLIST, src):
         length = len(src.chars)
diff --git a/rpython/rtyper/rmodel.py b/rpython/rtyper/rmodel.py
--- a/rpython/rtyper/rmodel.py
+++ b/rpython/rtyper/rmodel.py
@@ -2,8 +2,7 @@
 from rpython.flowspace.model import Constant
 from rpython.rtyper.error import TyperError, MissingRTypeOperation
 from rpython.rtyper.lltypesystem import lltype
-from rpython.rtyper.lltypesystem.lltype import (Void, Bool, typeOf,
-    LowLevelType, isCompatibleType)
+from rpython.rtyper.lltypesystem.lltype import Void, Bool, LowLevelType
 from rpython.tool.pairtype import pairtype, extendabletype, pair
 
 
@@ -120,14 +119,9 @@
 
     def convert_const(self, value):
         "Convert the given constant value to the low-level repr of 'self'."
-        if self.lowleveltype is not Void:
-            try:
-                realtype = typeOf(value)
-            except (AssertionError, AttributeError, TypeError):
-                realtype = '???'
-            if realtype != self.lowleveltype:
-                raise TyperError("convert_const(self = %r, value = %r)" % (
-                    self, value))
+        if not self.lowleveltype._contains_value(value):
+            raise TyperError("convert_const(self = %r, value = %r)" % (
+                self, value))
         return value
 
     def get_ll_eq_function(self):
@@ -356,18 +350,9 @@
         lltype = reqtype
     else:
         raise TypeError(repr(reqtype))
-    # Void Constants can hold any value;
-    # non-Void Constants must hold a correctly ll-typed value
-    if lltype is not Void:
-        try:
-            realtype = typeOf(value)
-        except (AssertionError, AttributeError):
-            realtype = '???'
-        if not isCompatibleType(realtype, lltype):
-            raise TyperError("inputconst(reqtype = %s, value = %s):\n"
-                             "expected a %r,\n"
-                             "     got a %r" % (reqtype, value,
-                                                lltype, realtype))
+    if not lltype._contains_value(value):
+        raise TyperError("inputconst(): expected a %r, got %r" %
+                         (lltype, value))
     c = Constant(value)
     c.concretetype = lltype
     return c
@@ -422,7 +407,8 @@
     def __ne__(self, other):
         return not (self == other)
 
-    def build_ll_dummy_value(self):
+    @property
+    def ll_dummy_value(self):
         TYPE = self.TYPE
         try:
             return self.rtyper.cache_dummy_values[TYPE]
@@ -435,8 +421,6 @@
             self.rtyper.cache_dummy_values[TYPE] = p
             return p
 
-    ll_dummy_value = property(build_ll_dummy_value)
-
 
 # logging/warning
 
diff --git a/rpython/rtyper/rstr.py b/rpython/rtyper/rstr.py
--- a/rpython/rtyper/rstr.py
+++ b/rpython/rtyper/rstr.py
@@ -9,7 +9,6 @@
 from rpython.rtyper.rfloat import FloatRepr
 from rpython.tool.pairtype import pairtype, pair
 from rpython.tool.sourcetools import func_with_new_name
-from rpython.tool.staticmethods import StaticMethods
 from rpython.rlib.rstring import UnicodeBuilder
 
 
@@ -800,10 +799,8 @@
 #  get flowed and annotated, mostly with SomePtr.
 #
 
-# this class contains low level helpers used both by lltypesystem
-class AbstractLLHelpers:
-    __metaclass__ = StaticMethods
-
+class AbstractLLHelpers(object):
+    @staticmethod
     def ll_isdigit(s):
         from rpython.rtyper.annlowlevel import hlstr
 
@@ -815,6 +812,7 @@
                 return False
         return True
 
+    @staticmethod
     def ll_isalpha(s):
         from rpython.rtyper.annlowlevel import hlstr
 
@@ -826,6 +824,7 @@
                 return False
         return True
 
+    @staticmethod
     def ll_isalnum(s):
         from rpython.rtyper.annlowlevel import hlstr
 
@@ -837,14 +836,17 @@
                 return False
         return True
 
+    @staticmethod
     def ll_char_isspace(ch):
         c = ord(ch)
         return c == 32 or (9 <= c <= 13)   # c in (9, 10, 11, 12, 13, 32)
 
+    @staticmethod
     def ll_char_isdigit(ch):
         c = ord(ch)
         return c <= 57 and c >= 48
 
+    @staticmethod
     def ll_char_isalpha(ch):
         c = ord(ch)
         if c >= 97:
@@ -852,6 +854,7 @@
         else:
             return 65 <= c <= 90
 
+    @staticmethod
     def ll_char_isalnum(ch):
         c = ord(ch)
         if c >= 65:
@@ -862,47 +865,54 @@
         else:
             return 48 <= c <= 57
 
+    @staticmethod
     def ll_char_isupper(ch):
         c = ord(ch)
         return 65 <= c <= 90
 
+    @staticmethod
     def ll_char_islower(ch):
         c = ord(ch)
         return 97 <= c <= 122
 
+    @staticmethod
     def ll_upper_char(ch):
         if 'a' <= ch <= 'z':
             ch = chr(ord(ch) - 32)
         return ch
 
+    @staticmethod
     def ll_lower_char(ch):
         if 'A' <= ch <= 'Z':
             ch = chr(ord(ch) + 32)
         return ch
 
+    @staticmethod
     def ll_char_hash(ch):
         return ord(ch)
 
+    @staticmethod
     def ll_unichar_hash(ch):
         return ord(ch)
 
+    @classmethod
     def ll_str_is_true(cls, s):
         # check if a string is True, allowing for None
         return bool(s) and cls.ll_strlen(s) != 0
-    ll_str_is_true = classmethod(ll_str_is_true)
 
+    @classmethod
     def ll_stritem_nonneg_checked(cls, s, i):
         if i >= cls.ll_strlen(s):
             raise IndexError
         return cls.ll_stritem_nonneg(s, i)
-    ll_stritem_nonneg_checked = classmethod(ll_stritem_nonneg_checked)
 
+    @classmethod
     def ll_stritem(cls, s, i):
         if i < 0:
             i += cls.ll_strlen(s)
         return cls.ll_stritem_nonneg(s, i)
-    ll_stritem = classmethod(ll_stritem)
 
+    @classmethod
     def ll_stritem_checked(cls, s, i):
         length = cls.ll_strlen(s)
         if i < 0:
@@ -910,8 +920,8 @@
         if i >= length or i < 0:
             raise IndexError
         return cls.ll_stritem_nonneg(s, i)
-    ll_stritem_checked = classmethod(ll_stritem_checked)
 
+    @staticmethod
     def parse_fmt_string(fmt):
         # we support x, d, s, f, [r]
         it = iter(fmt)
@@ -937,6 +947,7 @@
             r.append(curstr)
         return r
 
+    @staticmethod
     def ll_float(ll_str):
         from rpython.rtyper.annlowlevel import hlstr
         from rpython.rlib.rfloat import rstring_to_float
@@ -961,6 +972,7 @@
         assert end >= 0
         return rstring_to_float(s[beg:end + 1])
 
+    @classmethod
     def ll_splitlines(cls, LIST, ll_str, keep_newlines):
         from rpython.rtyper.annlowlevel import hlstr
         s = hlstr(ll_str)
@@ -991,4 +1003,3 @@
             item = cls.ll_stringslice_startstop(ll_str, j, strlen)
             res.ll_setitem_fast(list_length, item)
         return res
-    ll_splitlines = classmethod(ll_splitlines)
diff --git a/rpython/tool/staticmethods.py b/rpython/tool/staticmethods.py
deleted file mode 100644
--- a/rpython/tool/staticmethods.py
+++ /dev/null
@@ -1,14 +0,0 @@
-import types
-class AbstractMethods(type):
-    def __new__(cls, cls_name, bases, cls_dict):
-        for key, value in cls_dict.iteritems():
-            if isinstance(value, types.FunctionType):
-                cls_dict[key] = cls.decorator(value)
-        return type.__new__(cls, cls_name, bases, cls_dict)
-
-
-class StaticMethods(AbstractMethods):
-    """
-    Metaclass that turns plain methods into staticmethods.
-    """
-    decorator = staticmethod
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to