[pypy-svn] rev 1456 - in pypy/trunk/src/pypy/objspace/flow: . test
jriehl at codespeak.net
jriehl at codespeak.net
Mon Sep 29 20:47:26 CEST 2003
Author: jriehl
Date: Mon Sep 29 20:47:25 2003
New Revision: 1456
Added:
pypy/trunk/src/pypy/objspace/flow/__init__.py (contents, props changed)
pypy/trunk/src/pypy/objspace/flow/wrapper.py (contents, props changed)
Modified:
pypy/trunk/src/pypy/objspace/flow/ (props changed)
pypy/trunk/src/pypy/objspace/flow/objspace.py (contents, props changed)
pypy/trunk/src/pypy/objspace/flow/test/ (props changed)
pypy/trunk/src/pypy/objspace/flow/test/__init__.py (props changed)
pypy/trunk/src/pypy/objspace/flow/test/autopath.py (contents, props changed)
pypy/trunk/src/pypy/objspace/flow/test/test_objspace.py (contents, props changed)
Log:
End of the day checkin for first day of sprint
Added: pypy/trunk/src/pypy/objspace/flow/__init__.py
==============================================================================
--- (empty file)
+++ pypy/trunk/src/pypy/objspace/flow/__init__.py Mon Sep 29 20:47:25 2003
@@ -0,0 +1,2 @@
+from objspace import FlowObjSpace
+Space = FlowObjSpace
Modified: pypy/trunk/src/pypy/objspace/flow/objspace.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/flow/objspace.py (original)
+++ pypy/trunk/src/pypy/objspace/flow/objspace.py Mon Sep 29 20:47:25 2003
@@ -0,0 +1,100 @@
+import operator
+import pypy
+from pypy.interpreter.baseobjspace import ObjSpace
+from pypy.interpreter.pycode import PyCode
+from pypy.objspace.flow.wrapper import *
+from pypy.translator.controlflow import *
+
+class FlowObjSpace(ObjSpace):
+ def initialize(self):
+ self.w_builtins = W_Variable()
+ #self.make_builtins()
+ #self.make_sys()
+
+ def newdict(self, items_w):
+ # XXX Issue a delayed command to create a dictionary
+ return W_Variable()
+
+ def newtuple(self, args_w):
+ # XXX Issue a delayed command to create a tuple and assign to a new W_Variable
+ return W_Variable()
+
+ def getattr(self, w_obj, w_key):
+ # XXX Issue a delayed command
+ return W_Variable()
+
+ def wrap(self, obj):
+ if isinstance(obj, W_Object):
+ raise TypeError("already wrapped: " + repr(obj))
+ return W_Constant(obj)
+
+ def unwrap(self, w_obj):
+ if isinstance(w_obj, W_Object):
+ return w_obj.unwrap()
+ else:
+ raise TypeError("not wrapped: " + repr(w_obj))
+
+ def build_flow(self, func, w_args, w_kwds):
+ """
+ """
+ code = func.func_code
+ bytecode = PyCode()
+ bytecode._from_code(code)
+ w_globals = self.wrap(func.func_globals)
+ frame = bytecode.create_frame(self, w_globals)
+ arg_list = [W_Variable() for w in frame.fastlocals_w]
+ frame.setfastscope(arg_list)
+ self._crnt_ops = []
+ self._crnt_block = BasicBlock(arg_list, [], self._crnt_ops, None)
+ self._graph = FunctionGraph(self._crnt_block, code.co_name)
+ frames = [frame]
+ while len(frames) > 0:
+ crnt_frame = frames.pop()
+ ret_val = crnt_frame.run()
+ self._crnt_block.branch = EndBranch(ret_val)
+ g = self._graph
+ del self._graph
+ del self._crnt_block
+ del self._crnt_ops
+ return g
+
+# ______________________________________________________________________
+
+def make_op(name, symbol, arity, specialnames):
+ if hasattr(FlowObjSpace, name):
+ return # Shouldn't do it
+
+ op = getattr(operator, name, None)
+ if not op:
+ return # Can't do it
+
+ def generic_operator(self, *args_w):
+ assert len(args_w) == arity, name+" got the wrong number of arguments"
+ args = []
+ for w_arg in args_w:
+ try:
+ arg = self.unwrap(w_arg)
+ except UnwrapException:
+ break
+ else:
+ args.append(arg)
+ else:
+ # All arguments are constants: call the operator now
+ try:
+ result = op(*args)
+ except:
+ self.reraise()
+ else:
+ return self.wrap(result)
+
+ w_result = W_Variable()
+ self._crnt_ops.append(SpaceOperation(name, args_w, w_result))
+ return w_result
+
+ setattr(FlowObjSpace, name, generic_operator)
+
+for line in ObjSpace.MethodTable:
+ make_op(*line)
+
+# ______________________________________________________________________
+# End of objspace.py
Modified: pypy/trunk/src/pypy/objspace/flow/test/autopath.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/flow/test/autopath.py (original)
+++ pypy/trunk/src/pypy/objspace/flow/test/autopath.py Mon Sep 29 20:47:25 2003
@@ -1,78 +1,78 @@
-"""
-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 dont have the part
- an EnvironmentError is raised."""
-
- import sys, os
- try:
- head = this_dir = os.path.abspath(os.path.dirname(__file__))
- except NameError:
- head = this_dir = os.path.abspath(os.path.dirname(sys.argv[0]))
-
- while head:
- partdir = head
- head, tail = os.path.split(head)
- if tail == part:
- sys.path = [p for p in sys.path if not p.startswith(head)]
- if head not in sys.path:
- sys.path.insert(0, head)
- return partdir, this_dir
-
- raise EnvironmentError, "'%s' missing in '%r'" % (pathpart,this_path)
-
-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()
+"""
+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 dont have the part
+ an EnvironmentError is raised."""
+
+ import sys, os
+ try:
+ head = this_dir = os.path.abspath(os.path.dirname(__file__))
+ except NameError:
+ head = this_dir = os.path.abspath(os.path.dirname(sys.argv[0]))
+
+ while head:
+ partdir = head
+ head, tail = os.path.split(head)
+ if tail == part:
+ sys.path = [p for p in sys.path if not p.startswith(head)]
+ if head not in sys.path:
+ sys.path.insert(0, head)
+ return partdir, this_dir
+
+ raise EnvironmentError, "'%s' missing in '%r'" % (pathpart,this_path)
+
+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()
Modified: pypy/trunk/src/pypy/objspace/flow/test/test_objspace.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/flow/test/test_objspace.py (original)
+++ pypy/trunk/src/pypy/objspace/flow/test/test_objspace.py Mon Sep 29 20:47:25 2003
@@ -1,32 +1,35 @@
import autopath
from pypy.tool import test
+from pypy.objspace.flow.wrapper import *
+from pypy.translator.controlflow import *
+
class TestFlowOjSpace(test.TestCase):
def setUp(self):
self.space = test.objspace('flow')
- def codetest(sefl, source, functionname, args_w):
+ def codetest(self, source, functionname, args_w):
glob = {}
exec source in glob
func = glob[functionname]
w_args = self.space.newtuple(args_w)
- w_func = self.space.wrap(func)
w_kwds = self.space.newdict([])
- return self.space.build_flow(w_func, w_args, w_kwds)
+ return self.space.build_flow(func, w_args, w_kwds)
def test_nothing(self):
x = self.codetest("def f():\n"
- " pass\n")
- self.assertEquals(len(x), 1)
+ " pass\n",
+ 'f', [])
+ self.assertEquals(x.functionname, 'f')
+ self.assertEquals(x.startblock.branch.__class__, EndBranch)
def test_ifthenelse(self):
x = self.codetest("def f(i, j):\n"
" if i < 0:\n"
" i = j\n"
" return g(i) + 1\n",
- 'f', [W_Anything()])
- self.assertEquals(x.graph_string(),
- "")
+ 'f', [W_Variable()])
+
if __name__ == '__main__':
test.main()
Added: pypy/trunk/src/pypy/objspace/flow/wrapper.py
==============================================================================
--- (empty file)
+++ pypy/trunk/src/pypy/objspace/flow/wrapper.py Mon Sep 29 20:47:25 2003
@@ -0,0 +1,76 @@
+# ______________________________________________________________________
+"""Wrapper objects for the control flow analysis object space."""
+# ______________________________________________________________________
+
+# This is kinda a hack, but at the same time, I don't see why this was defined
+# in the object space module in the annotation object space.
+
+class UnwrapException(Exception):
+ pass
+
+# ______________________________________________________________________
+
+class W_Object(object):
+ """Abstract base class. do not instantiate."""
+
+ force = None # See cloningcontext.py
+
+ def __new__(cls, *args, **kwd):
+ assert cls is not W_Object
+ return object.__new__(cls)
+
+ def __init__(self):
+ pass
+
+ def __repr__(self):
+ s = self.argsrepr()
+ if len(s) > 100:
+ s = s[:25] + "..." + s[-25:]
+ return "%s(%s)" % (self.__class__.__name__, s)
+
+ def argsrepr(self):
+ return ""
+
+ def unwrap(self):
+ # XXX Somehow importing this at module level doesn't work
+ raise UnwrapException(self)
+
+ def __eq__(self, other):
+ return type(other) is type(self)
+
+ def __ne__(self, other):
+ return not self.__eq__(other)
+
+# ______________________________________________________________________
+
+class W_Variable(W_Object):
+ pass
+
+# ______________________________________________________________________
+
+class W_Constant(W_Object):
+ """A specific constant value."""
+
+ def __init__(self, value):
+ self.value = value
+
+ def argsrepr(self):
+ return repr(self.value)
+
+ def unwrap(self):
+ return self.value
+
+ def __eq__(self, other):
+ return type(other) is type(self) and self.value == other.value
+
+ def __len__(self):
+ return len(self.value)
+
+ def __getitem__(self, key):
+ return self.value[key]
+
+ def __setitem__(self, key, value):
+ self.value[key] = value
+
+# ______________________________________________________________________
+# End of wrapper.py
More information about the Pypy-commit
mailing list