[pypy-svn] rev 1220 - in pypy/branch/builtinrefactor/pypy: appspace appspace/test interpreter interpreter/test module objspace objspace/ann objspace/std tool

hpk at codespeak.net hpk at codespeak.net
Sun Jul 27 20:53:10 CEST 2003


Author: hpk
Date: Sun Jul 27 20:53:08 2003
New Revision: 1220

Added:
   pypy/branch/builtinrefactor/pypy/interpreter/gateway.py
   pypy/branch/builtinrefactor/pypy/interpreter/test/test_exceptcomp.py   (props changed)
      - copied unchanged from rev 1120, pypy/branch/builtinrefactor/pypy/appspace/test/test_exceptcomp.py
   pypy/branch/builtinrefactor/pypy/interpreter/test/test_exec.py   (props changed)
      - copied unchanged from rev 1120, pypy/branch/builtinrefactor/pypy/appspace/test/test_exec.py
   pypy/branch/builtinrefactor/pypy/interpreter/test/test_function.py
Removed:
   pypy/branch/builtinrefactor/pypy/appspace/test/test_exceptcomp.py
   pypy/branch/builtinrefactor/pypy/appspace/test/test_exec.py
   pypy/branch/builtinrefactor/pypy/interpreter/extmodule.py
   pypy/branch/builtinrefactor/pypy/interpreter/test/test_extmodule.py
   pypy/branch/builtinrefactor/pypy/interpreter/test/test_pyframe_app.py
   pypy/branch/builtinrefactor/pypy/module/builtin_app.py
   pypy/branch/builtinrefactor/pypy/module/builtin_helper_app.py
Modified:
   pypy/branch/builtinrefactor/pypy/appspace/test/test_cmathmodule.py
   pypy/branch/builtinrefactor/pypy/appspace/test/test_complexobject.py
   pypy/branch/builtinrefactor/pypy/appspace/types.py
   pypy/branch/builtinrefactor/pypy/interpreter/appfile.py
   pypy/branch/builtinrefactor/pypy/interpreter/baseobjspace.py
   pypy/branch/builtinrefactor/pypy/interpreter/executioncontext.py
   pypy/branch/builtinrefactor/pypy/interpreter/interactive.py
   pypy/branch/builtinrefactor/pypy/interpreter/main.py
   pypy/branch/builtinrefactor/pypy/interpreter/opcode.py
   pypy/branch/builtinrefactor/pypy/interpreter/opcode_app.py
   pypy/branch/builtinrefactor/pypy/interpreter/pyframe.py
   pypy/branch/builtinrefactor/pypy/interpreter/test/test_executioncontext.py
   pypy/branch/builtinrefactor/pypy/interpreter/test/test_interpreter.py
   pypy/branch/builtinrefactor/pypy/interpreter/test/test_objspace.py
   pypy/branch/builtinrefactor/pypy/interpreter/unittest_w.py
   pypy/branch/builtinrefactor/pypy/module/builtin.py
   pypy/branch/builtinrefactor/pypy/module/sysmodule.py
   pypy/branch/builtinrefactor/pypy/objspace/ann/cloningcontext.py
   pypy/branch/builtinrefactor/pypy/objspace/ann/objspace.py
   pypy/branch/builtinrefactor/pypy/objspace/ann/wrapper.py
   pypy/branch/builtinrefactor/pypy/objspace/std/funcobject.py
   pypy/branch/builtinrefactor/pypy/objspace/trivial.py
   pypy/branch/builtinrefactor/pypy/tool/test.py
Log:
- gigantic intermediary checkpoint-checkin
  this checkin is mainly for internal 'checkpointing' reasons
  please don't look at it too closely until you want to participate
  in the refactorings. it's definitely neccessary to cleanup
  the refactored code although it's beginning to work ...

- trivial object space works with a more unified approach 
  to functions, code and wrapping app-level stuff into 
  interpreter-level code. StdObjSpace fails because multimethods
  deeply depend on interpreter/pycode.py 
  depends deep

- moved some tests from appspace/test to interpreter/test where
  they belonged (test_exec 

  stdspace and annspace fail miserably (don't even try them)
 
- got rid of 'extmodule' and builtin_*app* stuff

- revamped PyFrame initialization (now a 'ScopedCode' invokes
  either 'setdictscope' or 'setfastscope' to set the local 
  scope of the frame.  the frame doesn't invoke any analysis
  itself anymore. 

- introduced a 'ScopedCode' class which takes a space, 
  a cpython code object, global and closure scope. Currently you call
  'eval_frame(w_locals)' to trigger interpretation of the interp-level defined
  function.  

- introduced an 'InterpretedFunction' which takes a space, 
  a function object, global and closure scope. Currently you call
  'eval_frame(w_args, w_kwargs)' to trigger interpretation of the 
  interp-level defined function. The arguments are parsed with
  another app-level function 'parse_args_complex' which is a
  rewrite (50% code) of 'decode_code_arguments' 

- introduced an 'AppVisibleModule' class that allows to easily
  define builtin modules (which can access interp-level stuff). 
  See the doc-string for more info.

- all the previous new classes live live in 'interpreter/gateway.py' 
  which seems to be a good name to define stuff that 'connects' 
  interp-level and app-level execution. 

- disabled 'mod' and 'divmod' complexobject-tests because they
  fail in python-2.3 (which also issues deprecation warnings)

- improved the tool.test to skip non-importable modules (and
  print a warning)

so much for now.



Modified: pypy/branch/builtinrefactor/pypy/appspace/test/test_cmathmodule.py
==============================================================================
--- pypy/branch/builtinrefactor/pypy/appspace/test/test_cmathmodule.py	(original)
+++ pypy/branch/builtinrefactor/pypy/appspace/test/test_cmathmodule.py	Sun Jul 27 20:53:08 2003
@@ -24,7 +24,7 @@
 #    import cmathmodule
 #    from pypy.complexobject import complex as pycomplex
 
-from test_complexobject import equal, enumerate
+from pypy.appspace.test.test_complexobject import equal, enumerate
 
 
 class TestCMathModule(test.TestCase):

Modified: pypy/branch/builtinrefactor/pypy/appspace/test/test_complexobject.py
==============================================================================
--- pypy/branch/builtinrefactor/pypy/appspace/test/test_complexobject.py	(original)
+++ pypy/branch/builtinrefactor/pypy/appspace/test/test_complexobject.py	Sun Jul 27 20:53:08 2003
@@ -183,7 +183,8 @@
             self.assertEqual(hash(zc), hash(zp))
 
 
-    def test_divmod(self):
+    # this fails on python2.3 and is depreacted anyway
+    def _test_divmod(self):
         "Compare divmod with CPython."
         
         for (z0c, z1c, z0p, z1p) in enumerate():
@@ -200,7 +201,8 @@
                 self.assertAEqual(mmc, mmp)
 
 
-    def test_mod(self):
+    # these fail on python2.3
+    def _test_mod(self):
         "Compare mod with CPython."
         
         for (z0c, z1c, z0p, z1p) in enumerate():

Deleted: /pypy/branch/builtinrefactor/pypy/appspace/test/test_exceptcomp.py
==============================================================================
--- /pypy/branch/builtinrefactor/pypy/appspace/test/test_exceptcomp.py	Sun Jul 27 20:53:08 2003
+++ (empty file)
@@ -1,138 +0,0 @@
-"""Test comparisons of Exceptions in except clauses.
-
-New for PyPy - Could be incorporated into CPython regression tests.
-"""
-import autopath
-from pypy.tool import test
-
-class TestExceptionComp(test.AppTestCase):
-
-    def setUp(self):
-        self.space = test.objspace()
-
-### XXX - String exceptions depreciated?
-##    def test_string(self):
-##        string = "string"
-##        try:
-##            raise string
-##        except string:
-##            pass
-##        except:
-##            self.fail("Identical string exceptions do not match.") 
-##
-##    def test_stringfail(self):
-##        string1 = "string1"
-##        string1_ = "string" + "1"
-##        assert string1 is not string1_
-##        try:
-##            raise string1
-##        except "string2":
-##            self.fail("Different string exceptions match.") 
-##        except string1_:
-##            self.fail("Non Identical string exceptions match.")
-##        except string1:
-##            pass
-##        except:
-##            self.fail("Unknown value for variable raise.")
-            
-
-    def test_exception(self):
-        try:
-            raise TypeError, "nothing"
-        except TypeError:
-            pass
-        except:
-            self.fail("Identical exceptions do not match.") 
-
-    def test_exceptionfail(self):
-        try:
-            raise TypeError, "nothing"
-        except KeyError:
-            self.fail("Different exceptions match.")
-        except TypeError:
-            pass
-        except:
-            self.fail("Unanticipated value for exception raise.")
-            
-
-    def test_called(self):
-        try:
-            raise SyntaxError("Invalid")
-        except SyntaxError:
-            pass
-        except:
-            self.fail("Instantiated exception does not match parent class.") 
-
-    def test_calledfail(self):
-        try:
-            raise SyntaxError("Invalid")
-        except ZeroDivisionError:
-            self.fail("Instantiated exception matches different parent class.") 
-        except SyntaxError:
-            pass
-        except:
-            self.fail("Unanticpated value for exception raise.")
-            
-        
-    def test_userclass(self):
-        class UserExcept(Exception):
-            pass
-        try:
-            raise UserExcept, "nothing"
-        except UserExcept:
-            pass
-        except:
-            self.fail("User defined class exceptions do not match.") 
-            
-    def test_subclass(self):
-        try:
-            raise KeyError("key")
-        except LookupError:
-            pass
-        except:
-            self.fail("Exception does not match parent class.") 
-
-    def test_deepsubclass(self):
-        try:
-            raise FloatingPointError("1.2r")
-        except Exception:
-            pass
-        except:
-            self.fail("Exception does not match grandparent class.") 
-
-    def test_tuple(self):
-        try:
-            raise ArithmeticError("2+jack")
-        except (ZeroDivisionError, ArithmeticError):
-            pass
-        except:
-            self.fail("Exception does not match self in tuple.") 
-
-    def test_parenttuple(self):
-        try:
-            raise ZeroDivisionError("0")
-        except (StandardError, SystemExit):
-            pass
-        except:
-            self.fail("Exception does not match parent in tuple.") 
-
-    def test_nestedtuples(self):
-        try:
-            raise AssertionError("0")
-        except (SystemExit, (KeyboardInterrupt, AssertionError)):
-            pass
-        except:
-            self.fail("Exception does not match self in nested tuple.") 
-
-    def test_deeptuples(self):
-        try:
-            raise IOError
-        except (FloatingPointError,(OSError,
-                                    (SyntaxError,IOError,ZeroDivisionError)),
-                (MemoryError, NotImplementedError)):
-            pass
-        except:
-            self.fail("Exception does not match self in deeply nested tuple.")
-            
-if __name__ == "__main__":
-    test.main()

Deleted: /pypy/branch/builtinrefactor/pypy/appspace/test/test_exec.py
==============================================================================
--- /pypy/branch/builtinrefactor/pypy/appspace/test/test_exec.py	Sun Jul 27 20:53:08 2003
+++ (empty file)
@@ -1,76 +0,0 @@
-"""Test the exec statement functionality.
-
-New for PyPy - Could be incorporated into CPython regression tests.
-"""
-import autopath
-from pypy.tool import test 
-
-class TestExecStmt(test.AppTestCase):
-
-    def setUp(self):
-        self.space = test.objspace()
-
-    def test_string(self):
-        g = {}
-        l = {}
-        exec "a = 3" in g, l
-        self.failUnlessEqual(l['a'], 3)
-
-    def test_localfill(self):
-        g = {}
-        exec "a = 3" in g
-        self.failUnlessEqual(g['a'], 3)
-        
-    def test_builtinsupply(self):
-        g = {}
-        exec "pass" in g
-        self.failUnless(g.has_key('__builtins__'))
-
-    def test_invalidglobal(self):
-        def f():
-            exec 'pass' in 1
-        self.failUnlessRaises(TypeError,f)
-
-    def test_invalidlocal(self):
-        def f():
-            exec 'pass' in {}, 2
-        self.failUnlessRaises(TypeError,f)
-
-    def test_codeobject(self):
-        co = compile("a = 3", '<string>', 'exec')
-        g = {}
-        l = {}
-        exec co in g, l
-        self.failUnlessEqual(l['a'], 3)
-        
-##    # Commented out as PyPy give errors using open()
-##    #     ["Not availible in restricted mode"]
-##    def test_file(self):
-##        fo = open("test_exec.py", 'r')
-##        g = {}
-##        exec fo in g
-##        self.failUnless(g.has_key('TestExecStmt'))
-        
-    def test_implicit(self):
-        a = 4
-        exec "a = 3"
-        self.failUnlessEqual(a,3)
-
-    def test_tuplelocals(self):
-        g = {}
-        l = {}
-        exec ("a = 3", g, l)
-        self.failUnlessEqual(l['a'], 3)
-        
-    def test_tupleglobals(self):
-        g = {}
-        exec ("a = 3", g)
-        self.failUnlessEqual(g['a'], 3)
-
-    def test_exceptionfallthrough(self):
-        def f():
-            exec 'raise TypeError' in {}
-        self.failUnlessRaises(TypeError,f)
-
-if __name__ == "__main__":
-    test.main()

Modified: pypy/branch/builtinrefactor/pypy/appspace/types.py
==============================================================================
--- pypy/branch/builtinrefactor/pypy/appspace/types.py	(original)
+++ pypy/branch/builtinrefactor/pypy/appspace/types.py	Sun Jul 27 20:53:08 2003
@@ -1,4 +1,6 @@
-"""Define names for all type symbols known in the standard interpreter.
+"""Appspace types module. 
+
+Define names for all type symbols known in the standard interpreter.
 
 Types that are part of optional modules (e.g. array) are not listed.
 """

Modified: pypy/branch/builtinrefactor/pypy/interpreter/appfile.py
==============================================================================
--- pypy/branch/builtinrefactor/pypy/interpreter/appfile.py	(original)
+++ pypy/branch/builtinrefactor/pypy/interpreter/appfile.py	Sun Jul 27 20:53:08 2003
@@ -48,25 +48,19 @@
         w_obj = self.space.getitem(self.w_namespace, w_name)
         return w_obj
 
-    def call(self, functionname, argumentslist):
+    def call(self, functionname, args):
         "Call a module function."
-        w_function = self.get(functionname)
-        w_arguments = self.space.newtuple(argumentslist)
+        w_func = self.get(functionname)
+        w_args = self.space.newtuple(args)
         w_keywords = self.space.newdict([])
-        return self.space.call(w_function, w_arguments, w_keywords)
+        return self.space.call(w_func, w_args, w_keywords)
 
     def runbytecode(self, bytecode):
         # initialize the module by running the bytecode in a new
         # dictionary, in a new execution context
-        from pyframe import PyFrame
-        from pycode import PyByteCode
-        ec = self.space.getexecutioncontext()
-        res = PyByteCode()
-        res._from_code(bytecode)
-        frame = PyFrame(self.space, res,
-                                self.w_namespace, self.w_namespace)
-        ec.eval_frame(frame)
-
+        from pypy.interpreter.gateway import ScopedCode
+        scopedcode = ScopedCode(self.space, bytecode, self.w_namespace)
+        scopedcode.eval_frame()
 
 class AppHelper(Namespace):
 

Modified: pypy/branch/builtinrefactor/pypy/interpreter/baseobjspace.py
==============================================================================
--- pypy/branch/builtinrefactor/pypy/interpreter/baseobjspace.py	(original)
+++ pypy/branch/builtinrefactor/pypy/interpreter/baseobjspace.py	Sun Jul 27 20:53:08 2003
@@ -9,6 +9,10 @@
         self.space = space
         self.operationerr = operationerr
 
+class Null:
+    """ marker object for Null values within the interpreter
+        (not visible at app-level)
+    """
 
 class ObjSpace:
     """Base class for the interpreter-level implementations of object spaces.
@@ -16,14 +20,17 @@
 
     def __init__(self):
         "Basic initialization of objects."
-        self.w_modules = self.newdict([])
         self.appfile_helpers = {}
         self.initialize()
 
     def make_builtins(self):
-        import pypy.module.builtin
-        self.builtin = pypy.module.builtin.Builtin(self)
-        self.w_builtin = self.builtin.wrap_base()
+        assert not hasattr(self, 'builtin')
+        if not hasattr(self, 'sys'):
+            self.make_sys()
+        
+        from pypy.module import builtin
+        self.builtin = builtin.__builtin__(self)
+        self.w_builtin = self.builtin._wrapped
         self.w_builtins = self.getattr(self.w_builtin, self.wrap("__dict__"))
 
         for name, value in self.__dict__.items():
@@ -34,20 +41,17 @@
                 #print "setitem: space instance %-20s into builtins" % name
                 self.setitem(self.w_builtins, self.wrap(name), value)
 
-        from pypy.module import __file__ as fn
-        import os
-        fn = os.path.join(os.path.dirname(fn), 'builtin_app.py')
-        w_args = self.newtuple([self.wrap(fn), self.w_builtins, self.w_builtins])
-        w_execfile = self.getitem(self.w_builtins, self.wrap('execfile'))
-        self.call(w_execfile, w_args, self.newdict([]))
+        self.builtin._wrap_postponed()
+        self.sys._setmodule(self.builtin)
 
     def make_sys(self):
-        import pypy.module.sysmodule
-        self.sys = pypy.module.sysmodule.Sys(self)
-        self.w_sys = self.sys.wrap_me()
-        self.setattr(self.w_sys, self.wrap("modules"), self.w_modules)
+        assert not hasattr(self, 'sys')
+        from pypy.module import sysmodule
+        self.sys = sysmodule.sys(self)
+        self.w_sys = self.sys._wrapped
+        self.sys._setmodule(self.sys)
 
-    # XXX use a dictionary in the future
+    # XXX get rid of this. 
     def get_builtin_module(self, w_name):
         name = self.unwrap(w_name)
         if name == '__builtin__':

Modified: pypy/branch/builtinrefactor/pypy/interpreter/executioncontext.py
==============================================================================
--- pypy/branch/builtinrefactor/pypy/interpreter/executioncontext.py	(original)
+++ pypy/branch/builtinrefactor/pypy/interpreter/executioncontext.py	Sun Jul 27 20:53:08 2003
@@ -209,6 +209,17 @@
     def __init__(self):
         self.items = []
 
+    def clone(self):
+        s = self.__class__()
+        for item in self.items:
+            try:
+                item = item.clone()
+            except AttributeError:
+                pass
+            s.push(item)
+        return s
+
+
     def push(self, item):
         self.items.append(item)
 

Deleted: /pypy/branch/builtinrefactor/pypy/interpreter/extmodule.py
==============================================================================
--- /pypy/branch/builtinrefactor/pypy/interpreter/extmodule.py	Sun Jul 27 20:53:08 2003
+++ (empty file)
@@ -1,149 +0,0 @@
-from pypy.interpreter import pycode
-import appfile
-
-class appmethod(object):
-    def __init__(self, func):
-        self.func = func
-    def __get__(self, instance, cls=None):
-        return self.func.__get__(instance, cls)
-
-class appdata(object):
-    def __init__(self, data):
-        self.data = data
-    def __get__(self, instance, cls=None):
-        return self.data
-
-
-# a little excercise in OOP, Python 2.2-style:
-
-class PyBuiltinCode(pycode.PyBaseCode):
-    """The code object implementing a built-in (interpreter-level) hook."""
-
-    def __init__(self, func, boundmethod=False):
-        pycode.PyBaseCode.__init__(self)
-        self.func = func
-        co = func.func_code
-        self.co_name = func.__name__
-        self.co_flags = co.co_flags
-        if boundmethod:
-            assert co.co_varnames[0] == "self"
-            start = 1
-        else:
-            start = 0
-        argnames = []
-        for argname in co.co_varnames[start:co.co_argcount]:
-            assert argname.startswith('w_')
-            argnames.append(argname[2:])
-        self.co_varnames = tuple(argnames)
-        self.co_argcount = co.co_argcount - start
-        self.next_arg = self.co_argcount + start
-
-    def prepare_args(self, space, w_locals):
-        args = []
-        for argname in self.co_varnames[:self.co_argcount]:
-            w_arg = space.getitem(w_locals, space.wrap(argname))
-            args.append(w_arg)
-        return args
-        
-    def eval_code(self, space, w_globals, w_locals):
-        args = self.prepare_args(space, w_locals)
-        return self.func(*args)
-
-
-class PyBuiltinVarCode(PyBuiltinCode):
-
-    def __init__(self, func, boundmethod=False):
-        super(PyBuiltinVarCode, self).__init__(func, boundmethod)
-        self.vararg_name = func.func_code.co_varnames[self.next_arg]
-        self.co_varnames += (self.vararg_name,)
-        assert self.vararg_name.endswith('_w'), "%s, arg %d: %s"%(
-            func.func_name, self.co_argcount + 1, self.vararg_name)
-        self.next_arg += 1
-
-    def prepare_args(self, space, w_locals):
-        args = super(PyBuiltinVarCode, self).prepare_args(space, w_locals)
-        w_args = space.getitem(w_locals, space.wrap(self.vararg_name))
-        args.extend(space.unpackiterable(w_args))
-        return args
-
-
-class PyBuiltinKwCode(PyBuiltinCode):
-    def __init__(self, func, boundmethod=False):
-        super(PyBuiltinKwCode, self).__init__(func, boundmethod)
-        self.kwarg_name = func.func_code.co_varnames[self.next_arg]
-        self.co_varnames += (self.kwarg_name,)
-        assert self.kwarg_name.endswith('_w'), "%s, arg %d: %s"%(
-            func.func_name, self.co_argcount + 1, self.kwarg_name)
-        self.next_arg += 1
-
-    def eval_code(self, space, w_globals, w_locals):
-        args = self.prepare_args(space, w_locals)
-        w_kws = space.getitem(w_locals, space.wrap(self.kwarg_name))
-        kws = {}
-        for w_key in space.unpackiterable(w_kws):
-            kws[space.unwrap(w_key)] = space.getitem(w_kws, w_key)
-        
-        return self.func(*args, **kws)
-
-
-class PyBuiltinVarKwCode(PyBuiltinKwCode, PyBuiltinVarCode):
-    pass
-
-
-def make_builtin_func(space, func, boundmethod=False):
-    if func.func_code.co_flags & pycode.CO_VARARGS:
-        if func.func_code.co_flags & pycode.CO_VARKEYWORDS:
-            code_cls = PyBuiltinVarKwCode
-        else:
-            code_cls = PyBuiltinVarCode
-    else:
-        if func.func_code.co_flags & pycode.CO_VARKEYWORDS:
-            code_cls = PyBuiltinKwCode
-        else:
-            code_cls = PyBuiltinCode
-    code = code_cls(func, boundmethod)
-    w_defaults = space.wrap(func.func_defaults)
-    w_function = space.newfunction(code, space.w_None, w_defaults)
-    return w_function
-    
-class BuiltinModule:
-    __appfile__ = None
-    __helper_appfile__ = None
-
-    _helper = None
-
-    def __init__(self, space):
-        self.space = space
-            
-    def wrap_me(self):
-        w_module = self.wrap_base()
-        self.wrap_appfile(w_module)
-        return w_module
-
-    def wrap_base(self):
-        space = self.space
-        modulename = self.__pythonname__
-        w_module = space.newmodule(space.wrap(modulename))
-        for key, value in self.__class__.__dict__.items() + self.__dict__.items():
-            if isinstance(value, appmethod):
-                w_function = make_builtin_func(space,
-                                               value.func.__get__(self),
-                                               boundmethod=True)
-                space.setattr(w_module, space.wrap(key), w_function)
-            elif isinstance(value, appdata):
-                w_data = space.wrap(value.data)
-                space.setattr(w_module, space.wrap(key), w_data)
-        return w_module
-
-    def wrap_appfile(self, w_module):
-        sappfile = self.__appfile__
-        if sappfile:
-            space = self.space
-            w_dict = space.getattr(w_module, space.wrap("__dict__"))
-            appfile.AppHelper(space, sappfile, w_dict)
-
-    def callhelp(self, functioname, argslist):
-        if self._helper is None:
-            self._helper = appfile.AppHelper(self.space,
-                                             self.__helper_appfile__)
-        self._helper.call(functioname,argslist)

Added: pypy/branch/builtinrefactor/pypy/interpreter/gateway.py
==============================================================================
--- (empty file)
+++ pypy/branch/builtinrefactor/pypy/interpreter/gateway.py	Sun Jul 27 20:53:08 2003
@@ -0,0 +1,343 @@
+"""
+
+gateway between app-level and interpreter-level 
+
+"""
+import inspect
+CO_VARARGS, CO_VARKEYWORDS = 0x4, 0x8
+
+class app2interp(object):
+    """ this class exposes an app-level defined function at interpreter-level 
+        Assumption:  the interp-level function will be called ala
+
+                            a.function(arg1, arg2, argn)
+
+                     with 'a' having an attribute 'space' which the app-level 
+                     code should run in. (might change in during the branch)
+    """
+    def __init__(self, func):
+        #print "making app2interp for", func
+        self.func = func
+        self._codecache = {}
+
+    def __get__(self, instance, cls=None):
+        space = instance.space
+        try:
+            return self._codecache[(space, instance, self)] 
+        except KeyError:
+            c = InterpretedFunction(space, self.func)
+            c.im_self = instance
+            assert c.simple, "function should be simple but isn't"
+            self._codecache[(space, instance, self)] = c
+            return c
+
+class ScopedCode(object):
+    """ a code object within a certain global and closure scope.
+    (the local scope is given when you call 'eval_frame')
+    """
+    def __init__(self, space, cpycode, w_globals=None, closure_w=()):
+        self.space = space
+        self.cpycode = cpycode
+        self.w_code = space.wrap(cpycode)
+        self.closure_w = closure_w
+        if w_globals is None:
+            w_globals = space.newdict([])
+        self.w_globals = w_globals
+
+    def create_frame(self, w_locals=None):
+        """ return result of executing code object within a frame"""
+        from pyframe import PyFrame
+        frame = PyFrame()
+        frame.initialize(self)
+        if w_locals is None:
+            w_locals = self.w_globals
+        frame.setdictscope(w_locals)
+        return frame
+
+    def eval_frame(self, *args, **kwargs):
+        frame = self.create_frame(*args, **kwargs)
+        return self.space.getexecutioncontext().eval_frame(frame)
+
+class InterpretedFunction(ScopedCode):
+    """ a function which executes at app-level (by interpreting bytecode
+    and dispatching operations on an objectspace). 
+    """
+
+    def __init__(self, space, cpyobj, w_globals=None, closure_w=()): 
+        """ initialization similar to base class but it also wraps 
+        some function-specific stuff (like defaults). 
+        """
+        if hasattr(cpyobj, 'func_code'):
+            self.w_defs = space.wrap(cpyobj.func_defaults or ()) 
+            cpycode = cpyobj.func_code
+        else:
+            self.w_defs = space.newtuple([])
+            cpycode = cpyobj
+        assert type(self.w_defs) is tuple, "type of desfs is %s" % type(self.w_defs)
+        ScopedCode.__init__(self, space, cpycode, w_globals, closure_w)
+        self.simple = cpycode.co_flags & (CO_VARARGS|CO_VARKEYWORDS)==0
+
+    def parse_args(self, frame, w_args, w_kwargs):
+        """ parse args and kwargs and set fast scope of frame.
+        """
+        space = self.space
+        if hasattr(self, 'im_self'):
+            args_w = space.unpacktuple(w_args)
+            args_w = [space.wrap(self.im_self)] + args_w
+            w_args = space.newtuple(args_w)
+
+        loc_w = None
+        if self.simple and (w_kwargs is None or not space.is_true(w_kwargs)):
+            try:
+                loc_w = space.unpacktuple(w_args, self.cpycode.co_argcount)
+            except ValueError:
+                pass
+        if loc_w is None:
+            #print "complicated case of arguments for", self.cpycode.co_name, "simple=", self.simple
+            assert type(self.w_defs) is tuple
+            w_loc = self.parse_args_complex(self.w_code, w_args, w_kwargs, self.w_defs)
+            loc_w = space.unpacktuple(w_loc)
+        loc_w.extend([_NULL] * (self.cpycode.co_nlocals - len(loc_w)))
+
+        # make nested cells
+        for name in self.cpycode.co_cellvars:
+            i = list(self.cpycode.co_varnames).index(name)
+            w_value = loc_w[i] 
+            loc_w[i] = _NULL
+            frame.closure_w += (Cell(w_value),)
+
+        assert len(loc_w) == self.cpycode.co_nlocals, "local arguments not prepared correctly"
+        frame.setfastscope(loc_w)
+
+    def __get__(self, instance, cls=None):
+        func = object.__new__(self.__class__)
+        func.__dict__.update(self.__dict__)
+        func.im_self = instance
+        return func
+
+    def create_frame(self, w_args, w_kwargs):
+        """ parse arguments and execute frame """
+        from pyframe import PyFrame
+        frame = PyFrame()
+        frame.initialize(self)
+        self.parse_args(frame, w_args, w_kwargs)
+        return frame
+
+    def parse_args_complex_w(self, cpycode, args, kwargs, defs):
+        """ return list of initial local values parsed from 
+        'args', 'kwargs' and defaults.
+        """
+        #if cpycode.co_name == 'failUnlessRaises':
+        #    print "co_name", cpycode.co_name
+        #    print "co_argcount", cpycode.co_argcount
+        #    print "co_nlocals", cpycode.co_nlocals
+        #    print "co_varnames", cpycode.co_varnames
+        #    print "args", args
+        #    print "kwargs", kwargs
+        #    print "defs", defs
+
+        CO_VARARGS, CO_VARKEYWORDS = 0x4, 0x8
+
+        #   co_argcount number of expected positional arguments 
+        #   (elipsis args like *args and **kwargs do not count) 
+        co_argcount = cpycode.co_argcount
+
+        # construct list of positional args 
+        positional_args = list(args[:co_argcount])
+
+        len_args = len(args)
+        len_defs = len(defs)
+
+        if len_args < co_argcount:
+            # not enough args, fill in kwargs or defaults if exists
+            i = len_args
+            while i < co_argcount:
+                name = cpycode.co_varnames[i]
+                if name in kwargs:
+                    positional_args.append(kwargs[name])
+                    del kwargs[name]
+                else:
+                    if i + len_defs < co_argcount:
+                        raise TypeError, "Not enough arguments"
+                    positional_args.append(defs[i-co_argcount])
+                i+=1
+        if cpycode.co_flags & CO_VARARGS:
+            positional_args.append(tuple(args[co_argcount:]))
+        elif len_args > co_argcount:
+            raise TypeError, "Too many arguments"
+
+        i = 0
+        while i < len_args and i < co_argcount:
+            name = cpycode.co_varnames[i]
+            if name in kwargs:
+                raise TypeError, "got multiple values for argument %r" % name
+            i+=1
+
+
+        # XXX probably need an extra pass over kwargs to determine if a 
+        #     keyword-arg sets a second time 
+        if cpycode.co_flags & CO_VARKEYWORDS:
+            positional_args.append(kwargs)
+        elif kwargs:
+            raise TypeError, "got unexpected keyword argument(s) %s" % repr(kwargs.keys()[0])
+
+        return positional_args
+    parse_args_complex = app2interp(parse_args_complex_w)
+
+    def __call__(self, *args_w, **kwargs_w):
+        """ call with native parameter passing convention """
+        #print "native call on", self.cpycode.co_name
+        #print "len args_w", len(args_w)
+        #print "kwargs_w", kwargs_w
+        w_args = self.space.newtuple(args_w)
+        w = self.space.wrap
+        w_kwargs = self.space.newdict([])
+        for name, w_value in kwargs_w.items():
+            self.space.setitem(w_kwargs, w(name), w_value)
+        return self.eval_frame(w_args, w_kwargs)
+
+class AppVisibleModule:
+    """ app-level visible Module defined at interpreter-level.
+
+    Inherit from this class if you want to have a module that accesses
+    the PyPy interpreter (e.g. builtins like 'locals()' require accessing the
+    frame). You can mix in application-level code by prefixing your method
+    with 'app_'. Both non-underscore methods and app-level methods will
+    be available on app-level with their respective name. 
+
+    On __wrap__(space) it returns an app-level (read: wrapped) module object. 
+
+    functions prefixed with 'app_' indicate app-level running functions.
+    Note that these functions don't get a 'self' argument because we really
+    need only the function (there is no notion of beeing 'bound' or 'unbound' 
+    for them).
+    
+    """
+    def __init__(self, space):
+        self.space = space
+
+        if hasattr(self, '_wrap_postponed'):
+            self._postponed = []
+
+        space = self.space
+        modname = self.__class__.__name__
+        self.w___name__ = space.wrap(modname)
+        self._wrapped = _wrapped = space.newmodule(self.w___name__)
+
+        # go through all items in the module class instance
+        for name in dir(self):
+            # skip spurious info and internal methods
+            if name == '__module__' or name.startswith('_') and not name.endswith('_'):
+                #print modname, "skipping", name
+                continue
+            obj = getattr(self, name)
+            # see if we just need to expose an already wrapped value
+            if name.startswith('w_'):
+                space.setattr(_wrapped, space.wrap(name[2:]), obj)
+
+            # see if have something defined at app-level
+            elif name.startswith('app_'):
+                obj = self.__class__.__dict__.get(name)
+                name = name[4:]
+
+                # just a simple function? 
+                if hasattr(obj, 'func_code'):
+                    w_res = space.wrap(InterpretedFunction(space, obj))
+
+                # or a class? (which eventually needs to be recompiled)
+                elif inspect.isclass(obj):
+                    # this module chooses to do this later (usually
+                    # for bootstrapping reasons)
+                    if hasattr(self, '_postponed'):
+                        self._postponed.append((name, obj))
+                    else:
+                        w_res = wrap_applevel_class(space, name, obj)
+                else:
+                    raise ValueError, "cannot wrap %s, %s" %(name, obj)
+
+            else:
+                w_res = wrap_interplevel(space, name, obj)
+                setattr(self, 'w_'+name, w_res)
+            w_name = space.wrap(name)
+            space.setattr(_wrapped, w_name, w_res)
+
+def wrap_applevel(space, name, obj):
+    """ wrap an app-level object which was compiled/lives 
+    at interpreter-level.
+    """
+    #print "app registering ", name #, obj
+    if hasattr(obj, 'func_code'):
+        return space.wrap(InterpretedFunction(space, obj))
+    elif inspect.isclass(obj):
+        return wrap_applevel_class(space, name, obj)
+    else:
+        raise ValueError, "cannot wrap %s, %s" % (name, obj)
+
+def wrap_applevel_class(space, name, obj):
+    """ construct an app-level class by reproducing the
+    source definition and running it through the interpreter.
+    It's a bit ugly but i don't know a better way (holger). 
+    """
+    l = ['class %s:' % name]
+    indent = '    '
+    for key, value in vars(obj).items():
+        if hasattr(value, 'func_code'):
+            s = inspect.getsource(value)
+            l.append(s)
+            indent = " " * (len(s) - len(s.lstrip()))
+
+    if getattr(obj, '__doc__', None):
+        l.insert(1, indent + obj.__doc__)
+
+    for key, value in vars(obj).items():
+        if not key in ('__module__', '__doc__'):
+            if isinstance(value, (str, int, float, tuple, list)):
+                l.append('%s%s = %r' % (indent, key, value))
+
+    s = "\n".join(l)
+    code = compile(s, s, 'exec')
+    scopedcode = ScopedCode(space, code, None)
+    scopedcode.eval_frame()
+    w_name = space.wrap(name)
+    w_res = space.getitem(scopedcode.w_globals, w_name)
+    return w_res
+
+def wrap_interplevel(space, name, obj):
+    """ wrap an app-level object which was compiled/lives 
+    at interpreter-level.
+    """
+    return space.wrap(obj)
+
+
+## Cells (used for nested scopes only) ##
+
+_NULL = object() # Marker object
+
+class Cell:
+    def __init__(self, w_value=_NULL):
+        self.w_value = w_value
+
+    def clone(self):
+        return self.__class__(self.w_value)
+
+    def get(self):
+        if self.w_value is _NULL:
+            raise ValueError, "get() from an empty cell"
+        return self.w_value
+
+    def set(self, w_value):
+        self.w_value = w_value
+
+    def delete(self):
+        if self.w_value is _NULL:
+            raise ValueError, "make_empty() on an empty cell"
+        self.w_value = _NULL
+
+    def __repr__(self):
+        """ representation for debugging purposes """
+        if self.w_value is _NULL:
+            return "%s()" % self.__class__.__name__
+        else:
+            return "%s(%s)" % (self.__class__.__name__, self.w_value)
+
+

Modified: pypy/branch/builtinrefactor/pypy/interpreter/interactive.py
==============================================================================
--- pypy/branch/builtinrefactor/pypy/interpreter/interactive.py	(original)
+++ pypy/branch/builtinrefactor/pypy/interpreter/interactive.py	Sun Jul 27 20:53:08 2003
@@ -35,11 +35,9 @@
         code.InteractiveConsole.interact(self, banner)
 
     def runcode(self, code):
-        from pypy.interpreter import pycode
-        r = pycode.PyByteCode()
-        r._from_code(code)
-        frame = pyframe.PyFrame(self.space, r,
-                                self.w_globals, self.w_globals)
+        from pypy.interpreter.gateway import ScopedCode
+        scopedcode = ScopedCode(self.space, code, self.w_globals)
+        frame = scopedcode.create_frame()
         try:
             self.ec.eval_frame(frame)
         except baseobjspace.OperationError, operationerr:

Modified: pypy/branch/builtinrefactor/pypy/interpreter/main.py
==============================================================================
--- pypy/branch/builtinrefactor/pypy/interpreter/main.py	(original)
+++ pypy/branch/builtinrefactor/pypy/interpreter/main.py	Sun Jul 27 20:53:08 2003
@@ -1,8 +1,6 @@
 import autopath
 from pypy.tool import test, option
-from pypy.objspace.std import StdObjSpace
-from pypy.module.builtin import Builtin
-from pypy.interpreter import executioncontext, baseobjspace, pyframe
+from pypy.interpreter import executioncontext, baseobjspace, gateway
 import sys, os
 
 def _run_eval_string(source, filename, space, eval):
@@ -13,6 +11,7 @@
         
     try:
         if space is None:
+            from pypy.objspace.std import StdObjSpace
             space = StdObjSpace()
 
         compile = space.builtin.compile
@@ -25,13 +24,13 @@
         w_mainmodule = space.newmodule(space.wrap("__main__"))
         w_globals = space.getattr(w_mainmodule, space.wrap("__dict__"))
         space.setitem(w_globals, space.wrap("__builtins__"), space.w_builtins)
-        
-        frame = pyframe.PyFrame(space, space.unwrap(w_code),
-                                w_globals, w_globals)
+       
     except baseobjspace.OperationError, operationerr:
         operationerr.record_interpreter_traceback()
         raise baseobjspace.PyPyError(space, operationerr)
     else:
+        scopedcode = gateway.ScopedCode(space, space.unwrap(w_code), w_globals)
+        frame = scopedcode.create_frame()
         if eval:
             return ec.eval_frame(frame)
         else:

Modified: pypy/branch/builtinrefactor/pypy/interpreter/opcode.py
==============================================================================
--- pypy/branch/builtinrefactor/pypy/interpreter/opcode.py	(original)
+++ pypy/branch/builtinrefactor/pypy/interpreter/opcode.py	Sun Jul 27 20:53:08 2003
@@ -1,8 +1,9 @@
 from appfile import AppFile
 from pypy.interpreter.baseobjspace import OperationError, NoValue
+from pypy.interpreter.pyframe import _NULL
 import dis
 from pypy.interpreter import pyframe, baseobjspace
-from pypy.interpreter.pycode import app2interp
+from pypy.interpreter.gateway import app2interp, ScopedCode
 
 
 # dynamically loaded application-space utilities
@@ -35,10 +36,8 @@
 
 def LOAD_FAST(f, varindex):
     # access a local variable through its cell object
-    cell = f.localcells[varindex]
-    try:
-        w_value = cell.get()
-    except ValueError:
+    w_value = f.locals_w[varindex]
+    if w_value is _NULL:
         varname = f.getlocalvarname(varindex)
         message = "local variable '%s' referenced before assignment" % varname
         raise OperationError(f.space.w_UnboundLocalError, f.space.wrap(message))
@@ -49,9 +48,19 @@
     f.valuestack.push(w_const)
 
 def STORE_FAST(f, varindex):
-    w_newvalue = f.valuestack.pop()
-    cell = f.localcells[varindex]
-    cell.set(w_newvalue)
+    try:
+        w_newvalue = f.valuestack.pop()
+        f.locals_w[varindex] = w_newvalue
+    except:
+        print "exception: got index error"
+        print " varindex:", varindex
+        print " len(locals_w)", len(f.locals_w)
+        import dis
+        print dis.dis(f.bytecode)
+        print "co_varnames", f.bytecode.co_varnames
+        print "co_nlocals", f.bytecode.co_nlocals
+        raise
+
 
 def POP_TOP(f):
     f.valuestack.pop()
@@ -215,6 +224,7 @@
 
 def PRINT_EXPR(f):
     w_expr = f.valuestack.pop()
+    #print f.space.unwrap(w_expr)
     f.space.gethelper(appfile).call("print_expr", [w_expr])
 
 def PRINT_ITEM_TO(f):
@@ -292,7 +302,9 @@
     plain = (w_locals is f.w_locals)
     if plain:
         f.fast2locals()
-    f.space.unwrap(w_prog).eval_code(f.space, w_globals, w_locals)
+    scopedcode = ScopedCode(f.space, f.space.unwrap(w_prog), w_globals)
+    scopedcode.eval_frame(w_locals)
+    #f.space.unwrap(w_prog).eval_code(f.space, w_globals, w_locals)
     if plain:
         f.locals2fast()
     
@@ -337,7 +349,7 @@
     w_methodsdict = f.valuestack.pop()
     w_bases       = f.valuestack.pop()
     w_name        = f.valuestack.pop()
-    w_newclass = app(f.space).build_class(w_name, w_bases, w_methodsdict, f.w_globals)
+    w_newclass = app(f.space.gethelperspace()).build_class(w_name, w_bases, w_methodsdict, f.w_globals)
     f.valuestack.push(w_newclass)
 
 def STORE_NAME(f, varindex):
@@ -425,6 +437,7 @@
 #    f.valuestack.push(w_value)
 
 def LOAD_GLOBAL(f, nameindex):
+    assert f.w_globals is not None
     varname = f.getname(nameindex)
     w_varname = f.space.wrap(varname)
     try:
@@ -447,23 +460,22 @@
     f.valuestack.push(w_value)
 
 def DELETE_FAST(f, varindex):
-    cell = f.localcells[varindex]
-    try:
-        cell.delete()
-    except ValueError:
+    w_value = f.locals_w[varindex]
+    if f.locals_w[varindex] is _NULL:
         varname = f.getlocalvarname(varindex)
         message = "local variable '%s' referenced before assignment" % varname
         raise OperationError(f.space.w_UnboundLocalError, f.space.wrap(message))
+    f.locals_w[varindex] = _NULL
 
 def LOAD_CLOSURE(f, varindex):
     # nested scopes: access the cell object
-    cell = f.nestedcells[varindex]
+    cell = f.closure_w[varindex]
     w_value = f.space.wrap(cell)
     f.valuestack.push(w_value)
 
 def LOAD_DEREF(f, varindex):
     # nested scopes: access a variable through its cell object
-    cell = f.nestedcells[varindex]
+    cell = f.closure_w[varindex]
     try:
         w_value = cell.get()
     except ValueError:
@@ -482,9 +494,9 @@
     # nested scopes: access a variable through its cell object
     w_newvalue = f.valuestack.pop()
     try:
-        cell = f.nestedcells[varindex]
+        cell = f.closure_w[varindex]
     except IndexError:
-        import pdb; pdb.set_trace()
+        #import pdb; pdb.set_trace()
         raise
     cell.set(w_newvalue)
 
@@ -716,8 +728,9 @@
     if opname in globals():
         fn = globals()[opname]
     elif not opname.startswith('<') and i>0:
-        import warnings
-        warnings.warn("* Warning, missing opcode %s" % opname)
+        #import warnings
+        #warnings.warn("* Warning, missing opcode %s" % opname)
+        pass
     dispatch_table.append(fn)
 
 

Modified: pypy/branch/builtinrefactor/pypy/interpreter/opcode_app.py
==============================================================================
--- pypy/branch/builtinrefactor/pypy/interpreter/opcode_app.py	(original)
+++ pypy/branch/builtinrefactor/pypy/interpreter/opcode_app.py	Sun Jul 27 20:53:08 2003
@@ -158,11 +158,11 @@
             locals = caller_locals
     if locals is None:
         locals = globals
-    if not isinstance(globals, types.DictType):
+    if not isinstance(globals, dict):
         raise TypeError("exec: arg 2 must be a dictionary or None")
     elif not globals.has_key('__builtins__'):
         globals['__builtins__'] = builtins
-    if not isinstance(locals, types.DictType):
+    if not isinstance(locals, dict):
         raise TypeError("exec: arg 3 must be a dictionary or None")
 ##     # XXX - HACK to check for code object
 ##     co = compile('1','<string>','eval')

Modified: pypy/branch/builtinrefactor/pypy/interpreter/pyframe.py
==============================================================================
--- pypy/branch/builtinrefactor/pypy/interpreter/pyframe.py	(original)
+++ pypy/branch/builtinrefactor/pypy/interpreter/pyframe.py	Sun Jul 27 20:53:08 2003
@@ -2,8 +2,7 @@
 """
 
 from pypy.interpreter.executioncontext import OperationError, Stack, NoValue
-
-from pypy.interpreter.pycode import app2interp
+from pypy.interpreter.gateway import app2interp, Cell, _NULL
 
 
 class PyFrame:
@@ -18,18 +17,41 @@
      * 'valuestack', 'blockstack', 'next_instr' control the interpretation
     """
 
-    def __init__(self, space, bytecode, w_globals, w_locals):
-        self.space = space
-        self.bytecode = bytecode # Misnomer; this is really like a code object
-        self.w_globals = w_globals
-        self.w_locals = w_locals
-        self.localcells, self.nestedcells = bytecode.locals2cells(space,
-                                                                  w_locals)
-        self.w_builtins = self.load_builtins()
+    def initialize(self, scopedcode):
+        self.space = scopedcode.space
+        self.bytecode = scopedcode.cpycode
+        self.w_globals = scopedcode.w_globals
+        self.closure_w = scopedcode.closure_w or ()
         self.valuestack = Stack()
         self.blockstack = Stack()
         self.last_exception = None
         self.next_instr = 0
+        self.w_builtins = self.space.w_builtins
+
+    def setdictscope(self, w_locals):
+        self.w_locals = w_locals
+        self.locals_w = [_NULL] * self.bytecode.co_nlocals
+        self.locals2fast()
+
+    def setfastscope(self, locals_w):
+        self.locals_w = locals_w
+        self.w_locals = self.space.newdict([])
+        self.fast2locals()
+
+    def XXXclone(self):
+        f = self.__class__()
+        f.space = self.space
+        f.bytecode = self.bytecode
+        f.w_globals = self.w_globals
+        f.w_locals = self.w_locals
+        f.w_builtins = self.w_builtins
+        f.valuestack = self.valuestack.clone()
+        f.blockstack = self.blockstack.clone()
+        f.localcells = [x.clone() for x in self.localcells]
+        f.nestedcells = [x.clone() for x in self.nestedcells]
+        f.last_exception = self.last_exception
+        f.next_instr = self.next_instr
+        return f
 
     def eval(self, executioncontext):
         "Interpreter main loop!"
@@ -48,6 +70,8 @@
                         else:
                             opcode.dispatch_noarg(self, op)
                     except OperationError, e:
+                        #import traceback
+                        #traceback.print_exc()
                         e.record_application_traceback(self, last_instr)
                         self.last_exception = e
                         executioncontext.exception_trace(e)
@@ -100,57 +124,25 @@
 
     def fast2locals(self):
         # Copy values from self.localcells to self.w_locals
-        for i in range(len(self.localcells)):
-            name = self.bytecode.co_varnames[i]
-            cell = self.localcells[i]
+        for name, w_value in zip(self.bytecode.co_varnames, self.locals_w):
             w_name = self.space.wrap(name)
-            try:
-                w_value = cell.get()
-            except ValueError:
-                pass
-            else:
+            if w_value is not _NULL:
                 self.space.setitem(self.w_locals, w_name, w_value)
 
     def locals2fast(self):
         # Copy values from self.w_locals to self.localcells
         for i in range(self.bytecode.co_nlocals):
-            name = self.bytecode.co_varnames[i]
-            cell = self.localcells[i]
-            w_name = self.space.wrap(name)
+            w_name = self.space.wrap(self.bytecode.co_varnames[i])
             try:
                 w_value = self.space.getitem(self.w_locals, w_name)
             except OperationError, e:
                 if not e.match(self.space, self.space.w_KeyError):
                     raise
-                else:
-                    pass
             else:
-                cell.set(w_value)
+                self.locals_w[i] = w_value
 
     ### frame initialization ###
 
-    def load_builtins(self):
-        # compute w_builtins.  This cannot be done in the '.app.py'
-        # file for bootstrapping reasons.
-        w_builtinsname = self.space.wrap("__builtins__")
-        try:
-            w_builtins = self.space.getitem(self.w_globals, w_builtinsname)
-        except OperationError, e:
-            if not e.match(self.space, self.space.w_KeyError):
-                raise
-            w_builtins = self.space.w_builtins  # fall-back for bootstrapping
-        # w_builtins can be a module object or a dictionary object.
-        # In frameobject.c we explicitely check if w_builtins is a module
-        # object.  Here we will just try to read its __dict__ attribute and
-        # if it fails we assume that it was a dictionary in the first place.
-        w_attrname = self.space.wrap("__dict__")
-        # XXX Commented out the following; it doesn't work for Ann space,
-        # and doesn't seem to be needed for other spaces AFAICT.
-##        try:
-##            w_builtins = self.space.getattr(w_builtins, w_attrname)
-##        except OperationError:
-##            pass # XXX catch and ignore any error
-        return w_builtins
 
     ### exception stack ###
 
@@ -252,8 +244,6 @@
         return etype, evalue
     normalize_exception = app2interp(app_normalize_exception)
 
-
-
 class FinallyBlock(FrameBlock):
     """A try:finally: block.  Stores the position of the exception handler."""
 
@@ -357,69 +347,3 @@
     """Detected bytecode corruption.  Never caught; it's an error."""
 
 
-## Cells ##
-
-_NULL = object() # Marker object
-
-class Cell:
-    def __init__(self, w_value=_NULL):
-        self.w_value = w_value
-
-    def get(self):
-        if self.w_value is _NULL:
-            raise ValueError, "get() from an empty cell"
-        return self.w_value
-
-    def set(self, w_value):
-        self.w_value = w_value
-
-    def delete(self):
-        if self.w_value is _NULL:
-            raise ValueError, "make_empty() on an empty cell"
-        self.w_value = _NULL
-
-    def __repr__(self):
-        """ representation for debugging purposes """
-        if self.w_value is _NULL:
-            return "%s()" % self.__class__.__name__
-        else:
-            return "%s(%s)" % (self.__class__.__name__, self.w_value)
-
-class AppFrame(PyFrame):
-    """Represents a frame for a regular Python function
-    that needs to be interpreted.
-
-    Public fields:
-     * 'space' is the object space this frame is running in
-     * 'w_locals' is the locals dictionary to use
-     * 'w_globals' is the attached globals dictionary
-     * 'w_builtins' is the attached built-ins dictionary
-     * 'valuestack', 'blockstack', 'next_instr' control the interpretation
-    """
-
-    def __init__(self, space, bytecode, w_globals, argtuple):
-        self.space = space
-        self.bytecode = bytecode # Misnomer; this is really like a code object
-
-        # XXX we may want to have lazy access to interp-level through w_globals later
-        self.w_globals = w_globals
-
-        # XXX construct self.w_locals
-        self.nestedcells = ()
-        self.localcells = [ Cell(x) for x in argtuple ]
-        missing = self.bytecode.co_nlocals - len(self.localcells)
-        self.localcells.extend([ Cell() for x in range(missing)])
-        
-        self.w_builtins = self.load_builtins()
-        self.valuestack = Stack()
-        self.blockstack = Stack()
-        self.last_exception = None
-        self.next_instr = 0
-        #self._dump()
-
-    def _dump(self):
-        print "AppFrame_dump"
-        print "  space     ", self.space
-        print "  localcells", self.localcells
-        print "  co_varnames  ", self.bytecode.co_varnames
-

Modified: pypy/branch/builtinrefactor/pypy/interpreter/test/test_executioncontext.py
==============================================================================
--- pypy/branch/builtinrefactor/pypy/interpreter/test/test_executioncontext.py	(original)
+++ pypy/branch/builtinrefactor/pypy/interpreter/test/test_executioncontext.py	Sun Jul 27 20:53:08 2003
@@ -1,7 +1,7 @@
 import autopath
 
 from pypy.tool import test
-from pypy.interpreter.pyframe import PyFrame
+from pypy.interpreter.gateway import ScopedCode
 from pypy.interpreter import baseobjspace, executioncontext
 
 class TestExecutionContext(test.TestCase):
@@ -16,9 +16,9 @@
                                         space.wrap('exec'))).co_consts[0]
         w_globals = ec.make_standard_w_globals()
         w_locals = space.newdict([(space.wrap('x'), space.wrap(5))])
-        frame = PyFrame(space, bytecode, w_globals, w_locals)
-        w_output = ec.eval_frame(frame)
-        self.assertEquals(frame.space.unwrap(w_output), 6)
+        scopedcode = ScopedCode(space, bytecode, w_globals)
+        w_output = scopedcode.eval_frame(w_locals)
+        self.assertEquals(space.unwrap(w_output), 6)
 
 
 if __name__ == '__main__':

Deleted: /pypy/branch/builtinrefactor/pypy/interpreter/test/test_extmodule.py
==============================================================================
--- /pypy/branch/builtinrefactor/pypy/interpreter/test/test_extmodule.py	Sun Jul 27 20:53:08 2003
+++ (empty file)
@@ -1,128 +0,0 @@
-import autopath
-
-# need pypy.module.builtin first to make other imports work (???)
-from pypy.module import builtin
-from pypy.interpreter import extmodule
-from pypy.tool import test
-
-import unittest
-
-class EmptyBM(extmodule.BuiltinModule):
-    __pythonname__ = 'empty_bm'
-
-class BM_with_appmethod(extmodule.BuiltinModule):
-    __pythonname__ = 'bm_with_appmethod'
-    def amethod(self): return 23
-    amethod = extmodule.appmethod(amethod)
-
-class BM_with_appdata(extmodule.BuiltinModule):
-    __pythonname__ = 'bm_with_appdata'
-    somedata = 'twentythree'
-    somedata = extmodule.appdata(somedata)
-
-class TestBuiltinModule(test.TestCase):
-
-    def setUp(self):
-        self.space = test.objspace()
-
-    def tearDown(self):
-        pass
-
-    def test_empty(self):
-        space = self.space
-        bm = EmptyBM(space)
-        w_bm = bm.wrap_me()
-        w_bmd = space.getattr(w_bm, space.wrap('__dict__'))
-        bmd = space.unwrap(w_bmd)
-        self.assertEqual(bmd,
-            {'__doc__': EmptyBM.__doc__,
-            '__name__': EmptyBM.__pythonname__} )
-
-    def test_appmethod(self):
-        space = self.space
-        bm = BM_with_appmethod(space)
-        w_bm = bm.wrap_me()
-        w_bmd = space.getattr(w_bm, space.wrap('__dict__'))
-        w_method = space.getitem(w_bmd, space.wrap('amethod'))
-        w_doc = space.getitem(w_bmd, space.wrap("__doc__"))
-        w_name = space.getitem(w_bmd, space.wrap("__name__"))
-        self.assertEqual(space.unwrap(w_doc), BM_with_appmethod.__doc__)
-        self.assertEqual(space.unwrap(w_name), BM_with_appmethod.__pythonname__)
-        result = space.call(w_method, space.wrap(()), space.wrap({}))
-        self.assertEqual(result, 23)
-
-    def test_appdata(self):
-        space = self.space
-        bm = BM_with_appdata(space)
-        w_bm = bm.wrap_me()
-        w_bmd = space.getattr(w_bm, space.wrap('__dict__'))
-        bmd = space.unwrap(w_bmd)
-        thedata = bmd.get('somedata')
-        self.assertNotEqual(thedata, None)
-        w_data = space.getitem(w_bmd, space.wrap('somedata'))
-        bmd['somedata'] = BM_with_appdata.somedata
-        self.assertEqual(bmd,
-            {'__doc__': BM_with_appdata.__doc__,
-            '__name__': BM_with_appdata.__pythonname__,
-            'somedata': BM_with_appdata.somedata} )
-        self.assertEqual(thedata, 'twentythree')
-
-
-class TestPyBuiltinCode(test.TestCase):
-
-    def setUp(self):
-        self.space = test.objspace()
-
-    def tearDown(self):
-        pass
-
-    def test_simple(self):
-        def f(w_x):
-            return w_x
-        builtin_f = extmodule.make_builtin_func(self.space, f)
-        w_input = self.space.wrap(42)
-        w_res = self.space.call_function(builtin_f, w_input)
-        self.assertEqual_w(w_res, w_input)
-
-    def test_default(self):
-        space = self.space
-        w = space.wrap
-        def f(w_x, w_y=23):
-            return space.add(w_x, w_y)
-        builtin_f = extmodule.make_builtin_func(space, f)
-        w_input = w(42)
-        w_res = space.call_function(builtin_f, w_input)
-        self.assertEqual_w(w_res, w(65))
-        w_res = space.call_function(builtin_f, w_input, w(100))
-        self.assertEqual_w(w_res, w(142))
-
-    def test_varargs(self):
-        s = self.space
-        w = s.wrap
-        def f(w_first, *args_w):
-            w_r = w_first
-            for w_i in args_w:
-                w_r = s.add(w_r, w_i)
-            return w_r
-        builtin_f = extmodule.make_builtin_func(s, f)        
-        self.assertEqual_w(s.call_function(builtin_f, w(1)), w(1))
-        self.assertEqual_w(s.call_function(builtin_f, w(1), w(2), w(3)), w(6))
-
-    def test_kwargs(self):
-        s = self.space
-        w = s.wrap
-        def f(**kws_w):
-            return s.add(kws_w['a'], kws_w['b'])
-        builtin_f = extmodule.make_builtin_func(s, f)
-        self.assertEqual_w(s.call_function(builtin_f, a=w(1), b=w(2)), w(3))
-
-    def test_varkwargs(self):
-        s = self.space
-        w = s.wrap
-        def f(*args_w, **kws_w):
-            return s.add(args_w[0], kws_w['a'])
-        builtin_f = extmodule.make_builtin_func(s, f)
-        self.assertEqual_w(s.call_function(builtin_f, w(2), a=w(1)), w(3))
-
-if __name__ == '__main__':
-    test.main()

Added: pypy/branch/builtinrefactor/pypy/interpreter/test/test_function.py
==============================================================================
--- (empty file)
+++ pypy/branch/builtinrefactor/pypy/interpreter/test/test_function.py	Sun Jul 27 20:53:08 2003
@@ -0,0 +1,106 @@
+
+import autopath
+from pypy.tool import test 
+import unittest
+
+class ArgParseTest(test.AppTestCase):
+    def test_simple_call(self):
+        def func(arg1, arg2):
+            return arg1, arg2
+        res = func(23,42)
+        self.assertEquals(res[0], 23)
+        self.assertEquals(res[1], 42)
+
+    def test_simple_varargs(self):
+        def func(arg1, *args):
+            return arg1, args
+        res = func(23,42)
+        self.assertEquals(res[0], 23)
+        self.assertEquals(res[1], (42,))
+
+    def test_simple_kwargs(self):
+        def func(arg1, **kwargs):
+            return arg1, kwargs
+        res = func(23, value=42)
+        self.assertEquals(res[0], 23)
+        self.assertEquals(res[1], {'value': 42})
+
+    def test_kwargs_sets_wrong_positional_raises(self):
+        def func(arg1):
+            pass
+        self.assertRaises(TypeError, func, arg2=23)
+
+    def test_kwargs_sets_positional(self):
+        def func(arg1):
+            return arg1
+        res = func(arg1=42)
+        self.assertEquals(res, 42)
+
+    def test_kwargs_sets_positional_mixed(self):
+        def func(arg1, **kw):
+            return arg1, kw
+        res = func(arg1=42, something=23)
+        self.assertEquals(res[0], 42)
+        self.assertEquals(res[1], {'something': 23})
+
+    def test_kwargs_sets_positional_mixed(self):
+        def func(arg1, **kw):
+            return arg1, kw
+        res = func(arg1=42, something=23)
+        self.assertEquals(res[0], 42)
+        self.assertEquals(res[1], {'something': 23})
+
+    def test_kwargs_sets_positional_twice(self):
+        def func(arg1, **kw):
+            return arg1, kw
+        self.assertRaises(
+            TypeError, func, 42, {'arg1': 23})
+
+    def test_default_arg(self):
+        def func(arg1,arg2=42):
+            return arg1, arg2
+        res = func(arg1=23)
+        self.assertEquals(res[0], 23)
+        self.assertEquals(res[1], 42)
+
+    def test_defaults_keyword_overrides(self):
+        def func(arg1=42, arg2=23):
+            return arg1, arg2
+        res = func(arg1=23)
+        self.assertEquals(res[0], 23)
+        self.assertEquals(res[1], 23)
+
+    def test_defaults_keyword_override_but_leaves_empty_positional(self):
+        def func(arg1,arg2=42):
+            return arg1, arg2
+        self.assertRaises(TypeError, func, arg2=23)
+
+    def test_kwargs_disallows_same_name_twice(self):
+        def func(arg1, **kw):
+            return arg1, kw
+        self.assertRaises(TypeError, func, 42, **{'arg1': 23})
+
+class ModuleMinimalTest(test.IntTestCase):
+    def setUp(self):
+        self.space = test.objspace()
+
+    def test_sys_exists(self):
+        w_sys = self.space.get_builtin_module('sys')
+        self.assert_(self.space.is_true(w_sys))
+
+    def test_import_exists(self):
+        space = self.space
+        w_builtin = space.get_builtin_module('__builtin__')
+        self.assert_(space.is_true(w_builtin))
+        w_name = space.wrap('__import__')
+        w_import = self.space.getattr(w_builtin, w_name)
+        self.assert_(space.is_true(w_import))
+
+    def test_sys_import(self):
+        from pypy.interpreter.main import run_string
+        run_string('import sys', space=self.space)
+
+if __name__ == '__main__':
+    test.main()
+
+        

Modified: pypy/branch/builtinrefactor/pypy/interpreter/test/test_interpreter.py
==============================================================================
--- pypy/branch/builtinrefactor/pypy/interpreter/test/test_interpreter.py	(original)
+++ pypy/branch/builtinrefactor/pypy/interpreter/test/test_interpreter.py	Sun Jul 27 20:53:08 2003
@@ -7,7 +7,7 @@
     def codetest(self, source, functionname, args):
         """Compile and run the given code string, and then call its function
         named by 'functionname' with arguments 'args'."""
-        from pypy.interpreter import baseobjspace, executioncontext, pyframe
+        from pypy.interpreter import baseobjspace, executioncontext, pyframe, gateway
         space = self.space
 
         compile = space.builtin.compile
@@ -19,9 +19,9 @@
         w_tempmodule = space.newmodule(w("__temp__"))
         w_glob = space.getattr(w_tempmodule, w("__dict__"))
         space.setitem(w_glob, w("__builtins__"), space.w_builtins)
-        
-        frame = pyframe.PyFrame(space, space.unwrap(w_code), w_glob, w_glob)
-        ec.eval_frame(frame)
+       
+        scopedcode = gateway.ScopedCode(space, space.unwrap(w_code), w_glob)
+        scopedcode.eval_frame()
 
         wrappedargs = w(args)
         wrappedfunc = space.getitem(w_glob, w(functionname))

Modified: pypy/branch/builtinrefactor/pypy/interpreter/test/test_objspace.py
==============================================================================
--- pypy/branch/builtinrefactor/pypy/interpreter/test/test_objspace.py	(original)
+++ pypy/branch/builtinrefactor/pypy/interpreter/test/test_objspace.py	Sun Jul 27 20:53:08 2003
@@ -67,12 +67,11 @@
         # unavoidable
         def f(x):
             return x
-        from pypy.interpreter.pycode import PyByteCode
-        c = PyByteCode()
-        c._from_code(f.func_code)
+
+        cpycode = f.func_code
         w_globals = self.space.newdict([])
         w_defs = self.space.newtuple([])
-        w_f = self.space.newfunction(c, w_globals, w_defs)
+        w_f = self.space.newfunction(cpycode, w_globals, w_defs)
         self.assertEqual_w(self.space.call_function(w_f, self.space.wrap(1)),
                            self.space.wrap(1))
     

Deleted: /pypy/branch/builtinrefactor/pypy/interpreter/test/test_pyframe_app.py
==============================================================================
--- /pypy/branch/builtinrefactor/pypy/interpreter/test/test_pyframe_app.py	Sun Jul 27 20:53:08 2003
+++ (empty file)
@@ -1,92 +0,0 @@
-import autopath
-
-from pypy.tool import test
-from pypy.interpreter.pyframe_app import decode_frame_arguments
-
-class CodeObject:
-    """ A code object class for test purposes. """
-    def __init__(self, count=3, vflag = 0, kflag = 0):
-        self.co_argcount = count
-        self.co_varnames = ('a', 'b', 'c', 'd', 'e')
-        self.co_flags = 4 * vflag + 8 * kflag
-
-
-class Testdecode_frame_arguments(test.TestCase):
-    
-    def test_plain_parameters(self):
-        self.assertEqual(
-            (1, 2, 3, (4,)),
-            decode_frame_arguments((1, 2, 3, 4), {}, (), None,
-                                   CodeObject(vflag=1)))
-
-    def test_non_used_keyword(self):
-        self.assertEqual(
-            (1, 2, 3, (4,), {'e':0}),
-            decode_frame_arguments((1, 2, 3, 4), {'e':0}, (), None,
-                                   CodeObject(vflag=1, kflag=1)))
-
-    def test_used_keyword(self):
-        self.assertEqual(
-            (1, 2, 10, ()),
-            decode_frame_arguments((1, 2), {'c':10}, (20,), None,
-                                   CodeObject(vflag=1)))
-
-    def test_mixed_keyword(self):
-        self.assertEqual(
-            (1, 2, 10, (), {'e':30}),
-            decode_frame_arguments((1, 2), {'c':10, 'e':30}, (20,), None,
-                                   CodeObject(vflag=1, kflag=1)))
-
-    def test_used_default(self):
-        self.assertEqual(
-            (1, 2, 20),
-            decode_frame_arguments((1, 2), {}, (20,), None, CodeObject()))
-
-    def test_no_varargs(self):
-        self.assertEqual(
-            (20, 30, 40),
-            decode_frame_arguments((), {}, (20, 30, 40), None, CodeObject()))
-
-    def test_no_args(self):
-        self.assertEqual(
-            (),
-            decode_frame_arguments((), {}, (), None, CodeObject(count=0)))
-
-    def test_fail_keywords_has_bad_formal_parameter(self):
-        self.assertRaises(
-            TypeError,
-            decode_frame_arguments,
-            (1, 2, 3), {'xxx':666}, (), None, CodeObject())
-
-    def test_fail_too_many_parameters(self):
-        self.assertRaises(
-            TypeError,
-            decode_frame_arguments,
-            (1, 2, 3, 4, 5, 6), {}, (), None, CodeObject())
-
-    def test_fail_not_enough_parameters(self):
-        self.assertRaises(
-            TypeError,
-            decode_frame_arguments,
-            (), {}, (), None, CodeObject())
-
-    def test_fail_extra_no_varargs(self):
-        self.assertRaises(
-            TypeError,
-            decode_frame_arguments,
-            (1, 2, 3, 4), {}, (20,), None, CodeObject(vflag=0))
-
-    def test_fail_setting_parameter_twice_with_extra_actual_keyword(self):
-        self.assertRaises(
-            TypeError,
-            decode_frame_arguments,
-            (1, 2, 3, 4), {'a':666, 'b':666}, (), None, CodeObject())
-
-    def test_fail_setting_parameter_twice_with_extra_formal_keyword(self):
-        self.assertRaises(
-            TypeError,
-            decode_frame_arguments,
-            (1, 2, 3, 4), {}, (), None, CodeObject(kflag=1))
-        
-if __name__ == "__main__":
-    test.main()   

Modified: pypy/branch/builtinrefactor/pypy/interpreter/unittest_w.py
==============================================================================
--- pypy/branch/builtinrefactor/pypy/interpreter/unittest_w.py	(original)
+++ pypy/branch/builtinrefactor/pypy/interpreter/unittest_w.py	Sun Jul 27 20:53:08 2003
@@ -4,18 +4,14 @@
 import unittest
 
 def wrap_func(space, func):
-    # this is generally useful enough that it should probably go
-    # somewhere more obvious (objspace method?)
-    from pypy.interpreter import pycode
-    code = pycode.PyByteCode()
-    code._from_code(func.func_code)
-    return space.newfunction(code, space.newdict([]),
-                             space.wrap(func.func_defaults), None)
+    from pypy.interpreter.gateway import InterpretedFunction
+    func = InterpretedFunction(space, func)
+    return space.wrap(func)
 
 def make_testcase_class(space, tc_w):
     # XXX this is all a bit insane (but it works)
     
-    from pypy.interpreter.extmodule import make_builtin_func
+    #from pypy.interpreter.extmodule import make_builtin_func
     w = space.wrap
     d = space.newdict([])
     space.setitem(d, w('failureException'), space.w_AssertionError)
@@ -40,7 +36,6 @@
 
     def __call__(self):
         from pypy.interpreter import executioncontext
-        from pypy.interpreter import pyframe
         space = self.testCase.space
         w = space.wrap
 
@@ -127,7 +122,7 @@
         return self.failIf(condition, msg)
 
     def assertRaises_w(self, w_exc_class, callable, *args, **kw):
-        from pypy.objspace.std.objspace import OperationError
+        from pypy.interpreter.baseobjspace import OperationError
         try:
             callable(*args, **kw)
         except OperationError, e:

Modified: pypy/branch/builtinrefactor/pypy/module/builtin.py
==============================================================================
--- pypy/branch/builtinrefactor/pypy/module/builtin.py	(original)
+++ pypy/branch/builtinrefactor/pypy/module/builtin.py	Sun Jul 27 20:53:08 2003
@@ -1,5 +1,6 @@
-from pypy.interpreter.extmodule import *
-from pypy.interpreter import pycode, executioncontext
+from pypy.interpreter import executioncontext
+from pypy.interpreter.gateway import \
+     AppVisibleModule, ScopedCode, wrap_applevel_class
 
 #######################
 ####  __builtin__  ####
@@ -7,55 +8,67 @@
 
 import __builtin__ as cpy_builtin
 
-class Builtin(BuiltinModule):
-    __pythonname__ = '__builtin__'
+class __builtin__(AppVisibleModule):
 
     def _actframe(self, index=-1):
         return self.space.getexecutioncontext().framestack.items[index]
 
+    def _wrap_postponed(self):
+        """ stuff that needs a mostly working interpreter goes here.
+
+        The AppVisibleModule init will put all objects into '_postponed'
+        that need to be wrapped now.  Currently this should only be
+        the 'xrange' class. 
+        """
+        for name, obj in self._postponed:
+            # for now this can only be a class
+            w_res = wrap_applevel_class(self.space, name, obj)
+            w_name = self.space.wrap(name)
+            self.space.setattr(self._wrapped, w_name, w_res)
+
     def globals(self):
         return self._actframe().w_globals
-    globals = appmethod(globals)
 
     def locals(self):
         return self._actframe().w_locals
-    locals = appmethod(locals)
-
 
     def __import__(self, w_modulename, w_locals, w_globals, w_fromlist):
         space = self.space
         w = space.wrap
         try:
-            w_mod = space.getitem(space.w_modules, w_modulename)
+            w_mod = space.getitem(space.sys.w_modules, w_modulename)
             return w_mod
         except executioncontext.OperationError,e:
             if not e.match(space, space.w_KeyError):
                 raise
             w_mod = space.get_builtin_module(w_modulename)
             if w_mod is not None:
-                space.setitem(space.w_modules,w_modulename,w_mod)
+                space.setitem(space.sys.w_modules, w_modulename, w_mod)
                 return w_mod
 
             import os, __future__
-            for path in space.unwrap(space.getattr(space.w_sys, w('path'))):
+            for path in space.unwrap(space.sys.w_path):
                 f = os.path.join(path, space.unwrap(w_modulename) + '.py')
                 if os.path.exists(f):
                     w_mod = space.newmodule(w_modulename)
-                    space.setitem(space.w_modules, w_modulename, w_mod)
+                    space.setitem(space.sys.w_modules, w_modulename, w_mod)
                     space.setattr(w_mod, w('__file__'), w(f))
                     w_source = w(open(f, 'r').read())
                     # wrt the __future__.generators.compiler_flag, "um" -- mwh
                     w_code = self.compile(w_source, w(f), w('exec'),
                                           w(__future__.generators.compiler_flag))
                     w_dict = space.getattr(w_mod, w('__dict__'))
-                    space.unwrap(w_code).eval_code(space, w_dict, w_dict)
+
+                    code = space.unwrap(w_code)
+                    from pypy.interpreter.gateway import ScopedCode
+                    scopedcode = ScopedCode(space, code, w_dict)
+                    scopedcode.eval_frame()
 
                     return w_mod
             
             w_exc = space.call_function(space.w_ImportError, w_modulename)
             raise executioncontext.OperationError(
                       space.w_ImportError, w_exc)
-    __import__ = appmethod(__import__)
 
     def compile(self, w_str, w_filename, w_startstr,
                 w_supplied_flags=None, w_dont_inherit=None):
@@ -78,10 +91,7 @@
 
         #print (str, filename, startstr, supplied_flags, dont_inherit)
         c = cpy_builtin.compile(str, filename, startstr, supplied_flags, dont_inherit)
-        res = pycode.PyByteCode()
-        res._from_code(c)
-        return space.wrap(res)
-    compile = appmethod(compile)
+        return space.wrap(c)
 
     def execfile(self, w_filename, w_globals=None, w_locals=None):
         space = self.space
@@ -95,79 +105,325 @@
         filename = space.unwrap(w_filename)
         s = open(filename).read()
         c = cpy_builtin.compile(s, filename, 'exec', 4096) # XXX generators 
-        res = pycode.PyByteCode()
-        res._from_code(c)
-
-        res.eval_code(space, w_globals, w_locals)
-        return space.w_None
-
-    execfile = appmethod(execfile)
 
 
+        scopedcode = ScopedCode(space, c, w_globals)
+        scopedcode.eval_frame(w_locals)
+        return space.w_None
 
     ####essentially implemented by the objectspace
     def abs(self, w_val):
         return self.space.abs(w_val)
-    abs = appmethod(abs)
 
     def chr(self, w_ascii):
         w_character = self.space.newstring([w_ascii])
         return w_character
-    chr = appmethod(chr)
 
     def len(self, w_obj):
         return self.space.len(w_obj)
-    len = appmethod(len)
 
     def delattr(self, w_object, w_name):
         return self.space.delattr(w_object, w_name)
-    delattr = appmethod(delattr)
 
     def getattr(self, w_object, w_name):
         return self.space.getattr(w_object, w_name)
-    getattr = appmethod(getattr)
-
 
     def hash(self, w_object):
         return self.space.hash(w_object)
-    hash = appmethod(hash)
 
     def oct(self, w_val):
         return self.space.oct(w_val)
-    oct = appmethod(oct)
 
     def hex(self, w_val):
         return self.space.hex(w_val)
-    hex = appmethod(hex)
-
 
     def id(self, w_object):
         return self.space.id(w_object)
-    id = appmethod(id)
 
     #XXX works only for new-style classes.
     #So we have to fix it, when we add support for old-style classes
     def issubclass(self, w_cls1, w_cls2):
         return self.space.issubtype(w_cls1, w_cls2)
-    issubclass = appmethod(issubclass)
 
     #XXX missing: second form of iter (callable, sentintel) 
     def iter(self, w_collection):
         return self.space.iter(w_collection)
-    iter = appmethod(iter)
 
     def ord(self, w_val):
         return self.space.ord(w_val)
-    ord = appmethod(ord)
 
     def pow(self, w_val):
         return self.space.pow(w_val)
-    pow = appmethod(pow)
 
     def repr(self, w_object):
         return self.space.repr(w_object)
-    repr = appmethod(repr)
 
     def setattr(self, w_object, w_name, w_val):
         return self.space.setattr(w_object, w_name, w_val)
-    setattr = appmethod(setattr)
+
+    # app-level functions
+
+    def app_apply(function, args, kwds={}):
+        """call a function (or other callable object) and return its result"""
+        return function(*args, **kwds)
+
+    def app_map(function, *collections):
+        """does 3 separate things, hence this enormous docstring.
+           1.  if function is None, return a list of tuples, each with one
+               item from each collection.  If the collections have different
+               lengths,  shorter ones are padded with None.
+
+           2.  if function is not None, and there is only one collection,
+               apply function to every item in the collection and return a
+               list of the results.
+
+           3.  if function is not None, and there are several collections,
+               repeatedly call the function with one argument from each
+               collection.  If the collections have different lengths,
+               shorter ones are padded with None"""
+
+        if len(collections) == 0:
+            raise TypeError, "map() requires at least one sequence"
+
+        elif len(collections) == 1:
+           #it's the most common case, so make it faster
+           if function is None:
+              return collections[0]
+           else:
+              return [function(x) for x in collections[0]]
+        else:
+           res = []
+           idx = 0   
+           while 1:
+              cont = 0     #is any collection not empty?
+              args = []
+              for collection in collections:
+                  try:
+                     elem = collection[idx]
+                     cont = cont + 1
+                  except IndexError:
+                     elem = None
+                  args.append(elem)
+              if cont:
+                  if function is None:
+                     res.append(tuple(args))
+                  else:
+                     res.append(function(*args))
+              else:
+                  return res
+              idx = idx + 1
+
+    def app_filter(function, collection):
+        """construct a list of those elements of collection for which function
+           is True.  If function is None, then return the items in the sequence
+           which are True."""
+
+        if function is None:
+            res = [item for item in collection if item]
+        else:
+            res = [item for item in collection if function(item)]
+
+        if type(collection) is tuple:
+           return tuple(res)
+        elif type(collection) is str:
+           return "".join(res)
+        else:
+           return res
+
+    def app_zip(*collections):
+        """return a list of tuples, where the nth tuple contains every
+           nth item of each collection.  If the collections have different
+           lengths, zip returns a list as long as the shortest collection,
+           ignoring the trailing items in the other collections."""
+
+        if len(collections) == 0:
+           raise TypeError, "zip() requires at least one sequence"
+        res = []
+        idx = 0
+        while 1:
+           try:
+              elems = []
+              for collection in collections:
+                 elems.append(collection[idx])
+              res.append(tuple(elems))
+           except IndexError:
+              break
+           idx = idx + 1
+        return res
+
+    def app_reduce(function, l, *initialt):
+        """ Apply function of two arguments cumulatively to the items of
+            sequence, from left to right, so as to reduce the sequence to a
+            single value.  Optionally begin with an initial value."""
+
+        if initialt:
+           initial, = initialt
+           idx = 0
+        else:
+           try:
+              initial = l[0]
+           except IndexError:
+              raise TypeError, "reduce() of empty sequence with no initial value"
+           idx = 1
+        while 1:
+           try:
+             initial = function(initial, l[idx])
+             idx = idx + 1
+           except IndexError:
+             break
+        return initial
+
+    def app_isinstance(obj, klass_or_tuple):
+        objcls = obj.__class__
+        if issubclass(klass_or_tuple.__class__, tuple):
+           for klass in klass_or_tuple:
+               if issubclass(objcls, klass):
+                  return 1
+           return 0
+        else:
+           try:
+               return issubclass(objcls, klass_or_tuple)
+           except TypeError:
+               raise TypeError, "isinstance() arg 2 must be a class or type"
+
+    def app_range(x, y=None, step=1):
+        """ returns a list of integers in arithmetic position from start (defaults
+            to zero) to stop - 1 by step (defaults to 1).  Use a negative step to
+            get a list in decending order."""
+
+        if y is None: 
+                start = 0
+                stop = x
+        else:
+                start = x
+                stop = y
+
+        if step == 0:
+            raise ValueError, 'range() arg 3 must not be zero'
+
+        elif step > 0:
+            if stop <= start: # no work for us
+                return []
+            howmany = (stop - start + step - 1)/step
+
+        else:  # step must be < 0, or we would have raised ValueError
+            if stop >= start: # no work for us
+                return []
+            howmany = (start - stop - step  - 1)/-step
+
+        arr = [None] * howmany  # this is to avoid using append.
+
+        i = start
+        n = 0
+        while n < howmany:
+            arr[n] = i
+            i += step
+            n += 1
+
+        return arr
+
+    # min and max could be one function if we had operator.__gt__ and
+    # operator.__lt__  Perhaps later when we have operator.
+
+    def app_min(*arr):
+        """return the smallest number in a list"""
+
+        if not arr:
+            raise TypeError, 'min() takes at least one argument'
+
+        if len(arr) == 1:
+            arr = arr[0]
+
+        iterator = iter(arr)
+        try:
+            min = iterator.next()
+        except StopIteration:
+            raise ValueError, 'min() arg is an empty sequence'
+
+        for i in iterator:
+            if min > i:
+                min = i
+        return min
+
+    def app_max(*arr):
+        """return the largest number in a list"""
+
+        if not arr:
+            raise TypeError, 'max() takes at least one argument'
+
+        if len(arr) == 1:
+            arr = arr[0]
+
+        iterator = iter(arr)
+        try:
+            max = iterator.next()
+        except StopIteration:
+            raise ValueError, 'max() arg is an empty sequence'
+
+        for i in iterator:
+            if max < i:
+                max = i
+        return max
+
+
+    def app_cmp(x, y):
+        """return 0 when x == y, -1 when x < y and 1 when x > y """
+        if x < y:
+            return -1
+        elif x == y:
+            return 0
+        else:
+            return 1
+
+    def app__vars(*obj):
+        """return a dictionary of all the attributes currently bound in obj.  If
+        called with no argument, return the variables bound in local scope."""
+
+        if len(obj) == 0:
+            return locals()
+        elif len(obj) != 1:
+            raise TypeError, "vars() takes at most 1 argument."
+        else:
+            try:
+                return obj[0].__dict__
+            except AttributeError:
+                raise TypeError, "vars() argument must have __dict__ attribute"
+
+    def app_hasattr(ob, attr):
+        try:
+            getattr(ob, attr)
+            return True
+        except AttributeError:
+            return False
+
+
+    class app_xrange:
+        def __init__(self, x, y=None, step=1):
+            """ returns an xrange object, see range for more docs"""
+
+            if y is None: 
+                self.start = 0
+                self.stop = x
+            else:
+                self.start = x
+                self.stop = y
+
+            if step == 0:
+                raise ValueError, 'xrange() step-argument (arg 3) must not be zero'
+
+            self.step = step
+
+        def __iter__(self):
+            def gen(self):
+                start,stop,step = self.start,self.stop,self.step
+                i = start
+                if step > 0:
+                    while i < stop:
+                        yield i
+                        i+=step
+                else:
+                    while i > stop:
+                        yield i
+                        i+=step
+
+            return gen(self)

Deleted: /pypy/branch/builtinrefactor/pypy/module/builtin_app.py
==============================================================================
--- /pypy/branch/builtinrefactor/pypy/module/builtin_app.py	Sun Jul 27 20:53:08 2003
+++ (empty file)
@@ -1,263 +0,0 @@
-def apply(function, args, kwds={}):
-    """call a function (or other callable object) and return its result"""
-    return function(*args, **kwds)
-
-def map(function, *collections):
-    """does 3 separate things, hence this enormous docstring.
-       1.  if function is None, return a list of tuples, each with one
-           item from each collection.  If the collections have different
-           lengths,  shorter ones are padded with None.
-
-       2.  if function is not None, and there is only one collection,
-           apply function to every item in the collection and return a
-           list of the results.
-
-       3.  if function is not None, and there are several collections,
-           repeatedly call the function with one argument from each
-           collection.  If the collections have different lengths,
-           shorter ones are padded with None"""
-
-    if len(collections) == 0:
-        raise TypeError, "map() requires at least one sequence"
-    
-    elif len(collections) == 1:
-       #it's the most common case, so make it faster
-       if function is None:
-          return collections[0]
-       else:
-          return [function(x) for x in collections[0]]
-    else:
-       res = []
-       idx = 0   
-       while 1:
-          cont = 0     #is any collection not empty?
-          args = []
-          for collection in collections:
-              try:
-                 elem = collection[idx]
-                 cont = cont + 1
-              except IndexError:
-                 elem = None
-              args.append(elem)
-          if cont:
-              if function is None:
-                 res.append(tuple(args))
-              else:
-                 res.append(function(*args))
-          else:
-              return res
-          idx = idx + 1
-
-def filter(function, collection):
-    """construct a list of those elements of collection for which function
-       is True.  If function is None, then return the items in the sequence
-       which are True."""
-     
-    if function is None:
-        res = [item for item in collection if item]
-    else:
-        res = [item for item in collection if function(item)]
-              
-    if type(collection) is tuple:
-       return tuple(res)
-    elif type(collection) is str:
-       return "".join(res)
-    else:
-       return res
-
-def zip(*collections):
-    """return a list of tuples, where the nth tuple contains every
-       nth item of each collection.  If the collections have different
-       lengths, zip returns a list as long as the shortest collection,
-       ignoring the trailing items in the other collections."""
-    
-    if len(collections) == 0:
-       raise TypeError, "zip() requires at least one sequence"
-    res = []
-    idx = 0
-    while 1:
-       try:
-          elems = []
-          for collection in collections:
-             elems.append(collection[idx])
-          res.append(tuple(elems))
-       except IndexError:
-          break
-       idx = idx + 1
-    return res
-
-def reduce(function, l, *initialt):
-    """ Apply function of two arguments cumulatively to the items of
-        sequence, from left to right, so as to reduce the sequence to a
-        single value.  Optionally begin with an initial value."""
-    
-    if initialt:
-       initial, = initialt
-       idx = 0
-    else:
-       try:
-          initial = l[0]
-       except IndexError:
-          raise TypeError, "reduce() of empty sequence with no initial value"
-       idx = 1
-    while 1:
-       try:
-         initial = function(initial, l[idx])
-         idx = idx + 1
-       except IndexError:
-         break
-    return initial
-    
-def isinstance(obj, klass_or_tuple):
-    objcls = obj.__class__
-    if issubclass(klass_or_tuple.__class__, tuple):
-       for klass in klass_or_tuple:
-           if issubclass(objcls, klass):
-              return 1
-       return 0
-    else:
-       try:
-           return issubclass(objcls, klass_or_tuple)
-       except TypeError:
-           raise TypeError, "isinstance() arg 2 must be a class or type"
- 
-def range(x, y=None, step=1):
-    """ returns a list of integers in arithmetic position from start (defaults
-        to zero) to stop - 1 by step (defaults to 1).  Use a negative step to
-        get a list in decending order."""
-
-    if y is None: 
-            start = 0
-            stop = x
-    else:
-            start = x
-            stop = y
-
-    if step == 0:
-        raise ValueError, 'range() arg 3 must not be zero'
-
-    elif step > 0:
-        if stop <= start: # no work for us
-            return []
-        howmany = (stop - start + step - 1)/step
-
-    else:  # step must be < 0, or we would have raised ValueError
-        if stop >= start: # no work for us
-            return []
-        howmany = (start - stop - step  - 1)/-step
-       
-    arr = [None] * howmany  # this is to avoid using append.
-
-    i = start
-    n = 0
-    while n < howmany:
-        arr[n] = i
-        i += step
-        n += 1
-
-    return arr
-
-# min and max could be one function if we had operator.__gt__ and
-# operator.__lt__  Perhaps later when we have operator.
-
-def min(*arr):
-    """return the smallest number in a list"""
-
-    if not arr:
-        raise TypeError, 'min() takes at least one argument'
-
-    if len(arr) == 1:
-        arr = arr[0]
-     
-    iterator = iter(arr)
-    try:
-        min = iterator.next()
-    except StopIteration:
-        raise ValueError, 'min() arg is an empty sequence'
-    
-    for i in iterator:
-        if min > i:
-            min = i
-    return min
-
-def max(*arr):
-    """return the largest number in a list"""
-
-    if not arr:
-        raise TypeError, 'max() takes at least one argument'
-
-    if len(arr) == 1:
-        arr = arr[0]
-
-    iterator = iter(arr)
-    try:
-        max = iterator.next()
-    except StopIteration:
-        raise ValueError, 'max() arg is an empty sequence'
-
-    for i in iterator:
-        if max < i:
-            max = i
-    return max
-
-
-def cmp(x, y):
-    """return 0 when x == y, -1 when x < y and 1 when x > y """
-    if x < y:
-        return -1
-    elif x == y:
-        return 0
-    else:
-        return 1
-
-def _vars(*obj):
-    """return a dictionary of all the attributes currently bound in obj.  If
-    called with no argument, return the variables bound in local scope."""
-
-    if len(obj) == 0:
-        return locals()
-    elif len(obj) != 1:
-        raise TypeError, "vars() takes at most 1 argument."
-    else:
-        try:
-            return obj[0].__dict__
-        except AttributeError:
-            raise TypeError, "vars() argument must have __dict__ attribute"
-
-def hasattr(ob, attr):
-    try:
-        getattr(ob, attr)
-        return True
-    except AttributeError:
-        return False
-
-class xrange:
-    def __init__(self, x, y=None, step=1):
-        """ returns an xrange object, see range for more docs"""
-
-        if y is None: 
-            self.start = 0
-            self.stop = x
-        else:
-            self.start = x
-            self.stop = y
-
-        if step == 0:
-            raise ValueError, 'xrange() step-argument (arg 3) must not be zero'
-
-        self.step = step
-
-    def __iter__(self):
-        def gen(self):
-            start,stop,step = self.start,self.stop,self.step
-            i = start
-            if step > 0:
-                while i < stop:
-                    yield i
-                    i+=step
-            else:
-                while i > stop:
-                    yield i
-                    i+=step
-
-        return gen(self)

Deleted: /pypy/branch/builtinrefactor/pypy/module/builtin_helper_app.py
==============================================================================

Modified: pypy/branch/builtinrefactor/pypy/module/sysmodule.py
==============================================================================
--- pypy/branch/builtinrefactor/pypy/module/sysmodule.py	(original)
+++ pypy/branch/builtinrefactor/pypy/module/sysmodule.py	Sun Jul 27 20:53:08 2003
@@ -1,23 +1,28 @@
 from pypy.interpreter.baseobjspace import OperationError
-from pypy.interpreter.extmodule import *
-import sys, os, pypy
+from pypy.interpreter.gateway import AppVisibleModule
+import os, pypy
 
-class Sys(BuiltinModule):
-    __pythonname__ = 'sys'
+import sys as cpy_sys
 
-    def __init__(self, space):
-        BuiltinModule.__init__(self, space)
+class sys(AppVisibleModule):
+    """ A Minimal 'sys' module.
 
+    Currently we only provide 'stdout' and 'displayhook'
+    """
 
+    def __init__(self, space):
         opd = os.path.dirname
-        
         pypydir = opd(opd(os.path.abspath(pypy.__file__)))
-        
         appdir = os.path.join(pypydir, 'pypy', 'appspace')
-
-        self.path = appdata([appdir] + [p for p in sys.path if p != pypydir])
-    
-    stdout = appdata(sys.stdout)
+        self.path = [appdir] + [p for p in cpy_sys.path if p!= pypydir]
+        self.w_modules = space.newdict([])
+        AppVisibleModule.__init__(self, space)
+   
+    stdout = cpy_sys.stdout 
+
+    def _setmodule(self, module):
+        """ put a module into the modules list """
+        self.space.setitem(self.w_modules, module.w___name__, module._wrapped)
 
     def displayhook(self, w_x):
         space = self.space
@@ -28,5 +33,3 @@
             except OperationError:
                 print "! could not print", w_x
             space.setitem(space.w_builtins, w('_'), w_x)
-    displayhook = appmethod(displayhook)
-    

Modified: pypy/branch/builtinrefactor/pypy/objspace/ann/cloningcontext.py
==============================================================================
--- pypy/branch/builtinrefactor/pypy/objspace/ann/cloningcontext.py	(original)
+++ pypy/branch/builtinrefactor/pypy/objspace/ann/cloningcontext.py	Sun Jul 27 20:53:08 2003
@@ -72,6 +72,11 @@
         # {(bytecode, w_globals): FunctionInfo(), ...}
 
     def getfunctioninfo(self, frame, new=False):
+        from pypy.interpreter.pyframe import AppFrame, PyFrame
+        if isinstance(frame, AppFrame):
+            print "getfunctioninfo from AppFrame", frame.bytecode.co_name
+            return FunctionInfo(self)
+
         key = self.makekey(frame)
         info = self.functioninfos.get(key)
         if info is None:
@@ -117,7 +122,10 @@
         return w_result
 
     def clone_frame(self, frame):
-        f = PyFrame(self.space, frame.bytecode, frame.w_globals, frame.w_locals)
+        return frame.clone()
+
+        f = frame.clone()
+        #f = PyFrame(self.space, frame.bytecode, frame.w_globals, frame.w_locals)
         f.valuestack = clonevaluestack(frame.valuestack)
         f.blockstack = cloneblockstack(frame.blockstack)
         f.last_exception = frame.last_exception

Modified: pypy/branch/builtinrefactor/pypy/objspace/ann/objspace.py
==============================================================================
--- pypy/branch/builtinrefactor/pypy/objspace/ann/objspace.py	(original)
+++ pypy/branch/builtinrefactor/pypy/objspace/ann/objspace.py	Sun Jul 27 20:53:08 2003
@@ -90,7 +90,8 @@
     def createexecutioncontext(self):
         return CloningExecutionContext(self)
 
-    def gethelperspace(self):
+    #XXX disabled the helperspace-concept for now
+    def _gethelperspace(self):
         return HelperObjSpace()
 
     # Specialized creators whose interface is in the abstract base class

Modified: pypy/branch/builtinrefactor/pypy/objspace/ann/wrapper.py
==============================================================================
--- pypy/branch/builtinrefactor/pypy/objspace/ann/wrapper.py	(original)
+++ pypy/branch/builtinrefactor/pypy/objspace/ann/wrapper.py	Sun Jul 27 20:53:08 2003
@@ -81,7 +81,13 @@
     def clone(self):
         if type(self.value) is type(lambda:0): # XXX
             return W_Constant(self.value)
-        return W_Constant(copy.deepcopy(self.value))
+        try:
+            return W_Constant(copy.deepcopy(self.value))
+        except TypeError:
+            return W_Constant(self.value)
+        except:
+            return W_Constant(self.value)
+
 
 class W_KnownKeysContainer(W_Object):
     """A dict with a known set of keys or a list with known length.

Modified: pypy/branch/builtinrefactor/pypy/objspace/std/funcobject.py
==============================================================================
--- pypy/branch/builtinrefactor/pypy/objspace/std/funcobject.py	(original)
+++ pypy/branch/builtinrefactor/pypy/objspace/std/funcobject.py	Sun Jul 27 20:53:08 2003
@@ -40,7 +40,8 @@
     if somecode.co_flags & CO_GENERATOR:
         from generatorobject import W_GeneratorObject
         from pypy.interpreter import pyframe
-        frame = pyframe.PyFrame(space, somecode, w_globals, w_locals)
+        frame = pyframe.PyFrame()
+        frame.initialize(space, somecode, w_globals, w_locals)
         w_ret = W_GeneratorObject(space, frame)
     else:
         w_ret = somecode.eval_code(space, w_globals, w_locals)

Modified: pypy/branch/builtinrefactor/pypy/objspace/trivial.py
==============================================================================
--- pypy/branch/builtinrefactor/pypy/objspace/trivial.py	(original)
+++ pypy/branch/builtinrefactor/pypy/objspace/trivial.py	Sun Jul 27 20:53:08 2003
@@ -9,10 +9,11 @@
 import operator, types, new, sys
 
 class nugen(object):
-    def __init__(self, space, frame):
-        self.space = space
+    def __init__(self, frame):
+        self.space = frame.space
         self.frame = frame
         self.running = 0
+
     def next(self):
         if self.running:
             raise OperationError(self.space.w_ValueError,
@@ -29,21 +30,24 @@
             self.running = 0
 
         return ret
+
     def __iter__(self):
         return self
 
-class numeth(object):
+class _numeth(object):
     def __init__(self, space, func, inst, cls):
         self.space = space
         self.func = func
         self.inst = inst
         self.cls = cls
+
     def _call_(self, *args, **kws):
         if self.inst is None and self.cls is not type(None):
             pass
         else:
             args = (self.inst,) + args
         return self.func(*args, **kws)
+
     def __call__(self, *args, **kws):
         try:
             return self._call_(*args, **kws)
@@ -52,35 +56,50 @@
         except:
             raise
 
-class nufun(object):
-    def __init__(self, space, code, globals, defaultarguments, closure):
-        self.space = space
+from pypy.interpreter.gateway import InterpretedFunction
+
+class nufun(InterpretedFunction):
+    def __init__(self, space, code, globals, defs, closure):
+        InterpretedFunction.__init__(self, space, code, globals, closure)
+        self.w_defs = space.wrap(defs)
         self.__name__ = code.co_name
-        self.func_code = self.code = code
-        self.globals = globals
-        self.defaultarguments = defaultarguments
-        self.closure = closure
-    def do_call(self, *args, **kwds):
-        locals = self.code.build_arguments(self.space, args, kwds,
-            w_defaults = self.defaultarguments,
-            w_closure = self.closure)
-        if self.code.co_flags & 0x0020:
-            from pypy.interpreter import pyframe
-            frame = pyframe.PyFrame(self.space, self.code,
-                                    self.globals, locals)
-            return nugen(self.space, frame)
+        self.func_code = code
+
+        #self.func_code = self.code = code
+        #self.globals = globals
+        #self.defaultarguments = defs
+        #self.closure = closure
+
+    def __call__(self, *args, **kwargs):
+        if self.cpycode.co_flags & 0x0020:
+            frame = self.create_frame(args, kwargs)
+            return nugen(frame)
         else:
-            return self.code.eval_code(self.space, self.globals, locals)
-    def __call__(self, *args, **kwds):
-        return self.do_call(*args, **kwds)
-    def __get__(self, ob, cls=None):
-        return numeth(self.space, self, ob, cls)
+            return self.eval_frame(args, kwargs)
+
+    #def __get__(self, ob, cls=None):
+    #    return numeth(self.space, self, ob, cls)
+
+    #def do_call(self, *args, **kwds):
+    #    locals = self.code.build_arguments(self.space, args, kwds,
+    #        w_defaults = self.defaultarguments,
+    #        w_closure = self.closure)
+    #    if self.code.co_flags & 0x0020:
+    #        from pypy.interpreter import pyframe
+    #        frame = pyframe.PyFrame()
+    #        frame.initialize(self.space, self.code,
+    #                         self.globals, locals)
+    #        return nugen(self.space, frame)
+    #    else:
+    #        return self.code.eval_code(self.space, self.globals, locals)
+
+    #def __call__(self, *args, **kwds):
+    #    return self.do_call(*args, **kwds)
 
 
 class TrivialObjSpace(ObjSpace):
 
     def clone_exception_hierarchy(self):
-        from pypy.interpreter.pycode import PyByteCode
         def __init__(self, *args):
             self.args = args
         def __str__(self):
@@ -154,7 +173,6 @@
                 newstuff[c.__name__] = c
         newstuff.update(self.clone_exception_hierarchy())
         self.make_builtins()
-        self.make_sys()
         # insert these into the newly-made builtins
         for key, w_value in newstuff.items():
             self.setitem(self.w_builtins, self.wrap(key), w_value)
@@ -167,6 +185,11 @@
         return w
 
     def reraise(self):
+        #import traceback
+        #traceback.print_exc()
+        #ec = self.getexecutioncontext() # .framestack.items[-1]
+        #ec.print_detailed_traceback(self)
+
         etype, evalue = sys.exc_info()[:2]
         name = etype.__name__
         if hasattr(self, 'w_' + name):
@@ -324,11 +347,14 @@
         except StopIteration:
             raise NoValue
 
-    def newfunction(self, code, globals, defaultarguments, closure=None):
-        assert hasattr(code, 'co_name')
-        assert hasattr(code, 'build_arguments')
-        assert hasattr(code, 'eval_code')
-        return nufun(self, code, globals, defaultarguments, closure)
+    def newfunction(self, code, globals, defs, closure=None):
+        #from pypy.interpreter.gateway import Function
+        #return Function(self, code, globals, defaultarguments, closure)
+
+        #assert hasattr(code, 'co_name')
+        #assert hasattr(code, 'build_arguments')
+        #assert hasattr(code, 'eval_code')
+        return nufun(self, code, globals, defs, closure)
 
     def newstring(self, asciilist):
         try:
@@ -351,10 +377,13 @@
             args = (callable.im_self,) + args
             callable = callable.im_func
         try:
-            return apply(callable, args, kwds or {})
+            return callable(*args, **(kwds or {}))
         except OperationError:
             raise
         except:
+            #print "got exception in", callable.__name__
+            #print "len args", len(args)
+            #print "kwds", kwds
             self.reraise()
                 
     def hex(self, ob):

Modified: pypy/branch/builtinrefactor/pypy/tool/test.py
==============================================================================
--- pypy/branch/builtinrefactor/pypy/tool/test.py	(original)
+++ pypy/branch/builtinrefactor/pypy/tool/test.py	Sun Jul 27 20:53:08 2003
@@ -225,8 +225,12 @@
             modpath = fullfn[len(autopath.pypydir)+1:-3]
             modpath = 'pypy.' + modpath.replace(os.sep, '.')
             if not filterfunc or filterfunc(modpath):
-                subsuite = loader.loadTestsFromName(modpath)
-                suite.addTest(subsuite, modpath)
+                try:
+                    subsuite = loader.loadTestsFromName(modpath)
+                except:
+                    print "skipping testfile (failed loading it)", modpath
+                else:
+                    suite.addTest(subsuite, modpath)
         elif recursive and os.path.isdir(fullfn):
             subsuite = testsuite_from_dir(fullfn, filterfunc, 1, loader)
             if subsuite:


More information about the Pypy-commit mailing list