[pypy-commit] pypy unroll-if-alt: merged default in

alex_gaynor noreply at buildbot.pypy.org
Wed Sep 21 00:16:35 CEST 2011


Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch: unroll-if-alt
Changeset: r47377:e7518e0ec519
Date: 2011-09-20 17:51 -0400
http://bitbucket.org/pypy/pypy/changeset/e7518e0ec519/

Log:	merged default in

diff --git a/pypy/interpreter/executioncontext.py b/pypy/interpreter/executioncontext.py
--- a/pypy/interpreter/executioncontext.py
+++ b/pypy/interpreter/executioncontext.py
@@ -1,5 +1,4 @@
 import sys
-from pypy.interpreter.miscutils import Stack
 from pypy.interpreter.error import OperationError
 from pypy.rlib.rarithmetic import LONG_BIT
 from pypy.rlib.unroll import unrolling_iterable
diff --git a/pypy/interpreter/miscutils.py b/pypy/interpreter/miscutils.py
--- a/pypy/interpreter/miscutils.py
+++ b/pypy/interpreter/miscutils.py
@@ -2,154 +2,6 @@
 Miscellaneous utilities.
 """
 
-import types
-
-from pypy.rlib.rarithmetic import r_uint
-
-class RootStack:
-    pass
-
-class Stack(RootStack):
-    """Utility class implementing a stack."""
-
-    _annspecialcase_ = "specialize:ctr_location" # polymorphic
-
-    def __init__(self):
-        self.items = []
-
-    def clone(self):
-        s = self.__class__()
-        for item in self.items:
-            try:
-                item = item.clone()
-            except AttributeError:
-                pass
-            s.push(item)
-        return s
-
-    def push(self, item):
-        self.items.append(item)
-
-    def pop(self):
-        return self.items.pop()
-
-    def drop(self, n):
-        if n > 0:
-            del self.items[-n:]
-
-    def top(self, position=0):
-        """'position' is 0 for the top of the stack, 1 for the item below,
-        and so on.  It must not be negative."""
-        if position < 0:
-            raise ValueError, 'negative stack position'
-        if position >= len(self.items):
-            raise IndexError, 'not enough entries in stack'
-        return self.items[~position]
-
-    def set_top(self, value, position=0):
-        """'position' is 0 for the top of the stack, 1 for the item below,
-        and so on.  It must not be negative."""
-        if position < 0:
-            raise ValueError, 'negative stack position'
-        if position >= len(self.items):
-            raise IndexError, 'not enough entries in stack'
-        self.items[~position] = value
-
-    def depth(self):
-        return len(self.items)
-
-    def empty(self):
-        return len(self.items) == 0
-
-
-class FixedStack(RootStack):
-    _annspecialcase_ = "specialize:ctr_location" # polymorphic
-
-    # unfortunately, we have to re-do everything
-    def __init__(self):
-        pass
-
-    def setup(self, stacksize):
-        self.ptr = r_uint(0) # we point after the last element
-        self.items = [None] * stacksize
-
-    def clone(self):
-        # this is only needed if we support flow space
-        s = self.__class__()
-        s.setup(len(self.items))
-        for item in self.items[:self.ptr]:
-            try:
-                item = item.clone()
-            except AttributeError:
-                pass
-            s.push(item)
-        return s
-
-    def push(self, item):
-        ptr = self.ptr
-        self.items[ptr] = item
-        self.ptr = ptr + 1
-
-    def pop(self):
-        ptr = self.ptr - 1
-        ret = self.items[ptr]   # you get OverflowError if the stack is empty
-        self.items[ptr] = None
-        self.ptr = ptr
-        return ret
-
-    def drop(self, n):
-        while n > 0:
-            n -= 1
-            self.ptr -= 1
-            self.items[self.ptr] = None
-
-    def top(self, position=0):
-        # for a fixed stack, we assume correct indices
-        return self.items[self.ptr + ~position]
-
-    def set_top(self, value, position=0):
-        # for a fixed stack, we assume correct indices
-        self.items[self.ptr + ~position] = value
-
-    def depth(self):
-        return self.ptr
-
-    def empty(self):
-        return not self.ptr
-
-
-class InitializedClass(type):
-    """NOT_RPYTHON.  A meta-class that allows a class to initialize itself (or
-    its subclasses) by calling __initclass__() as a class method."""
-    def __init__(self, name, bases, dict):
-        super(InitializedClass, self).__init__(name, bases, dict)
-        for basecls in self.__mro__:
-            raw = basecls.__dict__.get('__initclass__')
-            if isinstance(raw, types.FunctionType):
-                raw(self)   # call it as a class method
-
-
-class RwDictProxy(object):
-    """NOT_RPYTHON.  A dict-like class standing for 'cls.__dict__', to work
-    around the fact that the latter is a read-only proxy for new-style
-    classes."""
-    
-    def __init__(self, cls):
-        self.cls = cls
-
-    def __getitem__(self, attr):
-        return self.cls.__dict__[attr]
-
-    def __setitem__(self, attr, value):
-        setattr(self.cls, attr, value)
-
-    def __contains__(self, value):
-        return value in self.cls.__dict__
-
-    def items(self):
-        return self.cls.__dict__.items()
-
-
 class ThreadLocals:
     """Pseudo thread-local storage, for 'space.threadlocals'.
     This is not really thread-local at all; the intention is that the PyPy
diff --git a/pypy/rlib/parsing/codebuilder.py b/pypy/rlib/parsing/codebuilder.py
--- a/pypy/rlib/parsing/codebuilder.py
+++ b/pypy/rlib/parsing/codebuilder.py
@@ -1,3 +1,5 @@
+import contextlib
+
 class Codebuilder(object):
     def __init__(self):
         self.blocks = []
@@ -27,10 +29,12 @@
         assert blockstarter.endswith(":")
         self.emit(blockstarter)
         self.blocks.append(blockstarter)
-        def BlockEnder():
-            yield None
-            self.end_block(blockstarter)
-        return BlockEnder()
+
+    @contextlib.contextmanager
+    def block(self, blockstarter):
+        self.start_block(blockstarter)
+        yield None
+        self.end_block(blockstarter)
 
     def end_block(self, starterpart=""):
         block = self.blocks.pop()
diff --git a/pypy/rlib/parsing/deterministic.py b/pypy/rlib/parsing/deterministic.py
--- a/pypy/rlib/parsing/deterministic.py
+++ b/pypy/rlib/parsing/deterministic.py
@@ -1,3 +1,4 @@
+from __future__ import with_statement
 import py
 
 try:
@@ -228,11 +229,11 @@
         above = set()
         for state, nextstates in state_to_chars.iteritems():
             above.add(state)
-            for _ in result.start_block("if state == %s:" % (state, )):
-                for _ in result.start_block("if i < len(input):"):
+            with result.block("if state == %s:" % (state, )):
+                with result.block("if i < len(input):"):
                     result.emit("char = input[i]")
                     result.emit("i += 1")
-                for _ in result.start_block("else:"):
+                with result.block("else:"):
                     if state in self.final_states:
                         result.emit("return True")
                     else:
@@ -248,7 +249,7 @@
                     for i, (a, num) in enumerate(compressed):
                         if num < 5:
                             for charord in range(ord(a), ord(a) + num):
-                                for _ in result.start_block(
+                                with result.block(
                                     "%sif char == %r:" % (
                                         elif_prefix, chr(charord))):
                                     result.emit("state = %s" % (nextstate, ))
@@ -256,23 +257,23 @@
                                 if not elif_prefix:
                                     elif_prefix = "el"
                         else:
-                            for _ in result.start_block(
+                            with result.block(
                                 "%sif %r <= char <= %r:" % (
                                     elif_prefix, a, chr(ord(a) + num - 1))):
                                 result.emit("state = %s""" % (nextstate, ))
                                 result.emit(continue_prefix)
                             if not elif_prefix:
                                 elif_prefix = "el"
-                for _ in result.start_block("else:"):
+                with result.block("else:"):
                     result.emit("break") 
         #print state_to_chars.keys()
         for state in range(self.num_states):
             if state in state_to_chars:
                 continue
-            for _ in result.start_block("if state == %s:" % (state, )):
-                for _ in result.start_block("if i == len(input):"):
+            with result.block("if state == %s:" % (state, )):
+                with result.block("if i == len(input):"):
                     result.emit("return True")
-                for _ in result.start_block("else:"):
+                with result.block("else:"):
                     result.emit("break")
         result.emit("break")
         result.end_block("while")
@@ -303,14 +304,14 @@
         above = set()
         for state, nextstates in state_to_chars_sorted:
             above.add(state)
-            for _ in result.start_block("if state == %s:" % (state, )):
+            with result.block("if state == %s:" % (state, )):
                 if state in self.final_states:
                     result.emit("runner.last_matched_index = i - 1")
                     result.emit("runner.last_matched_state = state")
-                for _ in result.start_block("try:"):
+                with result.block("try:"):
                     result.emit("char = input[i]")
                     result.emit("i += 1")
-                for _ in result.start_block("except IndexError:"):
+                with result.block("except IndexError:"):
                     result.emit("runner.state = %s" % (state, ))
                     if state in self.final_states:
                         result.emit("return i")
@@ -327,21 +328,21 @@
                     for i, (a, num) in enumerate(compressed):
                         if num < 3:
                             for charord in range(ord(a), ord(a) + num):
-                                for _ in result.start_block("%sif char == %r:"
+                                with result.block("%sif char == %r:"
                                         % (elif_prefix, chr(charord))):
                                     result.emit("state = %s" % (nextstate, ))
                                     result.emit(continue_prefix)
                                 if not elif_prefix:
                                     elif_prefix = "el"
                         else:
-                            for _ in result.start_block(
+                            with result.block(
                                 "%sif %r <= char <= %r:" % (
                                     elif_prefix, a, chr(ord(a) + num - 1))):
                                     result.emit("state = %s" % (nextstate, ))
                                     result.emit(continue_prefix)
                             if not elif_prefix:
                                 elif_prefix = "el"
-                for _ in result.start_block("else:"):
+                with result.block("else:"):
                     result.emit("break")
         #print state_to_chars.keys()
         for state in range(self.num_states):
diff --git a/pypy/rlib/parsing/makepackrat.py b/pypy/rlib/parsing/makepackrat.py
--- a/pypy/rlib/parsing/makepackrat.py
+++ b/pypy/rlib/parsing/makepackrat.py
@@ -1,3 +1,4 @@
+from __future__ import with_statement
 import py
 import sys
 from pypy.rlib.parsing.tree import Nonterminal, Symbol, RPythonVisitor
@@ -321,27 +322,27 @@
         else:
             self.emit("_key = self._pos")
         self.emit("_status = self.%s.get(_key, None)" % (dictname, ))
-        for _ in self.start_block("if _status is None:"):
+        with self.block("if _status is None:"):
             self.emit("_status = self.%s[_key] = Status()" % (
                 dictname, ))
-        for _ in self.start_block("else:"):
+        with self.block("else:"):
             self.emit("_statusstatus = _status.status")
-            for _ in self.start_block("if _statusstatus == _status.NORMAL:"):
+            with self.block("if _statusstatus == _status.NORMAL:"):
                 self.emit("self._pos = _status.pos")
                 self.emit("return _status")
-            for _ in self.start_block("elif _statusstatus == _status.ERROR:"):
+            with self.block("elif _statusstatus == _status.ERROR:"):
                 self.emit("raise BacktrackException(_status.error)")
             if self.have_call:
-                for _ in self.start_block(
+                with self.block(
                     "elif (_statusstatus == _status.INPROGRESS or\n"
                     "      _statusstatus == _status.LEFTRECURSION):"):
                     self.emit("_status.status = _status.LEFTRECURSION")
-                    for _ in self.start_block("if _status.result is not None:"):
+                    with self.block("if _status.result is not None:"):
                         self.emit("self._pos = _status.pos")
                         self.emit("return _status")
-                    for _ in self.start_block("else:"):
+                    with self.block("else:"):
                         self.emit("raise BacktrackException(None)")
-                for _ in self.start_block(
+                with self.block(
                     "elif _statusstatus == _status.SOMESOLUTIONS:"):
                     self.emit("_status.status = _status.INPROGRESS")
         self.emit("_startingpos = self._pos")
@@ -352,10 +353,10 @@
     def memoize_footer(self, name, args):
         dictname = "_dict_%s" % (name, )
         if self.have_call:
-            for _ in self.start_block(
+            with self.block(
                 "if _status.status == _status.LEFTRECURSION:"):
-                for _ in self.start_block("if _status.result is not None:"):
-                    for _ in self.start_block("if _status.pos >= self._pos:"):
+                with self.block("if _status.result is not None:"):
+                    with self.block("if _status.pos >= self._pos:"):
                         self.emit("_status.status = _status.NORMAL")
                         self.emit("self._pos = _status.pos")
                         self.emit("return _status")
@@ -373,7 +374,7 @@
         self.emit("_status.error = _error")
         self.emit("return _status")
         self.end_block("try")
-        for _ in self.start_block("except BacktrackException, _exc:"):
+        with self.block("except BacktrackException, _exc:"):
             self.emit("_status.pos = -1")
             self.emit("_status.result = None")
             self.combine_error('_exc.error')
@@ -394,7 +395,7 @@
         self.start_block("class Parser(object):")
         for elt in t.children:
             self.dispatch(elt)
-        for _ in self.start_block("def __init__(self, inputstream):"):
+        with self.block("def __init__(self, inputstream):"):
             for line in self.initcode:
                 self.emit(line)
             self.emit("self._pos = 0")
@@ -405,7 +406,7 @@
 
     def emit_regex_code(self):
         for regex, matcher in self.matchers.iteritems():
-            for _ in  self.start_block(
+            with  self.block(
                     "def _regex%s(self):" % (abs(hash(regex)), )):
                 c = self.choice_point()
                 self.emit("_runner = self._Runner(self._inputstream, self._pos)")
@@ -423,8 +424,8 @@
                 self.emit("self._pos = _upto")
                 self.emit("return _result")
 
-        for _ in self.start_block("class _Runner(object):"):
-            for _ in self.start_block("def __init__(self, text, pos):"):
+        with self.block("class _Runner(object):"):
+            with self.block("def __init__(self, text, pos):"):
                 self.emit("self.text = text")
                 self.emit("self.pos = pos")
                 self.emit("self.last_matched_state = -1")
@@ -444,7 +445,7 @@
         otherargs = t.children[1].children
         argswithself = ", ".join(["self"] + otherargs)
         argswithoutself = ", ".join(otherargs)
-        for _ in self.start_block("def %s(%s):" % (name, argswithself)):
+        with self.block("def %s(%s):" % (name, argswithself)):
             self.emit("return self._%s(%s).result" % (name, argswithoutself))
         self.start_block("def _%s(%s):" % (name, argswithself, ))
         self.namecount = 0
@@ -465,10 +466,10 @@
             self.start_block("while 1:")
         for i, p in enumerate(possibilities):
             c = self.choice_point()
-            for _ in self.start_block("try:"):
+            with self.block("try:"):
                 self.dispatch(p)
                 self.emit("break")
-            for _ in self.start_block("except BacktrackException, _exc:"):
+            with self.block("except BacktrackException, _exc:"):
                 self.combine_error('_exc.error')
                 self.revert(c)
                 if i == len(possibilities) - 1:
@@ -484,9 +485,9 @@
 
     def visit_maybe(self, t):
         c = self.choice_point()
-        for _ in self.start_block("try:"):
+        with self.block("try:"):
             self.dispatch(t.children[0])
-        for _ in self.start_block("except BacktrackException:"):
+        with self.block("except BacktrackException:"):
             self.revert(c)
 
     def visit_repetition(self, t):
@@ -496,12 +497,12 @@
         if t.children[0] == '+':
             self.dispatch(t.children[1])
             self.emit("%s.append(_result)"  % (name, ))
-        for _ in self.start_block("while 1:"):
+        with self.block("while 1:"):
             c = self.choice_point()
-            for _ in self.start_block("try:"):
+            with self.block("try:"):
                 self.dispatch(t.children[1])
                 self.emit("%s.append(_result)" % (name, ))
-            for _ in self.start_block("except BacktrackException, _exc:"):
+            with self.block("except BacktrackException, _exc:"):
                 self.combine_error('_exc.error')
                 self.revert(c)
                 self.emit("break")
@@ -525,12 +526,12 @@
         self.namecount += 1
         child = t.children[0]
         self.emit("%s = _result" % (resultname, ))
-        for _ in self.start_block("try:"):
+        with self.block("try:"):
             self.dispatch(child)
-        for _ in self.start_block("except BacktrackException:"):
+        with self.block("except BacktrackException:"):
             self.revert(c)
             self.emit("_result = %s" % (resultname, ))
-        for _ in self.start_block("else:"):
+        with self.block("else:"):
             # heuristic to get nice error messages sometimes
             if isinstance(child, Symbol) and child.symbol == "QUOTE":
 
@@ -559,21 +560,21 @@
     def visit_if(self, t):
         if len(t.children) == 2:
             self.dispatch(t.children[0])
-        for _ in self.start_block("if not (%s):" % (
+        with self.block("if not (%s):" % (
             t.children[-1].additional_info[1:-1], )):
             self.emit("raise BacktrackException(")
             self.emit("    self._ErrorInformation(")
             self.emit("         _startingpos, ['condition not met']))")
-    
+
     def visit_choose(self, t):
-        for _ in self.start_block("for %s in (%s):" % (
+        with self.block("for %s in (%s):" % (
             t.children[0], t.children[1].additional_info[1:-1], )):
-            for _ in self.start_block("try:"):
+            with self.block("try:"):
                 self.dispatch(t.children[2])
                 self.emit("break")
-            for _ in self.start_block("except BacktrackException, _exc:"):
+            with self.block("except BacktrackException, _exc:"):
                 self.combine_error('_exc.error')
-        for _ in self.start_block("else:"):
+        with self.block("else:"):
             self.emit("raise BacktrackException(_error)")
 
     def visit_call(self, t):


More information about the pypy-commit mailing list