Armin Rigo avatar Armin Rigo committed d254c64

issue47 in-progress

New official entry points into PyPy:
A dist/pypy/bin/py.py
A dist/pypy/bin/translator.py
D pypy/interpreter/py.py

translate_pypy and associated files have been moved:
A dist/pypy/translator/goal
D dist/goal

There is a new directory for demo material, where I kept some of the examples
of the old goal directory and commented them:
RM dist/demo
M dist/demo/foodbill.py
M dist/demo/dis-goal.py
M dist/demo/http-and-html.py

There are three new demos too. Two about the thunk object space and one
showing a nicely annotated and translated piece of code (from the Psyco tests,
originally by Neil Schemenauer):
AM demo/bpnn.py
A demo/sharedref.py
AM demo/fibonacci.py

Comments (0)

Files changed (49)

+import sys, os
+sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
+#!/usr/bin/python
+"""
+    Translator Demo
+
+    Run this file -- over regular Python! -- to analyse and type-annotate
+    the functions and class defined in this module, starting from the
+    entry point function demo().
+
+    Requires Pygame.
+"""
+# Back-Propagation Neural Networks
+# 
+# Written in Python.  See http://www.python.org/
+#
+# Neil Schemenauer <nascheme@enme.ucalgary.ca>
+#
+# Modifications to the original (Armin Rigo):
+#   * import random from PyPy's lib, which is Python 2.2's plain
+#     Python implementation
+#   * use sys.stdout.write() instead of print statements for now
+#   * starts the Translator instead of the demo by default.
+
+import sys
+import math
+
+# XXX the Translator needs the plain Python version of random.py:
+import autopath; from pypy.lib import random
+
+
+random.seed(0)
+
+# calculate a random number where:  a <= rand < b
+def rand(a, b):
+    return (b-a)*random.random() + a
+
+# Make a matrix (we could use NumPy to speed this up)
+def makeMatrix(I, J, fill=0.0):
+    m = []
+    for i in range(I):
+        m.append([fill]*J)
+    return m
+
+class NN:
+    
+    def __init__(self, ni, nh, no):
+        # number of input, hidden, and output nodes
+        self.ni = ni + 1 # +1 for bias node
+        self.nh = nh
+        self.no = no
+
+        # activations for nodes
+        self.ai = [1.0]*self.ni
+        self.ah = [1.0]*self.nh
+        self.ao = [1.0]*self.no
+        
+        # create weights
+        self.wi = makeMatrix(self.ni, self.nh)
+        self.wo = makeMatrix(self.nh, self.no)
+        # set them to random vaules
+        for i in range(self.ni):
+            for j in range(self.nh):
+                self.wi[i][j] = rand(-2.0, 2.0)
+        for j in range(self.nh):
+            for k in range(self.no):
+                self.wo[j][k] = rand(-2.0, 2.0)
+
+        # last change in weights for momentum   
+        self.ci = makeMatrix(self.ni, self.nh)
+        self.co = makeMatrix(self.nh, self.no)
+
+    def update(self, inputs):
+        if len(inputs) != self.ni-1:
+            raise ValueError, 'wrong number of inputs'
+
+        # input activations
+        for i in range(self.ni-1):
+            #self.ai[i] = 1.0/(1.0+math.exp(-inputs[i]))
+            self.ai[i] = inputs[i]
+
+        # hidden activations
+        for j in range(self.nh):
+            sum = 0.0
+            for i in range(self.ni):
+                sum = sum + self.ai[i] * self.wi[i][j]
+            self.ah[j] = 1.0/(1.0+math.exp(-sum))
+
+        # output activations
+        for k in range(self.no):
+            sum = 0.0
+            for j in range(self.nh):
+                sum = sum + self.ah[j] * self.wo[j][k]
+            self.ao[k] = 1.0/(1.0+math.exp(-sum))
+
+        return self.ao[:]
+
+
+    def backPropagate(self, targets, N, M):
+        if len(targets) != self.no:
+            raise ValueError, 'wrong number of target values'
+
+        # calculate error terms for output
+        output_deltas = [0.0] * self.no
+        for k in range(self.no):
+            ao = self.ao[k]
+            output_deltas[k] = ao*(1-ao)*(targets[k]-ao)
+
+        # calculate error terms for hidden
+        hidden_deltas = [0.0] * self.nh
+        for j in range(self.nh):
+            sum = 0.0
+            for k in range(self.no):
+                sum = sum + output_deltas[k]*self.wo[j][k]
+            hidden_deltas[j] = self.ah[j]*(1-self.ah[j])*sum
+
+        # update output weights
+        for j in range(self.nh):
+            for k in range(self.no):
+                change = output_deltas[k]*self.ah[j]
+                self.wo[j][k] = self.wo[j][k] + N*change + M*self.co[j][k]
+                self.co[j][k] = change
+                #print N*change, M*self.co[j][k]
+
+        # update input weights
+        for i in range(self.ni):
+            for j in range(self.nh):
+                change = hidden_deltas[j]*self.ai[i]
+                self.wi[i][j] = self.wi[i][j] + N*change + M*self.ci[i][j]
+                self.ci[i][j] = change
+
+        # calculate error
+        error = 0.0
+        for k in range(len(targets)):
+            error = error + 0.5*(targets[k]-self.ao[k])**2
+        return error
+
+
+    def test(self, patterns):
+        for p in patterns:
+            print_('%s -> %s' % (p[0], self.update(p[0])))
+
+    def weights(self):
+        print_('Input weights:')
+        for i in range(self.ni):
+            print_(self.wi[i])
+        print_()
+        print_('Output weights:')
+        for j in range(self.nh):
+            print_(self.wo[j])
+
+    def train(self, patterns, iterations=2000, N=0.5, M=0.1):
+        # N: learning rate
+        # M: momentum factor
+        for i in xrange(iterations):
+            error = 0.0
+            for p in patterns:
+                inputs = p[0]
+                targets = p[1]
+                self.update(inputs)
+                error = error + self.backPropagate(targets, N, M)
+            if i % 100 == 0:
+                print_('error %-14f' % error)
+
+def print_(s):
+    sys.stdout.write(s+'\n')
+
+
+def demo():
+    # Teach network XOR function
+    pat = [
+        [[0,0], [0]],
+        [[0,1], [1]],
+        [[1,0], [1]],
+        [[1,1], [0]]
+    ]
+
+    # create a network with two input, two hidden, and two output nodes
+    n = NN(2, 3, 1)
+    # train it with some patterns
+    n.train(pat, 2000)
+    # test it
+    n.test(pat)
+
+
+
+if __name__ == '__main__':
+    #demo()
+
+    print 'Loading...'
+    from pypy.translator.translator import Translator
+    t = Translator(demo)
+    print 'Annotating...'
+    a = t.annotate([])
+    a.simplify()
+    #a.specialize()
+    print 'Displaying the call graph and the class hierarchy.'
+    t.viewcg()
+    print 'Compiling...'
+    f = t.ccompile()
+    print 'Running...'
+    f()
+"""
+An old-time classical example, and one of our first goals.
+To run on top of PyPy.
+"""
+
+import dis
+dis.dis(dis.dis)

demo/fibonacci.py

+"""
+Thunk (a.k.a. lazy objects) in PyPy.
+To run on top of the thunk object space with the following command-line:
+
+    py.py -o thunk thunk-demo.py
+
+This is a typical Functional Programming Languages demo, computing the
+Fibonacci sequence by using an infinite lazy linked list.
+"""
+
+try:
+    thunk    # only available in 'py.py -o thunk'
+except NameError:
+    print __doc__
+    raise SystemExit(2)
+
+# ____________________________________________________________
+
+
+class ListNode:
+    def __init__(self, head, tail):
+        self.head = head   # the first element of the list
+        self.tail = tail   # the sublist of all remaining elements
+
+
+def add_lists(list1, list2):
+    """Compute the linked-list equivalent of the Python expression
+          [a+b for (a,b) in zip(list1,list2)]
+    """
+    return ListNode(list1.head + list2.head,
+                    thunk(add_lists, list1.tail, list2.tail))
+
+
+# 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
+Fibonacci = ListNode(1, ListNode(1, None))
+Fibonacci.tail.tail = thunk(add_lists, Fibonacci, Fibonacci.tail)
+
+
+if __name__ == '__main__':
+    node = Fibonacci
+    while True:
+        print node.head
+        node = node.tail
+"""
+Of historical interest: we computed the food bill of our first Gothenburg
+sprint with PyPy :-)
+"""
+
+slips=[(1, 'Kals MatMarkn', 6150, 'Chutney for Curry', 'dinner Saturday'),
+       (2, 'Kals MatMarkn', 32000, 'Spaghetti, Beer', 'dinner Monday'),
+       (2, 'Kals MatMarkn', -810, 'Deposit on Beer Bottles', 'various'),
+       (3, 'Fram', 7700, 'Rice and Curry Spice', 'dinner Saturday'),
+       (4, 'Kals MatMarkn', 25000, 'Alcohol-Free Beer, sundries', 'various'),
+       (4, 'Kals MatMarkn', -1570, "Michael's toothpaste", 'none'),
+       (4, 'Kals MatMarkn', -1690, "Laura's toothpaste", 'none'),
+       (4, 'Kals MatMarkn', -720, 'Deposit on Beer Bottles', 'various'),
+       (4, 'Kals MatMarkn', -60, 'Deposit on another Beer Bottle', 'various'),
+       (5, 'Kals MatMarkn', 26750, 'lunch bread meat cheese', 'lunch Monday'),
+       (6, 'Kals MatMarkn', 15950, 'various', 'dinner Tuesday and Thursday'),
+       (7, 'Kals MatMarkn', 3650, 'Drottningsylt, etc.', 'dinner Thursday'),
+       (8, 'Kals MatMarkn', 26150, 'Chicken and Mushroom Sauce', 'dinner Wed'),
+       (8, 'Kals MatMarkn', -2490, 'Jacob and Laura -- juice', 'dinner Wed'),
+       (8, 'Kals MatMarkn', -2990, "Chicken we didn't cook", 'dinner Wednesday'),
+       (9, 'Kals MatMarkn', 1380, 'fruit for Curry', 'dinner Saturday'),
+       (9, 'Kals MatMarkn', 1380, 'fruit for Curry', 'dinner Saturday'),
+       (10, 'Kals MatMarkn', 26900, 'Jansons Frestelse', 'dinner Sunday'),
+       (10, 'Kals MatMarkn', -540, 'Deposit on Beer Bottles', 'dinner Sunday'),
+       (11, 'Kals MatMarkn', 22650, 'lunch bread meat cheese', 'lunch Thursday'),
+       (11, 'Kals MatMarkn', -2190, 'Jacob and Laura -- juice', 'lunch Thursday'),
+       (11, 'Kals MatMarkn', -2790, 'Jacob and Laura -- cereal', 'lunch Thurs'),
+       (11, 'Kals MatMarkn', -760, 'Jacob and Laura -- milk', 'lunch Thursday'),
+       (12, 'Kals MatMarkn', 18850, 'lunch bread meat cheese', 'lunch Friday'),
+       (13, 'Kals MatMarkn', 18850, 'lunch bread meat cheese', 'guestimate Sun'),
+       (14, 'Kals MatMarkn', 18850, 'lunch bread meat cheese', 'guestimate Tues'),
+       (15, 'Kals MatMarkn', 20000, 'lunch bread meat cheese', 'guestimate Wed'),
+       (16, 'Kals MatMarkn', 42050, 'grillfest', 'dinner Friday'),
+       (16, 'Kals MatMarkn', -1350, 'Deposit on Beer Bottles', 'dinner Friday'),
+       (17, 'System Bolaget', 15500, 'Cederlunds Caloric', 'dinner Thursday'),
+       (17, 'System Bolaget', 22400, '4 x Farnese Sangiovese 56SEK', 'various'),
+       (17, 'System Bolaget', 22400, '4 x Farnese Sangiovese 56SEK', 'various'),
+       (17, 'System Bolaget', 13800, '2 x Jacobs Creek 69SEK', 'various'),
+       (18, 'J and Ls winecabinet', 10800, '2 x Parrotes 54SEK', 'various'),
+       (18, 'J and Ls winecabinet', 14700, '3 x Saint Paulin 49SEK', 'various'),
+       (18, 'J and Ls winecabinet', 10400, '2 x Farnese Sangioves 52SEK',
+        'cheaper when we bought it'),
+       (18, 'J and Ls winecabinet', 17800, '2 x Le Poiane 89SEK', 'various'),
+       (18, 'J and Ls winecabinet', 9800, '2 x Something Else 49SEK', 'various'),
+       (19, 'Konsum', 26000, 'Saturday Bread and Fruit', 'Slip MISSING'),
+       (20, 'Konsum', 15245, 'Mooseburgers', 'found slip'),
+       (21, 'Kals MatMarkn', 20650, 'Grilling', 'Friday dinner'),
+       (22, 'J and Ls freezer', 21000, 'Meat for Curry, grilling', ''),
+       (22, 'J and Ls cupboard', 3000, 'Rice', ''),
+       (22, 'J and Ls cupboard', 4000, 'Charcoal', ''),
+       (23, 'Fram', 2975, 'Potatoes', '3.5 kg @ 8.50SEK'),
+       (23, 'Fram', 1421, 'Peas', 'Thursday dinner'),
+       (24, 'Kals MatMarkn', 20650, 'Grilling', 'Friday dinner'),
+       (24, 'Kals MatMarkn', -2990, 'TP', 'None'),
+       (24, 'Kals MatMarkn', -2320, 'T-Gul', 'None')
+       ]
+ 
+print [t[2] for t in slips]
+print (reduce(lambda x, y: x+y, [t[2] for t in slips], 0))/900

demo/http-and-html.py

+"""
+    Standard Library usage demo.
+    Uses urllib and htmllib to download and parse a web page.
+
+    The purpose of this demo is to remind and show that almost all
+    pure-Python modules of the Standard Library work just fine.
+"""
+
+url = 'http://www.python.org/'
+html = 'python.html'
+import urllib
+content = urllib.urlopen(url).read()
+file(html, 'w').write(content)
+import htmllib
+htmllib.test([html])
+import os
+os.remove(html)

demo/sharedref.py

+"""
+   This is an example usage of the 'thunk' object space of PyPy.
+   It implements transparent distributed object manipulation.
+
+   Start a server on a local port, say port 8888, with:
+
+       $ py.py -o thunk sharedref.py 8888
+       Waiting for connection on port 8888
+
+   Then start and connect a client from the same or another machine:
+
+       $ py.py -o thunk sharedref.py ip_or_name:8888
+       Connecting to ('...', 8888)
+       Ok
+       >>> l = [1,2,3]
+       >>> chan.send(l)    # send the list to the server over the connexion
+
+   On the server-side:
+
+       Connected from ('...', 1046)
+       >>> l = chan.recv()    # receive the list sent above
+       >>> l
+       [1, 2, 3]
+       >>> l.append(4)
+
+   Back on the client-side:
+
+       >>> l
+       [1, 2, 3, 4]
+
+   The list behaves like a single distributed object, which both sides can
+   modify and access without needing further explicit synchronization.
+   There is no difference between who was the original sender or receiver of
+   the object, nor between which side was originally 'server' or 'client'.
+"""
+
+import sys, marshal
+from socket import *
+from select import select
+
+
+class Channel:
+
+    def __init__(self, s, serverside):
+        # invariants: a shared object 'obj' is
+        #  - either remote, and a thunk, and not a value in self.cache
+        #  - or local (or at least on "our" side of this Channel), and
+        #    then it has a corresponding key in self.cache
+        self.s = s
+        self.cache = {}
+        self.inputfifo = []
+        self.count = int(not serverside)
+
+##    def _check(self, obj):
+##        print '%s: cache=%r' % (self, self.cache.keys()),
+##        if is_thunk(obj):
+##            print 'THUNK'
+##        else:
+##            print obj
+
+    def sendraw(self, obj):
+        data = marshal.dumps(obj)
+        hdr = str(len(data))
+        hdr = '0'*(10-len(hdr)) + hdr
+        self.s.sendall(hdr + data)
+
+    def _readbytes(self, count):
+        data = ''
+        while len(data) < count:
+            t = self.s.recv(count - len(data))
+            if not t:
+                raise EOFError
+            data += t
+        return data
+
+    def recvraw(self):
+        datasize = int(self._readbytes(10))
+        data = self._readbytes(datasize)
+        return marshal.loads(data)
+
+    def send(self, obj, n=None):
+        #print 'send', n,; self._check(obj)
+        if n is None:
+            n = self.count
+            self.count += 2
+            data = (n, obj, None)
+        else:
+            data = (n, obj)
+        self.sendraw(data)
+        become(obj, thunk(self._resume, n))
+        #print 'done', n,; self._check(obj)
+
+    def recv(self):
+        obj = self.inputfifo.pop(0)
+        #print 'recv',; self._check(obj)
+        return obj
+
+    def _resume(self, n):
+        #print 'resume', n,; sys.stdout.flush()
+        assert n not in self.cache
+        self.sendraw((n,))
+        while n not in self.cache:
+            self.handle_once()
+        obj = self.cache[n]
+        #self._check(obj)
+        return obj
+
+    def handle_once(self):
+        input = self.recvraw()
+        if len(input) > 1:
+            obj = input[1]
+            self.cache[input[0]] = obj
+            if len(input) > 2:
+                self.inputfifo.append(obj)
+        else:
+            n = input[0]
+            obj = self.cache[n]
+            self.send(obj, n)
+            del self.cache[n]
+
+
+def mainloop(channels):
+    stdin = sys.stdin.fileno()
+    sockfd = [chan.s.fileno() for chan in channels]
+    while True:
+        sys.stdout.write('>>> ')
+        sys.stdout.flush()
+        while True:
+            iwtd, owtd, ewtd = select([stdin] + sockfd, [], [stdin])
+            if stdin in iwtd or stdin in ewtd: break
+            for chan in channels:
+                if chan.s.fileno() in iwtd:
+                    chan.handle_once()
+        code = raw_input()
+        if not code: break
+        try:
+            co = compile(code, '<input>', 'single')
+            exec co in globals()
+        except Exception, e:
+            print e.__class__.__name__, str(e)
+
+
+def server(port):
+    s = socket(AF_INET, SOCK_STREAM)
+    s.bind(('', port))
+    s.listen(1)
+    print 'Waiting for connection on port', port
+    s, addr = s.accept()
+    print 'Connected from', addr
+    return Channel(s, True)
+
+def client(addr):
+    s = socket(AF_INET, SOCK_STREAM)
+    print 'Connecting to', addr
+    s.connect(addr)
+    print 'Ok'
+    return Channel(s, False)
+
+
+if __name__ == '__main__':
+    try:
+        thunk, become    # only available in 'py.py -o thunk'
+    except NameError:
+        print __doc__
+        raise SystemExit(2)
+
+    channels = []
+    for a in sys.argv[1:]:
+        try:
+            port = int(a)
+        except ValueError:
+            host, port = a.split(':')
+            port = int(port)
+            chan = client((host, port))
+        else:
+            chan = server(port)
+        channels.append(chan)
+
+    mainloop(channels)

goal/app_example.py

-print '--- beginning of app_example.py ---'
-print 6*7
-print '--- end of app_example.py ---'

goal/app_main.py

-# App-level version of py.py.
-# XXX very incomplete!  Blindly runs the file named as first argument.
-# No option checking, no interactive console, no fancy hooks.
-
-def entry_point(argv):
-    import sys
-    sys.executable = argv[0]
-    sys.argv = argv[1:]
-
-    mainmodule = type(sys)('__main__')
-    sys.modules['__main__'] = mainmodule
-
-    try:
-        execfile(sys.argv[0], mainmodule.__dict__)
-    except:
-        sys.excepthook(*sys.exc_info())
-        return 1
-    else:
-        return 0
-
-if __name__ == '__main__':
-    # debugging only
-    import sys
-    sys.exit(entry_point(sys.argv))

goal/autopath.py

-import sys, os
-sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

goal/buildcache.py

-from pypy.tool import option, autopath, testit
-from pypy.interpreter import gateway 
-import os
-
-#######################################################
-def app_triggerall():
-    import sys, types # , exceptions
-    k = 42
-    def gen():
-        yield k
-        #yield (k, 1.0, 4L, [''], {}, unicode, Ellipsis) 
-        try:
-            raise ValueError
-        except ValueError: 
-            x = sys.exc_info()
-        try:
-            raise x[0], x[1], x[2]
-        except ValueError:
-            pass
-            
-    #gen.func_code.co_name
-    str({'co_name': ('f',)}), str(object.__init__.im_func.func_code)
-    #"for %r" % ({'x': gen}) 
-    "%02d %04f %05g %05s <%s> %r" % (1, 2.25, 2.25, 2.25, [1,2], {'x': gen})
-    for x in gen():
-        pass
-
-def app_triggerexec():
-    exec "i=3"
-    
-gateway.importall(globals())   # app_xxx() -> xxx()
-
-#######################################################
-from pypy.objspace.std import stdtypedef
-    
-def buildcache(space): 
-    print "triggering cache build for %r" % space 
-    triggerall(space) 
-    triggerexec(space)
-    #testit.main(os.path.join(autopath.pypydir, 'objspace', 'std'))
-    #Cache.freeze()
-    #space._typecache = frozendict(space._typecache) 
-    #space._faketypecache = frozendict(space._faketypecache) 
-    #space._gatewaycache = frozendict(space._gatewaycache) 
-    #space = option.objspace('std')
-    #buildcache(space)
-    #for x in  stdtypedef._temp:
-    #    x.cache_table = frozendict(x.cache_table)
-    print "cache build finished, caches are 'frozen' now"
-
-if __name__ == '__main__': 
-    space = option.objspace('std') 
-    buildcache(space) 
-    #testit.main(autopath.pypydir)
-
-    testit.main(os.path.join(autopath.pypydir)) # , 'objspace', 'std'))

goal/buildcache2.py

-
-def buildcache(space):
-    from pypy.interpreter.typedef import interptypes
-    
-    space.builtin.getdict()
-    print "*builtin*"
-    w_dic = space.builtin.w_dict
-    #print space.unwrap(space.call_method(w_dic,"keys"))
-    
-
-    space.sys.getdict()
-    print "*sys*"
-    w_dic = space.sys.w_dict
-    #print space.unwrap(space.call_method(w_dic,"keys"))
-
-    # others
-    w_modules = space.sys.get('modules')
-    def getmodule(name):
-        return space.getitem(w_modules, space.wrap(name))
-
-    getmodule('parser').getdict()
-    print "*parser*"
-
-    for typedef in interptypes:
-        w_typ = space.gettypeobject(typedef)
-        w_typ.getdict()
-        print "*%s*" % typedef.name
-        
-    for typedef in space.model.pythontypes:
-        w_typ = getattr(space, 'w_' + typedef.name)
-        w_typ.getdict()
-
-        print "*%s*" % typedef.name
-        #print w_typ.dict_w.keys()
-
-    space.builtin.get('file').getdict()
-
-    space.appexec([],"""():
-    try:
-       raise ValueError
-    except ValueError:
-       pass
-    exec 'pass'    
-""")
-    # freeze caches?
-    print "cache build finished"
-
-if __name__ == '__main__':
-    import autopath    
-    from pypy.objspace.std.objspace import StdObjSpace
-
-    space = StdObjSpace()
-
-    buildcache(space)

goal/compiler-hack.py

-import compiler
-c = compiler.compile('a=1', '', 'exec')
-import dis
-dis.dis(c)
-exec c
-print a

goal/dis-goal.py

-import dis
-dis.dis(dis.dis)

goal/dis-pregoal.py

-import dis
-dis.disassemble(dis.dis.func_code)

goal/foodbill.py

-slips=[(1, 'Kals MatMarkn', 6150, 'Chutney for Curry', 'dinner Saturday'),
-       (2, 'Kals MatMarkn', 32000, 'Spaghetti, Beer', 'dinner Monday'),
-       (2, 'Kals MatMarkn', -810, 'Deposit on Beer Bottles', 'various'),
-       (3, 'Fram', 7700, 'Rice and Curry Spice', 'dinner Saturday'),
-       (4, 'Kals MatMarkn', 25000, 'Alcohol-Free Beer, sundries', 'various'),
-       (4, 'Kals MatMarkn', -1570, "Michael's toothpaste", 'none'),
-       (4, 'Kals MatMarkn', -1690, "Laura's toothpaste", 'none'),
-       (4, 'Kals MatMarkn', -720, 'Deposit on Beer Bottles', 'various'),
-       (4, 'Kals MatMarkn', -60, 'Deposit on another Beer Bottle', 'various'),
-       (5, 'Kals MatMarkn', 26750, 'lunch bread meat cheese', 'lunch Monday'),
-       (6, 'Kals MatMarkn', 15950, 'various', 'dinner Tuesday and Thursday'),
-       (7, 'Kals MatMarkn', 3650, 'Drottningsylt, etc.', 'dinner Thursday'),
-       (8, 'Kals MatMarkn', 26150, 'Chicken and Mushroom Sauce', 'dinner Wed'),
-       (8, 'Kals MatMarkn', -2490, 'Jacob and Laura -- juice', 'dinner Wed'),
-       (8, 'Kals MatMarkn', -2990, "Chicken we didn't cook", 'dinner Wednesday'),
-       (9, 'Kals MatMarkn', 1380, 'fruit for Curry', 'dinner Saturday'),
-       (9, 'Kals MatMarkn', 1380, 'fruit for Curry', 'dinner Saturday'),
-       (10, 'Kals MatMarkn', 26900, 'Jansons Frestelse', 'dinner Sunday'),
-       (10, 'Kals MatMarkn', -540, 'Deposit on Beer Bottles', 'dinner Sunday'),
-       (11, 'Kals MatMarkn', 22650, 'lunch bread meat cheese', 'lunch Thursday'),
-       (11, 'Kals MatMarkn', -2190, 'Jacob and Laura -- juice', 'lunch Thursday'),
-       (11, 'Kals MatMarkn', -2790, 'Jacob and Laura -- cereal', 'lunch Thurs'),
-       (11, 'Kals MatMarkn', -760, 'Jacob and Laura -- milk', 'lunch Thursday'),
-       (12, 'Kals MatMarkn', 18850, 'lunch bread meat cheese', 'lunch Friday'),
-       (13, 'Kals MatMarkn', 18850, 'lunch bread meat cheese', 'guestimate Sun'),
-       (14, 'Kals MatMarkn', 18850, 'lunch bread meat cheese', 'guestimate Tues'),
-       (15, 'Kals MatMarkn', 20000, 'lunch bread meat cheese', 'guestimate Wed'),
-       (16, 'Kals MatMarkn', 42050, 'grillfest', 'dinner Friday'),
-       (16, 'Kals MatMarkn', -1350, 'Deposit on Beer Bottles', 'dinner Friday'),
-       (17, 'System Bolaget', 15500, 'Cederlunds Caloric', 'dinner Thursday'),
-       (17, 'System Bolaget', 22400, '4 x Farnese Sangiovese 56SEK', 'various'),
-       (17, 'System Bolaget', 22400, '4 x Farnese Sangiovese 56SEK', 'various'),
-       (17, 'System Bolaget', 13800, '2 x Jacobs Creek 69SEK', 'various'),
-       (18, 'J and Ls winecabinet', 10800, '2 x Parrotes 54SEK', 'various'),
-       (18, 'J and Ls winecabinet', 14700, '3 x Saint Paulin 49SEK', 'various'),
-       (18, 'J and Ls winecabinet', 10400, '2 x Farnese Sangioves 52SEK',
-        'cheaper when we bought it'),
-       (18, 'J and Ls winecabinet', 17800, '2 x Le Poiane 89SEK', 'various'),
-       (18, 'J and Ls winecabinet', 9800, '2 x Something Else 49SEK', 'various'),
-       (19, 'Konsum', 26000, 'Saturday Bread and Fruit', 'Slip MISSING'),
-       (20, 'Konsum', 15245, 'Mooseburgers', 'found slip'),
-       (21, 'Kals MatMarkn', 20650, 'Grilling', 'Friday dinner'),
-       (22, 'J and Ls freezer', 21000, 'Meat for Curry, grilling', ''),
-       (22, 'J and Ls cupboard', 3000, 'Rice', ''),
-       (22, 'J and Ls cupboard', 4000, 'Charcoal', ''),
-       (23, 'Fram', 2975, 'Potatoes', '3.5 kg @ 8.50SEK'),
-       (23, 'Fram', 1421, 'Peas', 'Thursday dinner'),
-       (24, 'Kals MatMarkn', 20650, 'Grilling', 'Friday dinner'),
-       (24, 'Kals MatMarkn', -2990, 'TP', 'None'),
-       (24, 'Kals MatMarkn', -2320, 'T-Gul', 'None')
-       ]
- 
-print [t[2] for t in slips]
-print (reduce(lambda x, y: x+y, [t[2] for t in slips], 0))/900

goal/gadfly-demo.py

-import os
-os.mkdir('db')
-
-import gadfly
-connection = gadfly.gadfly()
-connection.startup('test', 'db')
-cursor = connection.cursor()
-
-def print_and_execute(cursor, operation):
-    print operation
-    cursor.execute(operation)
-
-print_and_execute(cursor, "CREATE TABLE pypy(py varchar)")
-print_and_execute(cursor, "INSERT INTO pypy(py) VALUES ('py')")
-print_and_execute(cursor, "SELECT * FROM pypy")
-for row in cursor.fetchall():
-    print row
-
-connection.commit()
-connection.close()
-
-import shutil
-shutil.rmtree('db')

goal/hello_world.py

-aStr = 'hello world'
-print len(aStr)

goal/http-and-html.py

-url = 'http://www.python.org/'
-html = 'python.html'
-import urllib
-content = urllib.urlopen(url).read()
-file(html, 'w').write(content)
-import htmllib
-htmllib.test([html])
-import os
-os.remove(html)

goal/objectspace-reconstruct.py

-
-def __getattribute__(w_obj,w_name):
-  name = space.unwrap(w_name)
-  w_descr = space.lookup(w_obj, name)
-  if w_descr is not None:
-     if space.is_data_descr(w_descr):
-         return space.get(w_descr,w_obj,space.type(w_obj))
-  w_dict = space.getdict(w_obj)
-  if w_dict is not None:
-     try:
-       return space.getitem(w_dict,w_name)
-     except OperationError, e:
-       if not e.match(space,space.w_KeyError):
-         raise
-  if w_descr is not None:
-     return space.get(w_descr,w_obj,space.wrap(type))
-  raise OperationError(space.w_AttributeError,w_name)
-
-def space.getattr(w_obj,w_name):
-   w_descr = space.lookup(w_obj, '__getattribute__')
-   try:
-     return space.get_and_call_function(w_descr, w_obj, w_name)
-   except: # AttributeError
-     w_descr = space.lookup(w_obj, '__getattr__')
-     if w_descr is None:
-       raise
-     return space.get_and_call_function(w_descr, w_obj, w_name)
-
-def space.get(w_obj,w_name):
-   w_descr = space.lookup(w_obj, '__get__')
-   if w_descr is not None:
-     return space.get_and_call(w_descr, w_obj, space.newtuple([w_name]))
-   else:
-     return w_obj
-
-def space.call(w_obj, w_args, w_kwargs):
-   w_descr = space.lookup(w_obj, '__call__')
-   if w_descr is None:
-     raise OperationError(space.w_TypeError, space.wrap('...'))
-   return space.get_and_call(w_descr, w_obj, w_args, w_kwargs)
-
-
-class BaseObjSpace:
-  def get_and_call(self, w_descr, w_obj, w_args, w_kwargs):
-    if isinstance(w_descr, W_Function):
-      args_w = space.unpacktuple(w_args)
-      return w_descr.func.call(space.newtuple([w_obj]+args_w),w_kwargs)
-    else:
-      w_bound = space.get(w_descr,w_obj,space.gettype(w_obj))
-      return space.call(w_bound, w_args, w_kwargs)
-
-class Wrappable:
-    def __wrap__(self, space):
-        return self
-
-class Function(Wrappable):
-    TypeDef = Type("function", [], {
-        '__call__' : app_descr_function_call,
-        'func_code' : Property(func_code_getter) 
-        })
-
-class BuiltinType:
-    def __init__(self, name, bases, rawdict):
-        self.name = name 
-        self.bases = bases 
-        self.rawdict = rawdict
-        self.mro = []
-
-class W_Function:
-    def __init__(self, space, func):
-        self.func = func
-        self.space = space
-
-    def gettype(self):
-        space = self.space
-        try:
-            return space.FunctionType 
-        except AttributeError:
-            space.FunctionType = f = Type(space, [space.ObjectType]) 
-            f.dict_w['__call__'] = space.wrap(app_descr_function_call)
-            func_code_property = Property(func_code_getter)
-            f.dict_w['func_code'] = space.wrap(func_code_property) 
-            return f 
-
-class StdObjectSpace:
-    def lookup(space, w_obj, name):
-        typ = space._gettype(w_obj)
-        return space.wrap(typ.lookup(name))
-
-    def type(space,w_obj):
-        return space.wrap(space._gettype(w_obj))
-
-    def _gettype
-        try:
-            return space._types[w_obj.__class__]
-        except KeyError:
-            typ = space.buildtype(w_obj.TypeDef) 
-            space._types[w_obj.__class__] = typ
-            return typ
-
-    def buildtype(space, typedef):
-        typ = Type(w_
-        for name, value 
-
-    def wrap(space, obj):
-        
-        assert self.space == space 
-        return W_Type(space, self)
-
-def trivspace.lookup(space, w_obj, name):
-    if isinstance(w_obj, Wrappable):
-        for basedef in w_obj.TypeDef.mro():
-            if name in basedef.rawdict:
-                return space.wrap(basedef.rawdict[name])
-        return None 
-    else:
-        for cls in w_obj.__class__.__mro__:
-            if name in cls.__dict__:
-                return cls.__dict__[name]
-        return None
-
-
-def func_code_getter(space,w_func):
-    return space.wrap(w_func.func.code)
-
-def object___class__(space,w_obj):
-    return space.type(w_obj)
-
-def descr_function_call(space, w_func, w_args, w_kwds): 
-    return w_func.func.call(w_args, w_kwds)
-app_descr_function_call = gateway.interp2app(descr_function_call) 
-
-
-
-
-class Property:
-    def __init__(self, fget, fset=None, fdel=None, doc=None):
-        self.fget = fget
-        self.fset = fset
-        self.fdel = fdel
-        self.doc = doc
-    def __wrap__(self, space):
-        return W_Property(space, self)
-
-class W_Property(...Wrapped):
-    def __init__(self, space, property):
-        self.space = space
-        self.property = property
-    def gettype(self):
-        space = self.space
-        try:
-            return space.PropertyType
-        except AttributeError:
-            space.PropertyType = t = Type(space, "builtin-property", [])
-            t.dict_w["__get__"] = space.wrap(app_descr_property_get)
-            return t
-
-def descr_property_get(space, w_property, w_obj, w_ignored):
-    return w_property.property.fget(space, w_obj)
-
-app_descr_property_get = gateway.interp2app(descr_property_get)
-
-

goal/pydoc-goal.py

-import pydoc
-pydoc.help(pydoc)

goal/pydoc-pregoal.py

-import pydoc

goal/stringcomp.py

-
-### a trivial program to test strings, lists, functions and methods ###
-
-def addstr(s1,s2):
-    return s1 + s2
-
-str = "an interesting string"
-str2 = 'another::string::xxx::y:aa'
-str3 = addstr(str,str2)
-arr = []
-for word in str.split():
-    if word in str2.split('::'):
-        arr.append(word)
-print ''.join(arr)
-print "str + str2 = ", str3
-
-

goal/targetpypy.py

-import buildcache2
-from pypy.objspace.std.objspace import StdObjSpace, W_Object
-from pypy.objspace.std.intobject import W_IntObject
-
-# __________  Entry point  __________
-
-def entry_point():
-    w_a = W_IntObject(space, -6)
-    w_b = W_IntObject(space, -7)
-    return space.mul(w_a, w_b)
-
-# _____ Define and setup target ___
-
-def target():
-    global space
-    # disable translation of the whole of classobjinterp.py
-    StdObjSpace.setup_old_style_classes = lambda self: None
-    space = StdObjSpace()
-    # call cache filling code
-    buildcache2.buildcache(space)    
-    # further call the entry_point once to trigger building remaining
-    # caches (as far as analyzing the entry_point is concerned)
-    entry_point()
-
-    return entry_point, []
-
-# _____ Run translated _____
-def run(c_entry_point):
-    w_result = c_entry_point()
-    print w_result
-    print w_result.intval
-    assert w_result.intval == 42

goal/targetpypy0.py

-from pypy.objspace import dummy
-from pypy.interpreter.pycode import PyCode
-
-# __________  Entry point  __________
-
-def entry_point(code, w_loc):
-    code2 = PyCode(space)
-    code2 = code2._from_code(code)
-    code2.exec_code(space, space.wrap({}), w_loc)
-
-# _____ Define and setup target _____
-
-def target():
-    global space
-    space = dummy.DummyObjSpace()
-
-    from pypy.interpreter import pycode
-
-    pycode.setup_frame_classes()
-
-    from pypy.interpreter import pyopcode
-
-    # cheat
-    space._gatewaycache.content[pyopcode.app] =  space.newdict([])
-
-    return entry_point,[object, dummy.W_Obj]
-
-# _____ Run translated _____
-
-def run(c_entry_point):
-    w_result = c_entry_point(compile("a+b","<stuff>","eval"),dummy.W_Obj())
-    print w_result
-

goal/targetpypy1.py

-from pypy.objspace.std.objspace import StdObjSpace, W_Object
-from pypy.objspace.std.intobject import W_IntObject
-from pypy.objspace.std import stdtypedef
-
-# __________  Entry point  __________
-
-operations = "mul add sub div mod lshift rshift floordiv truediv ".split()
-
-def entry_point():
-    w_a = W_IntObject(space, -7)
-    w_b = W_IntObject(space, 6)
-    results_w = [mmentrypoints[op](space, w_a, w_b) for op in operations]
-    return [space.unwrap(each) for each in resuls_w]
-
-# flatten the above code, to get a nicer look
-def make_flat_code():
-    g = globals()
-    # make globals constants from the operations
-    code = """def entry_point():
-    import sys
-    w_a = W_IntObject(space, -7)
-    # -sys.maxint-1 crashes: genc problem with OP_SUB and int constant
-    # when implementing lshift_Long_Long and rshift__Long_Long
-    w_b = W_IntObject(space,  6)
-    results_w = []
-    append = results_w.append
-"""
-    for op in operations:
-        g["op_%s" % op] = mmentrypoints[op]
-        line = "    append(op_%s(space, w_a, w_b))" % op
-        code += line + '\n'
-    code += "    return [space.unwrap(each) for each in results_w]\n"
-    print code
-    exec code in g
-    
-# _____ Define and setup target _____
-def target():
-    global space, mmentrypoints
-    # disable translation of the whole of classobjinterp.py
-    StdObjSpace.setup_old_style_classes = lambda self: None
-    space = StdObjSpace()
-    # call cache filling code *not* needed here
-
-    # ------------------------------------------------------------
-    mmentrypoints = {}
-    for name in operations:
-        mm = getattr(space.MM, name)
-        exprargs, expr, miniglobals, fallback = (
-            mm.install_not_sliced(space.model.typeorder, baked_perform_call=False))
-        func = stdtypedef.make_perform_trampoline('__mm_'+name,
-                                                  exprargs, expr, miniglobals,
-                                                  mm)
-        mmentrypoints[name] = func
-    # ------------------------------------------------------------
-
-    # further call the entry_point once to trigger building remaining
-    # caches (as far as analyzing the entry_point is concerned)
-    make_flat_code()
-    entry_point()
-
-    return entry_point, []
-
-# _____ Run translated _____
-
-def run(c_entry_point):
-    res = c_entry_point()
-    print res
-    import operator
-    assert res == [getattr(operator, name)(-7, 6) for name in operations]
-
-if __name__ == "__main__":
-    # just run it without translation
-    target()
-    run(entry_point)
-    

goal/targetpypymain.py

-import os, sys
-from pypy.objspace.std.objspace import StdObjSpace
-from pypy.annotation.model import *
-from pypy.annotation.listdef import ListDef
-from pypy.interpreter import gateway
-
-# WARNING: this requires the annotator.
-# There is no easy way to build all caches manually,
-# but the annotator can do it for us for free.
-
-this_dir = os.path.dirname(sys.argv[0])
-
-# __________  Entry point  __________
-
-def entry_point(argv):
-    w_argv = space.newlist([space.wrap(s) for s in argv])
-    w_exitcode = space.call(w_entry_point, w_argv)
-    return space.int_w(w_exitcode)
-
-# _____ Define and setup target ___
-
-def target():
-    global space, w_entry_point
-    # disable translation of the whole of classobjinterp.py
-    StdObjSpace.setup_old_style_classes = lambda self: None
-    # disable geninterp for now -- we have faaar toooo much interp-level code
-    # for the poor translator already
-    gateway.ApplevelClass.use_geninterp = False
-
-    space = StdObjSpace()
-
-    # manually imports app_main.py
-    filename = os.path.join(this_dir, 'app_main.py')
-    w_dict = space.newdict([])
-    space.exec_(open(filename).read(), w_dict, w_dict)
-    w_entry_point = space.getitem(w_dict, space.wrap('entry_point'))
-
-    s_list_of_strings = SomeList(ListDef(None, SomeString()))
-    return entry_point, [s_list_of_strings]
-
-# _____ Run translated _____
-def run(c_entry_point):
-    argv = [os.path.join(this_dir, 'app_example.py')]
-    exitcode = c_entry_point(argv)
-    assert exitcode == 0

goal/targetrpystone.py

-import buildcache2
-from pypy.objspace.std.objspace import StdObjSpace
-from pypy.translator.test import rpystone
-
-# __________  Entry point  __________
-
-LOOPS = 150000
-
-# rpystone.setslow(False)
-
-def entry_point():
-    rpystone.entrypoint(LOOPS)
-    
-# _____ Define and setup target _____
-def target():
-    global space, mmentrypoints
-    space = StdObjSpace()
-
-    # ------------------------------------------------------------
-
-    return entry_point, []
-
-# _____ Run translated _____
-
-def run(c_entry_point):
-    res_w = c_entry_point()
-    print res_w
-    print "CPython:"
-    rpystone.entrypoint(50000)
-
-if __name__ == "__main__":
-    # just run it without translation
-    LOOPS = 50000
-    target()
-    run(entry_point)
-    

goal/targetrpystone2.py

-import buildcache2
-from pypy.objspace.std.objspace import StdObjSpace
-from pypy.translator.test import rpystone
-
-# __________  Entry point  __________
-
-LOOPS = 1000000
-
-rpystone.setslow(False)
-
-def entry_point():
-    rpystone.entrypoint(LOOPS)
-    
-# _____ Define and setup target _____
-def target():
-    global space, mmentrypoints
-    space = StdObjSpace()
-
-    # ------------------------------------------------------------
-
-    return entry_point, []
-
-# _____ Run translated _____
-
-def run(c_entry_point):
-    res_w = c_entry_point()
-    print res_w
-    print "CPython:"
-    rpystone.entrypoint(50000)
-
-if __name__ == "__main__":
-    # just run it without translation
-    LOOPS = 50000
-    target()
-    run(entry_point)
-    

goal/translate_pypy.py

-#
-#  
-#
-"""
-Command-line options for translate_pypy:
-
-   port       Listen on the given port number for connexions
-                  (see pypy/translator/tool/pygame/graphclient.py)
-   targetspec
-              targetspec.py is a python file defining
-              what is the translation target and setting things up for it,
-              it should have a target function returning an entry_point ...;
-              defaults to targetpypy. The .py suffix is optional.
-   -text      Don't start the Pygame viewer
-   -no-a      Don't infer annotations, just translate everything
-   -no-s      Don't simplify the graph after annotation
-   -no-t      Don't type-specialize the graph operations with the C typer
-   -no-c      Don't generate the C code
-   -c         Generate the C code, but don't compile it
-   -o         Generate and compile the C code, but don't run it
-   -no-mark-some-objects
-              Do not mark functions that have SomeObject in their signature.
-   -tcc       Equivalent to the envvar PYPY_CC='tcc -shared -o "%s.so" "%s.c"'
-                  -- http://fabrice.bellard.free.fr/tcc/
-   -no-d      Disable recording of debugging information
-   -huge=%    Threshold in the number of functions after which only a local call
-              graph and not a full one is displayed
-"""
-import autopath, sys, threading, pdb, os
-
-from pypy.translator.translator import Translator
-from pypy.translator.ann_override import pypy_overrides
-from pypy.annotation import model as annmodel
-from pypy.tool.cache import Cache
-from pypy.annotation.model import SomeObject
-from pypy.tool.udir import udir 
-from pypy.tool.ansi_print import ansi_print
-
-
-# XXX this tries to make compiling faster
-from pypy.translator.tool import buildpyxmodule
-buildpyxmodule.enable_fast_compilation()
-
-
-
-
-# __________  Main  __________
-
-def analyse(target):
-    global t, entry_point
-
-    entry_point, inputtypes = target()
-
-    t = Translator(entry_point, verbose=True, simplifying=True)
-    if listen_port:
-        run_async_server()
-    if not options['-no-a']:
-        try:
-            a = t.annotate(inputtypes, overrides=pypy_overrides)
-        finally:
-            worstblocks_topten(t.annotator)
-        if not options['-no-s']:
-            a.simplify()
-        if not options['-no-t']:
-            a.specialize()
-        t.frozen = True   # cannot freeze if we don't have annotations
-        if not options['-no-mark-some-objects']:
-            options['-no-mark-some-objects'] = True # Do not do this again
-            find_someobjects(t)
-
-
-def find_someobjects(translator, quiet=False):
-    """Find all functions in that have SomeObject in their signature."""
-    annotator = translator.annotator
-    if not annotator:
-        return # no annotations available
-
-    translator.highlight_functions = {}
-
-    def is_someobject(var):
-        try:
-            return annotator.binding(var).__class__ == SomeObject
-        except KeyError:
-            return False
-
-    def short_binding(var):
-        try:
-            binding = annotator.binding(var)
-        except KeyError:
-            return "?"
-        if binding.is_constant():
-            return 'const %s' % binding.__class__.__name__
-        else:
-            return binding.__class__.__name__
-
-    header = True
-    items = [(graph.name, func, graph)
-             for func, graph in translator.flowgraphs.items()]
-    items.sort()
-    num = someobjnum = 0
-    for graphname, func, graph in items:
-        unknown_input_args = len(filter(is_someobject, graph.getargs()))
-        unknown_return_value = is_someobject(graph.getreturnvar())
-        if unknown_input_args or unknown_return_value:
-            someobjnum += 1
-            translator.highlight_functions[func] = True
-            if not quiet:
-                if header:
-                    header = False
-                    print "=" * 70
-                    print "Functions that have SomeObject in their signature"
-                    print "=" * 70
-                print ("%(name)s(%(args)s) -> %(result)s\n"
-                       "%(filename)s:%(lineno)s\n"
-                       % {'name': graph.name,
-                          'filename': func.func_globals.get('__name__', '?'),
-                          'lineno': func.func_code.co_firstlineno,
-                          'args': ', '.join(map(short_binding,
-                                                graph.getargs())),
-                          'result': short_binding(graph.getreturnvar())})
-        num += 1
-    if not quiet:
-        print "=" * 70
-        percent = int(num and (100.0*someobjnum / num) or 0)
-        print "someobjectness: %2d percent" % (percent)
-        print "(%d out of %d functions get or return SomeObjects" % (
-            someobjnum, num) 
-        print "=" * 70
-
-
-def update_usession_dir(stabledir = udir.dirpath('usession')): 
-    from py import path 
-    try:
-        if stabledir.check(dir=1): 
-            for x in udir.visit(path.checker(file=1)): 
-                target = stabledir.join(x.relto(udir)) 
-                if target.check():
-                    target.remove()
-                else:
-                    target.dirpath().ensure(dir=1) 
-                try:
-                    target.mklinkto(x) 
-                except path.Invalid: 
-                    x.copy(target) 
-    except path.Invalid: 
-        print "ignored: couldn't link or copy to %s" % stabledir 
-
-def run_in_thread(fn, args, cleanup=None, cleanup_args=()):
-    def _run_in_thread():
-        fn(*args)
-        if cleanup is not None:
-            cleanup(*cleanup_args)
-    return threading.Thread(target=_run_in_thread, args=())
-
-def run_async_server():
-    from pypy.translator.tool import graphpage, graphserver
-    homepage = graphpage.TranslatorPage(t)
-    graphserver.run_server(homepage, port=listen_port, background=True)
-    options['-text'] = True
-
-def worstblocks_topten(ann, n=10):
-    h = [(count, block) for block, count in ann.reflowcounter.iteritems()]
-    h.sort()
-    print
-    ansi_print(',-----------------------  Top %d Most Reflown Blocks  -----------------------.' % n, 36)
-    for i in range(n):
-        if not h:
-            break
-        count, block = h.pop()
-        ansi_print('                                                      #%3d: reflown %d times  |' % (i+1, count), 36)
-        about(block)
-    ansi_print("`----------------------------------------------------------------------------'", 36)
-    print
-
-
-if __name__ == '__main__':
-
-    targetspec = 'targetpypy'
-    huge = 100
-
-    options = {'-text': False,
-               '-no-c': False,
-               '-c':    False,
-               '-o':    False,
-               '-no-mark-some-objects': False,
-               '-no-a': False,
-               '-no-s': False,
-               '-no-t': False,
-               '-tcc':  False,
-               '-no-d': False,
-               }
-    listen_port = None
-    for arg in sys.argv[1:]:
-        if arg in ('-h', '--help'):
-            print __doc__.strip()
-            sys.exit()
-        try:
-            listen_port = int(arg)
-        except ValueError:
-            if os.path.isfile(arg+'.py'):
-                assert not os.path.isfile(arg), (
-                    "ambiguous file naming, please rename %s" % arg)
-                targetspec = arg
-            elif os.path.isfile(arg) and arg.endswith('.py'):
-                targetspec = arg[:-3]
-            elif arg.startswith('-huge='):
-                huge = int(arg[6:])
-            else:                
-                assert arg in options, "unknown option %r" % (arg,)
-                options[arg] = True
-    if options['-tcc']:
-        os.environ['PYPY_CC'] = 'tcc -shared -o "%s.so" "%s.c"'
-    if options['-no-d']:
-        annmodel.DEBUG = False
-
-    def about(x):
-        """ interactive debugging helper """
-        from pypy.objspace.flow.model import Block, flatten
-        if isinstance(x, Block):
-            for func, graph in t.flowgraphs.items():
-                if x in flatten(graph):
-                    funcname = func.func_name
-                    cls = getattr(func, 'class_', None)
-                    if cls:
-                        funcname = '%s.%s' % (cls.__name__, funcname)
-                    print '%s is a %s in the graph of %s' % (x,
-                                x.__class__.__name__, funcname)
-                    print 'at %s:%d' % (func.func_globals.get('__name__', '?'),
-                                        func.func_code.co_firstlineno)
-                    break
-            else:
-                print '%s is a %s at some unknown location' % (x,
-                                x.__class__.__name__)
-            print 'containing the following operations:'
-            for op in x.operations:
-                print op
-            print '--end--'
-            return
-        print "don't know about", x
-
-    def run_server():
-        from pypy.translator.tool import graphpage
-        from pypy.translator.tool.pygame.graphclient import get_layout
-        from pypy.translator.tool.pygame.graphdisplay import GraphDisplay
-        import pygame
-
-        if not options['-no-mark-some-objects']:
-            find_someobjects(t, quiet=True)
-
-        if len(t.functions) <= huge:
-            page = graphpage.TranslatorPage(t)
-        else:
-            page = graphpage.LocalizedCallGraphPage(t, entry_point)
-
-        display = GraphDisplay(get_layout(page))
-        async_quit = display.async_quit
-        def show(page):
-            display.async_cmd(layout=get_layout(page))
-        return display.run, show, async_quit, pygame.quit
-
-    class PdbPlusShow(pdb.Pdb):
-
-        def post_mortem(self, t):
-            self.reset()
-            while t.tb_next is not None:
-                t = t.tb_next
-            self.interaction(t.tb_frame, t)        
-
-        show = None
-
-        def _show(self, page):
-            if not self.show:
-                print "*** No display"
-                return
-            self.show(page)
-
-        def _importobj(self, fullname):
-            obj = None
-            name = ''
-            for comp in fullname.split('.'):
-                name += comp
-                obj = getattr(obj, comp, None)
-                if obj is None:
-                    try:
-                        obj = __import__(name, {}, {}, ['*'])
-                    except ImportError:
-                        raise NameError
-                name += '.'
-            return obj
-
-        TRYPREFIXES = ['','pypy.','pypy.objspace.','pypy.interpreter.', 'pypy.objspace.std.' ]
-
-        def _getobj(self, name):
-            if '.' in name:
-                for pfx in self.TRYPREFIXES:
-                    try:
-                        return self._importobj(pfx+name)
-                    except NameError:
-                        pass
-            try:
-                return self._getval(name)
-            except (NameError, AttributeError, LookupError):
-                print "*** Not found:", name
-            return None
-
-        def do_showg(self, arg):
-            """showg obj
-show graph for obj, obj can be an expression or a dotted name
-(in which case prefixing with some packages in pypy is tried (see help pypyprefixes)).
-if obj is a function or method, the localized call graph is shown;
-if obj is a class or ClassDef the class definition graph is shown"""            
-            from pypy.annotation.classdef import ClassDef
-            from pypy.translator.tool import graphpage            
-            obj = self._getobj(arg)
-            if obj is None:
-                return
-            if hasattr(obj, 'im_func'):
-                obj = obj.im_func
-            if obj in t.flowgraphs:
-                page = graphpage.LocalizedCallGraphPage(t, obj)
-            elif obj in getattr(t.annotator, 'getuserclasses', lambda: {})():
-                page = graphpage.ClassDefPage(t, t.annotator.getuserclasses()[obj])
-            elif isinstance(obj, ClassDef):
-                page = graphpage.ClassDefPage(t, obj)
-            else:
-                print "*** Nothing to do"
-                return
-            self._show(page)
-
-        def do_flowg(self, arg):
-            """callg obj
-show flow graph for function obj, obj can be an expression or a dotted name
-(in which case prefixing with some packages in pypy is tried (see help pypyprefixes))"""            
-            import types
-            from pypy.translator.tool import graphpage                        
-            obj = self._getobj(arg)
-            if obj is None:
-                return
-            if hasattr(obj, 'im_func'):
-                obj = obj.im_func
-            if not isinstance(obj, types.FunctionType):
-                print "*** Not a function"
-                return
-            self._show(graphpage.FlowGraphPage(t, [obj]))
-
-        def do_callg(self, arg):
-            """callg obj
-show localized call-graph for function obj, obj can be an expression or a dotted name
-(in which case prefixing with some packages in pypy is tried (see help pypyprefixes))"""
-            import types
-            from pypy.translator.tool import graphpage                        
-            obj = self._getobj(arg)
-            if obj is None:
-                return
-            if hasattr(obj, 'im_func'):
-                obj = obj.im_func
-            if not isinstance(obj, types.FunctionType):
-                print "*** Not a function"
-                return
-            self._show(graphpage.LocalizedCallGraphPage(t, obj))
-
-        def do_classhier(self, arg):
-            """classhier
-show class hierarchy graph"""
-            from pypy.translator.tool import graphpage           
-            self._show(graphpage.ClassHierarchyPage(t))
-
-        def help_graphs(self):
-            print "graph commands are: showg, flowg, callg, classhier"
-
-        def help_pypyprefixes(self):
-            print "these prefixes are tried for dotted names in graph commands:"
-            print self.TRYPREFIXES
-
-    def debug(got_error):
-        pdb_plus_show = PdbPlusShow()
-        
-        if got_error:
-            import traceback
-            exc, val, tb = sys.exc_info()
-            print >> sys.stderr
-            traceback.print_exception(exc, val, tb)
-            print >> sys.stderr
-
-            block = getattr(val, '__annotator_block', None)
-            if block:
-                print '-'*60
-                about(block)
-                print '-'*60
-
-            print >> sys.stderr
-            func, args = pdb_plus_show.post_mortem, (tb,)
-        else:
-            print '-'*60
-            print 'Done.'
-            print
-            func, args = pdb_plus_show.set_trace, ()
-        if options['-text']:
-            func(*args)
-        else:
-            start, show, stop, cleanup = run_server()
-            pdb_plus_show.show = show
-            debugger = run_in_thread(func, args, stop)
-            debugger.start()
-            start()
-            debugger.join()
-            cleanup()
-
-    try:
-        targetspec_dic = {}
-        sys.path.insert(0, os.path.dirname(targetspec))
-        execfile(targetspec+'.py',targetspec_dic)
-        print "Analysing target as defined by %s" % targetspec
-        analyse(targetspec_dic['target'])
-        print '-'*60
-        if options['-no-c']:
-            print 'Not generating C code.'
-        elif options['-c']:
-            print 'Generating C code without compiling it...'
-            filename = t.ccompile(really_compile=False)
-            update_usession_dir()
-            print 'Written %s.' % (filename,)
-        else:
-            print 'Generating and compiling C code...'
-            c_entry_point = t.ccompile()
-            update_usession_dir()
-            if not options['-o']:
-                print 'Running!'
-                targetspec_dic['run'](c_entry_point)
-    except:
-        debug(True)
-    else:
-        debug(False)
-    

pypy/bin/autopath.py

+"""
+self cloning, automatic path configuration 
+
+copy this into any subdirectory of pypy from which scripts need 
+to be run, typically all of the test subdirs. 
+The idea is that any such script simply issues
+
+    import autopath
+
+and this will make sure that the parent directory containing "pypy"
+is in sys.path. 
+
+If you modify the master "autopath.py" version (in pypy/tool/autopath.py) 
+you can directly run it which will copy itself on all autopath.py files
+it finds under the pypy root directory. 
+
+This module always provides these attributes:
+
+    pypydir    pypy root directory path 
+    this_dir   directory where this autopath.py resides 
+
+"""
+
+
+def __dirinfo(part):
+    """ return (partdir, this_dir) and insert parent of partdir
+    into sys.path.  If the parent directories don't have the part
+    an EnvironmentError is raised."""
+
+    import sys, os
+    try:
+        head = this_dir = os.path.realpath(os.path.dirname(__file__))
+    except NameError:
+        head = this_dir = os.path.realpath(os.path.dirname(sys.argv[0]))
+
+    while head:
+        partdir = head
+        head, tail = os.path.split(head)
+        if tail == part:
+            break
+    else:
+        raise EnvironmentError, "'%s' missing in '%r'" % (partdir, this_dir)
+    
+    checkpaths = sys.path[:]
+    pypy_root = os.path.join(head, '')
+    
+    while checkpaths:
+        orig = checkpaths.pop()
+        fullorig = os.path.join(os.path.realpath(orig), '')
+        if fullorig.startswith(pypy_root):
+            if os.path.exists(os.path.join(fullorig, '__init__.py')):
+                sys.path.remove(orig)
+    if head not in sys.path:
+        sys.path.insert(0, head)
+
+    munged = {}
+    for name, mod in sys.modules.items():
+        fn = getattr(mod, '__file__', None)
+        if '.' in name or not isinstance(fn, str):
+            continue
+        newname = os.path.splitext(os.path.basename(fn))[0]
+        if not newname.startswith(part + '.'):
+            continue
+        path = os.path.join(os.path.dirname(os.path.realpath(fn)), '')
+        if path.startswith(pypy_root) and newname != part:
+            modpaths = os.path.normpath(path[len(pypy_root):]).split(os.sep)
+            if newname != '__init__':
+                modpaths.append(newname)
+            modpath = '.'.join(modpaths)
+            if modpath not in sys.modules:
+                munged[modpath] = mod
+
+    for name, mod in munged.iteritems():
+        if name not in sys.modules:
+            sys.modules[name] = mod
+        if '.' in name:
+            prename = name[:name.rfind('.')]
+            postname = name[len(prename)+1:]
+            if prename not in sys.modules:
+                __import__(prename)
+                if not hasattr(sys.modules[prename], postname):
+                    setattr(sys.modules[prename], postname, mod)
+
+    return partdir, this_dir
+
+def __clone():
+    """ clone master version of autopath.py into all subdirs """
+    from os.path import join, walk
+    if not this_dir.endswith(join('pypy','tool')):
+        raise EnvironmentError("can only clone master version "
+                               "'%s'" % join(pypydir, 'tool',_myname))
+
+
+    def sync_walker(arg, dirname, fnames):
+        if _myname in fnames:
+            fn = join(dirname, _myname)
+            f = open(fn, 'rwb+')
+            try:
+                if f.read() == arg:
+                    print "checkok", fn
+                else:
+                    print "syncing", fn
+                    f = open(fn, 'w')
+                    f.write(arg)
+            finally:
+                f.close()
+    s = open(join(pypydir, 'tool', _myname), 'rb').read()
+    walk(pypydir, sync_walker, s)
+
+_myname = 'autopath.py'
+
+# set guaranteed attributes
+
+pypydir, this_dir = __dirinfo('pypy')
+
+if __name__ == '__main__':
+    __clone()
+#!/usr/bin/env python 
+
+"""Main entry point into the PyPy interpreter.  For a list of options, type
+
+      py.py --help
+
+"""
+
+try:
+    import autopath
+except ImportError:
+    pass
+
+from pypy.tool.getpy import py
+#py.magic.invoke(compile=1)
+
+from pypy.tool import option
+from pypy.tool.optik import make_option
+from pypy.interpreter import main, interactive, error
+import os, sys
+import time
+
+class Options(option.Options):
+    verbose = os.getenv('PYPY_TB')
+    interactive = 0
+    command = []
+    completer = False
+
+def get_main_options():
+    options = option.get_standard_options()
+
+    options.append(make_option(
+        '-v', action='store_true', dest='verbose',
+        help='show verbose interpreter-level traceback'))
+
+    options.append(make_option(
+        '-C', action='store_true', dest='completer',
+        help='use readline commandline completer'))
+
+    options.append(make_option(
+        '-i', action="store_true", dest="interactive",
+        help="inspect interactively after running script"))
+
+    options.append(make_option(
+        '-O', action="store_true", dest="optimize",
+        help="dummy optimization flag for compatibility with C Python"))
+
+    def command_callback(option, opt, value, parser):
+        parser.values.command = parser.rargs[:]
+        parser.rargs[:] = []
+        
+    options.append(make_option(
+        '-c', action="callback",
+        callback=command_callback,
+        help="program passed in as CMD (terminates option list)"))
+        
+    return options
+
+def main_(argv=None):
+    starttime = time.time() 
+    args = option.process_options(get_main_options(), Options, argv[1:])
+    if Options.verbose:
+        error.RECORD_INTERPLEVEL_TRACEBACK = True
+
+    # create the object space
+    space = option.objspace()
+    space._starttime = starttime
+    assert 'pypy.tool.udir' not in sys.modules, (
+        "running py.py should not import pypy.tool.udir, which is\n"
+        "only for testing or translating purposes.")
+    space.setitem(space.sys.w_dict,space.wrap('executable'),space.wrap(argv[0]))
+
+    # store the command-line arguments into sys.argv
+    go_interactive = Options.interactive
+    banner = ''
+    exit_status = 0
+    if Options.command:
+        args = ['-c'] + Options.command[1:]
+    for arg in args:
+        space.call_method(space.sys.get('argv'), 'append', space.wrap(arg))
+
+    # load the source of the program given as command-line argument
+    if Options.command:
+        def doit():
+            main.run_string(Options.command[0], space=space)
+    elif args:
+        def doit():
+            main.run_file(args[0], space=space)
+    else:
+        def doit():
+            pass
+        space.call_method(space.sys.get('argv'), 'append', space.wrap(''))
+        go_interactive = 1
+        banner = None
+
+    try:
+        # compile and run it
+        if not main.run_toplevel(space, doit, verbose=Options.verbose):
+            exit_status = 1
+
+        # start the interactive console
+        if go_interactive:
+            con = interactive.PyPyConsole(space, verbose=Options.verbose,
+                                                 completer=Options.completer)
+            if banner == '':
+                banner = '%s / %s'%(con.__class__.__name__,
+                                    repr(space))
+            con.interact(banner)
+            exit_status = 0
+    finally:
+        # call the sys.exitfunc()
+        w_exitfunc = space.sys.getdictvalue(space, 'exitfunc')
+        if w_exitfunc is not None:
+            def doit():
+                space.call_function(w_exitfunc)
+            main.run_toplevel(space, doit, verbose=Options.verbose)
+
+    return exit_status
+
+##def main_(argv=None):
+##    starttime = time.time() 
+##    from pypy.tool import tb_server
+##    args = option.process_options(get_main_options(), Options, argv[1:])
+##    space = None
+##    exit_status = 1   # until proven otherwise
+##                      # XXX should review what CPython's policy is for
+##                      # the exit status code
+##    try:
+##        space = option.objspace()
+##        space._starttime = starttime
+##        assert 'pypy.tool.udir' not in sys.modules, (
+##            "running py.py should not import pypy.tool.udir, which is\n"
+##            "only for testing or translating purposes.")
+##        go_interactive = Options.interactive
+##        if Options.verbose:
+##            error.RECORD_INTERPLEVEL_TRACEBACK = True
+##        banner = ''
+##        space.setitem(space.sys.w_dict,space.wrap('executable'),space.wrap(argv[0]))
+##        if Options.command:
+##            args = ['-c'] + Options.command[1:]
+##        for arg in args:
+##            space.call_method(space.sys.get('argv'), 'append', space.wrap(arg))
+##        try:
+##            if Options.command:
+##                main.run_string(Options.command[0], '<string>', space)
+##            elif args:
+##                main.run_file(args[0], space)
+##            else:
+##                space.call_method(space.sys.get('argv'), 'append', space.wrap(''))
+##                go_interactive = 1
+##                banner = None
+##            exit_status = 0
+##        except error.OperationError, operationerr:
+##            if Options.verbose:
+##                operationerr.print_detailed_traceback(space)
+##            else:
+##                operationerr.print_application_traceback(space)
+##        if go_interactive:
+##            con = interactive.PyPyConsole(space, verbose=Options.verbose, completer=Options.completer)
+##            if banner == '':
+##                banner = '%s / %s'%(con.__class__.__name__,
+##                                    repr(space))
+##            con.interact(banner)
+##    except:
+##        exc_type, value, tb = sys.exc_info()
+##        sys.last_type = exc_type
+##        sys.last_value = value
+##        sys.last_traceback = tb
+##        if issubclass(exc_type, SystemExit):
+##            pass   # don't print tracebacks for SystemExit
+##        elif isinstance(value, error.OperationError):
+##            value.print_detailed_traceback(space=space)
+##        else:
+##            sys.excepthook(exc_type, value, tb)
+##        tb_server.wait_until_interrupt()
+##        exit_status = 1
+            
+##    tb_server.stop()
+##    return exit_status
+
+if __name__ == '__main__':
+    try:
+        import readline
+    except:
+        pass
+    if hasattr(sys, 'setrecursionlimit'):
+        # for running "python -i py.py -Si -- py.py -Si" 
+        sys.setrecursionlimit(3000)
+    sys.exit(main_(sys.argv))

pypy/bin/translator.py

+#!/usr/bin/env python 
+
+"""PyPy Translator Frontend
+
+Glue script putting together the various pieces of the translator.
+Can be used for interactive testing of the translator.
+
+Example:
+
+    t = Translator(func)
+    t.view()                           # control flow graph
+
+    print t.source()                   # original source
+    print t.c()                        # C translation
+    print t.cl()                       # common lisp translation
+    print t.llvm()                     # LLVM translation
+
+    t.simplify()                       # flow graph simplification
+    a = t.annotate([int])              # pass the list of args types
+    a.simplify()                       # simplification by annotator
+    t.view()                           # graph + annotations under the mouse
+
+    t.call(arg)                        # call original function
+    t.dis()                            # bytecode disassemble
+
+    f = t.ccompile()                   # C compilation
+    f = t.llvmcompile()                # LLVM compilation
+    assert f(arg) == t.call(arg)       # sanity check
+
+Some functions are provided for the benefit of interactive testing.
+Try dir(test) for list of current snippets.
+"""
+
+import autopath, os, sys
+from pypy.translator.translator import Translator
+
+
+if __name__ == '__main__':
+    from pypy.translator.test import snippet as test
+    if (os.getcwd() not in sys.path and
+        os.path.curdir not in sys.path):
+        sys.path.insert(0, os.getcwd())
+    print __doc__
+
+    # 2.3 specific -- sanxiyn
+    import os
+    os.putenv("PYTHONINSPECT", "1")

pypy/documentation/objspace.txt

 
 Example usage::
 
-    $ python interpreter/py.py -o thunk
+    $ py.py -o thunk
     >>>> def f():
     ...     print 'computing...'
     ...     return 6*7

pypy/interpreter/py.py

-#!/usr/bin/env python 
-
-try:
-    import autopath
-except ImportError:
-    pass
-
-from pypy.tool.getpy import py
-#py.magic.invoke(compile=1)
-
-from pypy.tool import option
-from pypy.tool.optik import make_option
-from pypy.interpreter import main, interactive, error
-import os, sys
-import time
-
-class Options(option.Options):
-    verbose = os.getenv('PYPY_TB')
-    interactive = 0
-    command = []
-    completer = False
-
-def get_main_options():
-    options = option.get_standard_options()
-
-    options.append(make_option(
-        '-v', action='store_true', dest='verbose',
-        help='show verbose interpreter-level traceback'))
-
-    options.append(make_option(
-        '-C', action='store_true', dest='completer',
-        help='use readline commandline completer'))
-
-    options.append(make_option(
-        '-i', action="store_true", dest="interactive",
-        help="inspect interactively after running script"))
-
-    options.append(make_option(
-        '-O', action="store_true", dest="optimize",
-        help="dummy optimization flag for compatibility with C Python"))
-
-    def command_callback(option, opt, value, parser):
-        parser.values.command = parser.rargs[:]
-        parser.rargs[:] = []
-        
-    options.append(make_option(
-        '-c', action="callback",
-        callback=command_callback,
-        help="program passed in as CMD (terminates option list)"))
-        
-    return options
-
-def main_(argv=None):
-    starttime = time.time() 
-    args = option.process_options(get_main_options(), Options, argv[1:])
-    if Options.verbose:
-        error.RECORD_INTERPLEVEL_TRACEBACK = True
-
-    # create the object space
-    space = option.objspace()
-    space._starttime = starttime
-    assert 'pypy.tool.udir' not in sys.modules, (
-        "running py.py should not import pypy.tool.udir, which is\n"
-        "only for testing or translating purposes.")
-    space.setitem(space.sys.w_dict,space.wrap('executable'),space.wrap(argv[0]))
-
-    # store the command-line arguments into sys.argv
-    go_interactive = Options.interactive
-    banner = ''
-    exit_status = 0
-    if Options.command:
-        args = ['-c'] + Options.command[1:]
-    for arg in args:
-        space.call_method(space.sys.get('argv'), 'append', space.wrap(arg))
-
-    # load the source of the program given as command-line argument
-    if Options.command:
-        def doit():
-            main.run_string(Options.command[0], space=space)
-    elif args:
-        def doit():
-            main.run_file(args[0], space=space)
-    else:
-        def doit():
-            pass
-        space.call_method(space.sys.get('argv'), 'append', space.wrap(''))
-        go_interactive = 1
-        banner = None
-
-    try:
-        # compile and run it
-        if not main.run_toplevel(space, doit, verbose=Options.verbose):
-            exit_status = 1
-
-        # start the interactive console
-        if go_interactive:
-            con = interactive.PyPyConsole(space, verbose=Options.verbose,
-                                                 completer=Options.completer)
-            if banner == '':
-                banner = '%s / %s'%(con.__class__.__name__,
-                                    repr(space))
-            con.interact(banner)
-            exit_status = 0
-    finally:
-        # call the sys.exitfunc()
-        w_exitfunc = space.sys.getdictvalue(space, 'exitfunc')
-        if w_exitfunc is not None:
-            def doit():
-                space.call_function(w_exitfunc)
-            main.run_toplevel(space, doit, verbose=Options.verbose)
-
-    return exit_status
-
-##def main_(argv=None):
-##    starttime = time.time() 
-##    from pypy.tool import tb_server
-##    args = option.process_options(get_main_options(), Options, argv[1:])
-##    space = None
-##    exit_status = 1   # until proven otherwise
-##                      # XXX should review what CPython's policy is for
-##                      # the exit status code
-##    try:
-##        space = option.objspace()
-##        space._starttime = starttime
-##        assert 'pypy.tool.udir' not in sys.modules, (
-##            "running py.py should not import pypy.tool.udir, which is\n"
-##            "only for testing or translating purposes.")
-##        go_interactive = Options.interactive
-##        if Options.verbose:
-##            error.RECORD_INTERPLEVEL_TRACEBACK = True
-##        banner = ''
-##        space.setitem(space.sys.w_dict,space.wrap('executable'),space.wrap(argv[0]))
-##        if Options.command:
-##            args = ['-c'] + Options.command[1:]
-##        for arg in args:
-##            space.call_method(space.sys.get('argv'), 'append', space.wrap(arg))
-##        try:
-##            if Options.command:
-##                main.run_string(Options.command[0], '<string>', space)
-##            elif args:
-##                main.run_file(args[0], space)
-##            else:
-##                space.call_method(space.sys.get('argv'), 'append', space.wrap(''))
-##                go_interactive = 1
-##                banner = None
-##            exit_status = 0
-##        except error.OperationError, operationerr:
-##            if Options.verbose:
-##                operationerr.print_detailed_traceback(space)