[Python-checkins] python/nondist/sandbox/parrotbench b4.py, NONE, 1.1 out4, NONE, 1.1 b.py, 1.1, 1.2 b0.py, 1.3, 1.4 out0, 1.2, 1.3

gvanrossum at users.sourceforge.net gvanrossum at users.sourceforge.net
Tue Dec 30 20:33:11 EST 2003


Update of /cvsroot/python/python/nondist/sandbox/parrotbench
In directory sc8-pr-cvs1:/tmp/cvs-serv11112

Modified Files:
	b.py b0.py out0 
Added Files:
	b4.py out4 
Log Message:
Another test, based on heapq.py.  Fixed and updated b0.py and out0.

--- NEW FILE: b4.py ---
sample = '''

def heappush(heap, item):
    heap.append(item)
    _siftdown(heap, 0, len(heap)-1)

def heappop(heap):
    lastelt = heap.pop()    # raises appropriate IndexError if heap is empty
    if heap:
        returnitem = heap[0]
        heap[0] = lastelt
        _siftup(heap, 0)
    else:
        returnitem = lastelt
    return returnitem

def heapreplace(heap, item):
    returnitem = heap[0]    # raises appropriate IndexError if heap is empty
    heap[0] = item
    _siftup(heap, 0)
    return returnitem

def heapify(x):
    n = len(x)
    for i in xrange(n//2 - 1, 0-1, 0-1):
        _siftup(x, i)

def _siftdown(heap, startpos, pos):
    newitem = heap[pos]
    while pos > startpos:
        parentpos = (pos - 1) >> 1
        parent = heap[parentpos]
        if parent <= newitem:
            break
        heap[pos] = parent
        pos = parentpos
    heap[pos] = newitem

def _siftup(heap, pos):
    endpos = len(heap)
    startpos = pos
    newitem = heap[pos]
    # Bubble up the smaller child until hitting a leaf.
    childpos = 2*pos + 1    # leftmost child position
    while childpos < endpos:
        # Set childpos to index of smaller child.
        rightpos = childpos + 1
        if rightpos < endpos:
            if heap[rightpos] <= heap[childpos]:
                childpos = rightpos
        # Move the smaller child up.
        heap[pos] = heap[childpos]
        pos = childpos
        childpos = 2*pos + 1
    # The leaf at pos is empty now.  Put newitem there, and bubble it up
    # to its final resting place (by sifting its parents down).
    heap[pos] = newitem
    _siftdown(heap, startpos, pos)
'''

from b0 import Parser, Scanner, getcFromString, eval, instrumentTree, Node
scanner = Scanner(getcFromString(sample).next).tokenize()
parser = Parser(scanner)
root = parser.parse()
instrumentTree(Node)
env = {}
eval(root, env, env)
heappush = env['heappush']
heappop = env['heappop']
# Simple sanity test
heap = []
data = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0]
for item in data:
    heappush(heap, item)
sort = []
while heap:
    sort.append(heappop(heap))
print sort

--- NEW FILE: out4 ---
 Define.eval({}, {})
  Node.isgenerator()
  return False
  Node.isgenerator()
  return False
 return
 Define.eval({'heappush': <__main__.Function object at 0>}, {'heappush': <__main__.Function object at 0>})
  Node.isgenerator()
  return False
  If.isgenerator()
   Node.isgenerator()
   return False
   Node.isgenerator()
   return False
   Node.isgenerator()
   return False
   Node.isgenerator()
   return False
  return False
[...3670 lines suppressed...]
   return <built-in method pop of list object at 0>
  return 9
  Name.assign(9, {'heappop': <__main__.Function object at 0>, '_siftup': <__main__.Function object at 0>, 'heapify': <__main__.Function object at 0>, 'heappush': <__main__.Function object at 0>, '_siftdown': <__main__.Function object at 0>, 'heapreplace': <__main__.Function object at 0>}, {'heap': []})
  return
 return
 If.eval({'heappop': <__main__.Function object at 0>, '_siftup': <__main__.Function object at 0>, 'heapify': <__main__.Function object at 0>, 'heappush': <__main__.Function object at 0>, '_siftdown': <__main__.Function object at 0>, 'heapreplace': <__main__.Function object at 0>}, {'lastelt': 9, 'heap': []})
  Name.eval({'heappop': <__main__.Function object at 0>, '_siftup': <__main__.Function object at 0>, 'heapify': <__main__.Function object at 0>, 'heappush': <__main__.Function object at 0>, '_siftdown': <__main__.Function object at 0>, 'heapreplace': <__main__.Function object at 0>}, {'lastelt': 9, 'heap': []})
  return []
  Assign.eval({'heappop': <__main__.Function object at 0>, '_siftup': <__main__.Function object at 0>, 'heapify': <__main__.Function object at 0>, 'heappush': <__main__.Function object at 0>, '_siftdown': <__main__.Function object at 0>, 'heapreplace': <__main__.Function object at 0>}, {'lastelt': 9, 'heap': []})
   Name.eval({'heappop': <__main__.Function object at 0>, '_siftup': <__main__.Function object at 0>, 'heapify': <__main__.Function object at 0>, 'heappush': <__main__.Function object at 0>, '_siftdown': <__main__.Function object at 0>, 'heapreplace': <__main__.Function object at 0>}, {'lastelt': 9, 'heap': []})
   return 9
   Name.assign(9, {'heappop': <__main__.Function object at 0>, '_siftup': <__main__.Function object at 0>, 'heapify': <__main__.Function object at 0>, 'heappush': <__main__.Function object at 0>, '_siftdown': <__main__.Function object at 0>, 'heapreplace': <__main__.Function object at 0>}, {'lastelt': 9, 'heap': []})
   return
  return
 return
 Return.eval({'heappop': <__main__.Function object at 0>, '_siftup': <__main__.Function object at 0>, 'heapify': <__main__.Function object at 0>, 'heappush': <__main__.Function object at 0>, '_siftdown': <__main__.Function object at 0>, 'heapreplace': <__main__.Function object at 0>}, {'lastelt': 9, 'returnitem': 9, 'heap': []})
  Name.eval({'heappop': <__main__.Function object at 0>, '_siftup': <__main__.Function object at 0>, 'heapify': <__main__.Function object at 0>, 'heappush': <__main__.Function object at 0>, '_siftdown': <__main__.Function object at 0>, 'heapreplace': <__main__.Function object at 0>}, {'lastelt': 9, 'returnitem': 9, 'heap': []})
  return 9
 raise <__main__.DoReturn instance at 0>
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Index: b.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/parrotbench/b.py,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** b.py	31 Dec 2003 00:10:34 -0000	1.1
--- b.py	31 Dec 2003 01:33:07 -0000	1.2
***************
*** 3,4 ****
--- 3,5 ----
  import b2
  import b3
+ import b4

Index: b0.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/parrotbench/b0.py,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** b0.py	31 Dec 2003 00:10:34 -0000	1.3
--- b0.py	31 Dec 2003 01:33:07 -0000	1.4
***************
*** 1,7 ****
  def check(a, b):
      if not a == b:
!         print (a, b)
!         if __debug__:
!             raise AssertionError("%r != %r" % (a, b))
  
  def proto___new__(cls, name):
--- 1,5 ----
  def check(a, b):
      if not a == b:
!         raise AssertionError("%.30r != %.30r" % (a, b))
  
  def proto___new__(cls, name):
***************
*** 185,197 ****
  '''
  
- s = Scanner(getcFromString(sample).next)
- it = Clone(s.tokenize())
- it2 = Clone(it)
- L = []
- for pair in it:
-     L.append(pair)
- L2 = list(it2)
- check(L, L2)
- 
  operators = {
      '<': 1,
--- 183,186 ----
***************
*** 244,247 ****
--- 233,239 ----
          self.value = value
  
+ class DoBreak(Exception):
+     pass
+ 
  def eval(body, globals, locals):
      for stmt in body:
***************
*** 317,326 ****
          for value in self.seq.eval(globals, locals):
              self.var.assign(value, globals, locals)
!             eval(self.body, globals, locals)
      def geneval(self, globals, locals):
          for value in self.seq.eval(globals, locals):
              self.var.assign(value, globals, locals)
!             for v in geneval(self.body, globals, locals):
!                 yield v
  class While(Node):
      def __init__(self, test, body):
--- 309,324 ----
          for value in self.seq.eval(globals, locals):
              self.var.assign(value, globals, locals)
!             try:
!                 eval(self.body, globals, locals)
!             except DoBreak:
!                 break
      def geneval(self, globals, locals):
          for value in self.seq.eval(globals, locals):
              self.var.assign(value, globals, locals)
!             try:
!                 for v in geneval(self.body, globals, locals):
!                     yield v
!             except DoBreak:
!                 break
  class While(Node):
      def __init__(self, test, body):
***************
*** 333,356 ****
      def eval(self, globals, locals):
          while self.test.eval(globals, locals):
!             eval(self.body, globals, locals)
      def geneval(self, globals, locals):
          while self.test.eval(globals, locals):
!             for value in geneval(self.body, globals, locals):
!                 yield value
  class If(Node):
!     def __init__(self, test, body):
          self.test = test
          self.body = body
      def __repr__(self):
          return "%s(%r, ...)" % (self.__class__.__name__, self.test)
      def isgenerator(self):
!         return isgenerator(self.body)
      def eval(self, globals, locals):
          if self.test.eval(globals, locals):
              eval(self.body, globals, locals)
      def geneval(self, globals, locals):
          if self.test.eval(globals, locals):
              for value in geneval(self.body, globals, locals):
                  yield value
  class Return(Node):
      def __init__(self, expr=None):
--- 331,367 ----
      def eval(self, globals, locals):
          while self.test.eval(globals, locals):
!             try:
!                 eval(self.body, globals, locals)
!             except DoBreak:
!                 break
      def geneval(self, globals, locals):
          while self.test.eval(globals, locals):
!             try:
!                 for value in geneval(self.body, globals, locals):
!                     yield value
!             except DoBreak:
!                 break
  class If(Node):
!     def __init__(self, test, body, elsebody=None):
          self.test = test
          self.body = body
+         self.elsebody = elsebody
      def __repr__(self):
          return "%s(%r, ...)" % (self.__class__.__name__, self.test)
      def isgenerator(self):
!         return isgenerator(self.body) or (self.elsebody is not None and
!                                           isgenerator(self.elsebody))
      def eval(self, globals, locals):
          if self.test.eval(globals, locals):
              eval(self.body, globals, locals)
+         elif self.elsebody is not None:
+             eval(self.elsebody, globals, locals)
      def geneval(self, globals, locals):
          if self.test.eval(globals, locals):
              for value in geneval(self.body, globals, locals):
                  yield value
+         elif self.elsebody is not None:
+             for value in geneval(self.elsebody, globals, locals):
+                 yield value
  class Return(Node):
      def __init__(self, expr=None):
***************
*** 377,380 ****
--- 388,396 ----
              value = self.expr.eval(globals, locals)
          yield value
+ class Break(Node):
+     def __repr__(self):
+         return "%s()" % (self.__class__.__name__,)
+     def eval(self, globals, locals):
+         raise DoBreak()
  class Print(Node):
      def __init__(self, exprs):
***************
*** 438,441 ****
--- 454,460 ----
          v = self.expr.eval(globals, locals)
          return v[self.index.eval(globals, locals)]
+     def assign(self, value, globals, locals):
+         v = self.expr.eval(globals, locals)
+         v[self.index.eval(globals, locals)] = value
  class Call(Node):
      def __init__(self, expr, args):
***************
*** 524,527 ****
--- 543,547 ----
      def nexttoken(self):
          self.token, self.value = rv = self.scanner.next()
+         ##print rv
          return rv
      def expect(self, token, value=None):
***************
*** 540,544 ****
      def parse_stmt(self,
                     keywords=('def', 'for', 'while', 'if',
!                              'print', 'return', 'yield')):
          if self.value in keywords:
              return getattr(self, 'parse_' + self.value)()
--- 560,564 ----
      def parse_stmt(self,
                     keywords=('def', 'for', 'while', 'if',
!                              'print', 'return', 'yield', 'break')):
          if self.value in keywords:
              return getattr(self, 'parse_' + self.value)()
***************
*** 554,558 ****
              while self.value == ',':
                  self.nexttoken()
!                 arg.append(self.expect(NAME))
          self.expect(OPERATOR, ')')
          self.expect(OPERATOR, ':')
--- 574,578 ----
              while self.value == ',':
                  self.nexttoken()
!                 args.append(self.expect(NAME))
          self.expect(OPERATOR, ')')
          self.expect(OPERATOR, ':')
***************
*** 582,586 ****
          self.expect(NEWLINE)
          body = self.parse_body()
!         return If(test, body)
      def parse_body(self):
          self.expect(INDENT)
--- 602,613 ----
          self.expect(NEWLINE)
          body = self.parse_body()
!         if self.value != 'else':
!             elsebody = None
!         else:
!             self.expect(NAME, 'else')
!             self.expect(OPERATOR, ':')
!             self.expect(NEWLINE)
!             elsebody = self.parse_body()
!         return If(test, body, elsebody)
      def parse_body(self):
          self.expect(INDENT)
***************
*** 608,611 ****
--- 635,642 ----
          self.expect(NEWLINE)
          return Yield(e)
+     def parse_break(self):
+         self.expect(NAME, 'break')
+         self.expect(NEWLINE)
+         return Break()
      def parse_simple(self):
          e = self.parse_exprs()
***************
*** 651,655 ****
              elif self.value == '(':
                  self.nexttoken()
!                 args = self.parse_exprs()
                  self.expect(OPERATOR, ')')
                  t = Call(t, args)
--- 682,691 ----
              elif self.value == '(':
                  self.nexttoken()
!                 if self.value == ')':
!                     args = []
!                 else:
!                     args = self.parse_exprs()
!                     if isinstance(args, Exprs):
!                         args = args.exprs
                  self.expect(OPERATOR, ')')
                  t = Call(t, args)
***************
*** 671,678 ****
          raise SyntaxError
  
- scanner = Scanner(getcFromString(sample).next).tokenize()
- parser = Parser(scanner)
- root = parser.parse()
- 
  class List(list):
      __setitem__ = list.__setitem__
--- 707,710 ----
***************
*** 692,695 ****
--- 724,728 ----
  
  output = OutputFile()
+ 
  def write(s):
      s = str(s).replace('<b0.', '<__main__.')
***************
*** 710,717 ****
          print s,
      print >>output, s,
  def writeln(s=''):
      write(str(s) + '\n')
! indent = ""
  OutputFile.softspace = True
  def instrumentClass(cls):
      cname = cls.__name__ + '.'
--- 743,768 ----
          print s,
      print >>output, s,
+ 
  def writeln(s=''):
      write(str(s) + '\n')
! 
! def myhash(s, ord=ord):
!     if not s:
!         return 0
!     x = ord(s[0])<<7
!     for c in s:
!         x = ((1000003*x) ^ ord(c)) & 0xffffffffL
!     return x^len(s)
! 
! def checkoutput(n=0):
!     outputtext = output.getvalue()
!     check(strhash(outputtext), n)
! 
! strhash = myhash
! 
  OutputFile.softspace = True
+ 
+ indent = ""
+ 
  def instrumentClass(cls):
      cname = cls.__name__ + '.'
***************
*** 720,728 ****
          if hasattr(descr, '__get__'):
              setattr(cls, name, instrumentDescriptor(cname+name, descr))
  def unInstrumentClass(cls):
      for name in cls.__dict__:
!         descr = getattr(cls, name)
          if isinstance(descr, instrumentDescriptor):
              setattr(cls, name, descr.obj)
  class instrumentDescriptor(object):
      def __init__(self, name, obj):
--- 771,781 ----
          if hasattr(descr, '__get__'):
              setattr(cls, name, instrumentDescriptor(cname+name, descr))
+ 
  def unInstrumentClass(cls):
      for name in cls.__dict__:
!         descr = cls.__dict__[name]
          if isinstance(descr, instrumentDescriptor):
              setattr(cls, name, descr.obj)
+ 
  class instrumentDescriptor(object):
      def __init__(self, name, obj):
***************
*** 734,737 ****
--- 787,791 ----
              return result
          return instrumentCall(self.name, result)
+ 
  class instrumentCall(object):
      def __init__(self, name, obj):
***************
*** 763,784 ****
              indent = oldindent
  
  scanner = Scanner(getcFromString(sample).next).tokenize()
  parser = Parser(scanner)
- instrumentClass(Parser)
  root = parser.parse()
  
! def myhash(s, ord=ord):
!     if not s:
!         return 0
!     x = ord(s[0])<<7
!     for c in s:
!         x = ((1000003*x) ^ ord(c)) & 0xffffffffL
!     return x^len(s)
! 
! def checkoutput(n=0):
!     outputtext = output.getvalue()
!     check(strhash(outputtext), n)
! 
! strhash = myhash
  
  checkoutput(1365277872)
--- 817,847 ----
              indent = oldindent
  
+ def instrumentTree(base):
+     instrumentClass(base)
+     for cls in base.__subclasses__():
+         instrumentTree(cls)
+ 
+ def unInstrumentTree(base):
+     unInstrumentClass(base)
+     for cls in base.__subclasses__():
+         unInstrumentTree(cls)
+ 
+ s = Scanner(getcFromString(sample).next)
+ it = Clone(s.tokenize())
+ it2 = Clone(it)
+ L = []
+ for pair in it:
+     L.append(pair)
+ L2 = list(it2)
+ check(L, L2)
+ 
  scanner = Scanner(getcFromString(sample).next).tokenize()
  parser = Parser(scanner)
  root = parser.parse()
  
! scanner = Scanner(getcFromString(sample).next).tokenize()
! parser = Parser(scanner)
! instrumentClass(Parser)
! root = parser.parse()
  
  checkoutput(1365277872)
***************
*** 798,802 ****
  checkoutput(3960406533)
  
- ##unInstrumentClass(Parser)
  it = Clone(getcFromString(unicode(sample, "utf8")))
  it2 = Clone(it)
--- 861,864 ----
***************
*** 813,825 ****
  checkoutput(3960406533)
  
- def instrumentTree(base):
-     instrumentClass(base)
-     for cls in base.__subclasses__():
-         instrumentTree(cls)
- def unInstrumentTree(base):
-     unInstrumentClass(base)
-     for cls in base.__subclasses__():
-         unInstrumentTree(cls)
- 
  instrumentTree(Node)
  scanner = Clone(Scanner(it2.next).tokenize())
--- 875,878 ----
***************
*** 827,837 ****
  parser = Parser(scanner)
  root = parser.parse()
  env = {}
  eval(root, env, env)
  g = env['pi']()
  for i in range(10):
!     write(g.next())
! writeln()
! out = output.getvalue()
  
  unInstrumentTree(Node)
--- 880,892 ----
  parser = Parser(scanner)
  root = parser.parse()
+ checkoutput(111416090)
  env = {}
  eval(root, env, env)
  g = env['pi']()
+ digits = []
  for i in range(10):
!     digits.append(g.next())
! checkoutput(1793452098)
! print "".join(map(str, digits))
  
  unInstrumentTree(Node)
***************
*** 839,854 ****
  parser = Parser(scanner2)
  root = parser.parse()
  env = {}
  eval(root, env, env)
  g = env['pi']()
  for i in range(10):
!     write(g.next())
! writeln()
  out2 = output.getvalue()
- check(out, out2)
- check(strhash(out[:1000]), 773700721)
- check(strhash(out[100000:101000]), 3131788725)
- check(strhash(out[200000:201000]), 3508888315)
- check(strhash(out[300000:301000]), 199799004)
- check(strhash(out[400000:401000]), 84251599)
- check(strhash(out[-1000:]), 620844360)
--- 894,904 ----
  parser = Parser(scanner2)
  root = parser.parse()
+ checkoutput(0)
  env = {}
  eval(root, env, env)
  g = env['pi']()
+ digits = []
  for i in range(10):
!     digits.append(g.next())
! print "".join(map(str, digits))
  out2 = output.getvalue()

Index: out0
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/parrotbench/out0,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** out0	30 Dec 2003 16:28:25 -0000	1.2
--- out0	31 Dec 2003 01:33:07 -0000	1.3
***************
*** 5895,12368 ****
           return "Number(u'0')"
          return "Return(Number(u'0'))"
!        If.__init__(Binop(Name(u's'), u'==', String(u"''")), [Return(Number(u'0'))])
!        return
!        If.__repr__()
!         Binop.__repr__()
!          Name.__repr__()
!          return "Name(u's')"
!          Literal.__repr__()
!          return 'String(u"\'\'")'
[...6577 lines suppressed...]
   return 5L
! 5  Node.geneval({u'strhash': <__main__.Function object at 0>, u'pi': <__main__.Generator object at 0>}, {u'a': 14007054816000L, u'q': 25, u'b': 2787459998400L, u'd': 5L, u'k': 13, u'a1': 455729992416000L, u'p': 144, u'b1': 84139894238400L, u'd1': 5L})
   return <generator object at 0>
   Assign.eval({u'strhash': <__main__.Function object at 0>, u'pi': <__main__.Generator object at 0>}, {u'a': 14007054816000L, u'q': 25, u'b': 2787459998400L, u'd': 5L, u'k': 13, u'a1': 455729992416000L, u'p': 144, u'b1': 84139894238400L, u'd1': 5L})
--- 8871,8875 ----
   Name.eval({u'strhash': <__main__.Function object at 0>, u'pi': <__main__.Generator object at 0>}, {u'a': 14007054816000L, u'q': 25, u'b': 2787459998400L, u'd': 5L, u'k': 13, u'a1': 455729992416000L, u'p': 144, u'b1': 84139894238400L, u'd1': 5L})
   return 5L
!  Node.geneval({u'strhash': <__main__.Function object at 0>, u'pi': <__main__.Generator object at 0>}, {u'a': 14007054816000L, u'q': 25, u'b': 2787459998400L, u'd': 5L, u'k': 13, u'a1': 455729992416000L, u'p': 144, u'b1': 84139894238400L, u'd1': 5L})
   return <generator object at 0>
   Assign.eval({u'strhash': <__main__.Function object at 0>, u'pi': <__main__.Generator object at 0>}, {u'a': 14007054816000L, u'q': 25, u'b': 2787459998400L, u'd': 5L, u'k': 13, u'a1': 455729992416000L, u'p': 144, u'b1': 84139894238400L, u'd1': 5L})
***************
*** 15642,15644 ****
   Name.eval({u'strhash': <__main__.Function object at 0>, u'pi': <__main__.Generator object at 0>}, {u'a': 9576126383040000L, u'q': 29, u'b': 2742857884166400L, u'd': 3L, u'k': 15, u'a1': 346367486707200000L, u'p': 196, u'b1': 96034297911552000L, u'd1': 3L})
   return 3L
! 3 
--- 9173,9176 ----
   Name.eval({u'strhash': <__main__.Function object at 0>, u'pi': <__main__.Generator object at 0>}, {u'a': 9576126383040000L, u'q': 29, u'b': 2742857884166400L, u'd': 3L, u'k': 15, u'a1': 346367486707200000L, u'p': 196, u'b1': 96034297911552000L, u'd1': 3L})
   return 3L
! 3141592653
! 3141592653





More information about the Python-checkins mailing list