[pypy-svn] r9283 - pypy/branch/dist-interpapp/pypy/interpreter

arigo at codespeak.net arigo at codespeak.net
Thu Feb 17 18:47:08 CET 2005


Author: arigo
Date: Thu Feb 17 18:47:07 2005
New Revision: 9283

Modified:
   pypy/branch/dist-interpapp/pypy/interpreter/baseobjspace.py
   pypy/branch/dist-interpapp/pypy/interpreter/executioncontext.py
   pypy/branch/dist-interpapp/pypy/interpreter/module.py
   pypy/branch/dist-interpapp/pypy/interpreter/pycode.py
   pypy/branch/dist-interpapp/pypy/interpreter/pyframe.py
   pypy/branch/dist-interpapp/pypy/interpreter/pyopcode.py
   pypy/branch/dist-interpapp/pypy/interpreter/typedef.py
Log:
(intermediate check-in, things still break.)

Added proper support for the __builtins__ name in globals, as in CPython.
Now PyFrame.w_builtins has been replaced with PyFrame.builtin, which is the 
builtin module to use, instead of a dict (to enable lazy-loading).

Also changed PyCode.co_names_w back to PyCode.co_names, to avoid unwrapping.



Modified: pypy/branch/dist-interpapp/pypy/interpreter/baseobjspace.py
==============================================================================
--- pypy/branch/dist-interpapp/pypy/interpreter/baseobjspace.py	(original)
+++ pypy/branch/dist-interpapp/pypy/interpreter/baseobjspace.py	Thu Feb 17 18:47:07 2005
@@ -284,6 +284,8 @@
             statement = PyCode(self)._from_code(statement)
         if not isinstance(statement, PyCode):
             raise TypeError, 'space.exec_(): expected a string, code or PyCode object'
+        self.call_method(w_globals, 'setdefault',
+                         self.wrap('__builtins__'), self.wrap(self.builtin))
         return statement.exec_code(self, w_globals, w_locals)
 
     def appexec(self, posargs_w, source): 

Modified: pypy/branch/dist-interpapp/pypy/interpreter/executioncontext.py
==============================================================================
--- pypy/branch/dist-interpapp/pypy/interpreter/executioncontext.py	(original)
+++ pypy/branch/dist-interpapp/pypy/interpreter/executioncontext.py	Thu Feb 17 18:47:07 2005
@@ -25,17 +25,16 @@
         locals = getthreadlocals()
         locals.executioncontext = previous_ec
 
-    def get_w_builtins(self):
-        # XXX sort out __builtins__ issue 
-        #if self.framestack.empty():
-        return self.space.w_builtin
-        #else:
-        #    return self.framestack.top().w_builtins
+    def get_builtin(self):
+        try:
+            return self.framestack.top().builtin
+        except IndexError:
+            return self.space.builtin
 
     def make_standard_w_globals(self):
         "Create a new empty 'globals' dictionary."
         w_key = self.space.wrap("__builtins__")
-        w_value = self.get_w_builtins()
+        w_value = self.space.wrap(self.get_builtin())
         w_globals = self.space.newdict([(w_key, w_value)])
         return w_globals
 

Modified: pypy/branch/dist-interpapp/pypy/interpreter/module.py
==============================================================================
--- pypy/branch/dist-interpapp/pypy/interpreter/module.py	(original)
+++ pypy/branch/dist-interpapp/pypy/interpreter/module.py	Thu Feb 17 18:47:07 2005
@@ -14,7 +14,8 @@
             w_dict = space.newdict([])
         self.w_dict = w_dict 
         self.w_name = w_name 
-        space.setitem(w_dict, space.wrap('__name__'), w_name) 
+        if w_name is not None:
+            space.setitem(w_dict, space.wrap('__name__'), w_name) 
 
     def getdict(self):
         return self.w_dict

Modified: pypy/branch/dist-interpapp/pypy/interpreter/pycode.py
==============================================================================
--- pypy/branch/dist-interpapp/pypy/interpreter/pycode.py	(original)
+++ pypy/branch/dist-interpapp/pypy/interpreter/pycode.py	Thu Feb 17 18:47:07 2005
@@ -55,7 +55,7 @@
         self.co_flags = 0            # CO_..., see above
         self.co_code = None          # string: instruction opcodes
         self.co_consts_w = []        # list of constants used (wrapped!)
-        self.co_names_w = []         # list of wrapped strs: names (for attrs..)
+        self.co_names = []           # list of strings: names (for attrs..)
         self.co_varnames = ()        # tuple of strings: local variable names
         self.co_freevars = ()        # tuple of strings: free variable names
         self.co_cellvars = ()        # tuple of strings: cell variable names
@@ -88,13 +88,13 @@
         self.co_code = x
         #self.co_consts = <see below>
         x = code.co_names; assert isinstance(x, tuple)
-        self.co_names_w = [ self.space.wrap(i) for i in x ] 
+        self.co_names = list(x)
         x = code.co_varnames; assert isinstance(x, tuple)
-        self.co_varnames = x
+        self.co_varnames = list(x)
         x = code.co_freevars; assert isinstance(x, tuple)
-        self.co_freevars = x
+        self.co_freevars = list(x)
         x = code.co_cellvars; assert isinstance(x, tuple)
-        self.co_cellvars = x
+        self.co_cellvars = list(x)
         x = code.co_filename; assert isinstance(x, str)
         self.co_filename = x
         x = code.co_name; assert isinstance(x, str)
@@ -157,7 +157,7 @@
     
     def fget_co_names(space, w_self):
         self = space.interpclass_w(w_self)
-        return space.newtuple(self.co_names_w)
+        return space.newtuple([space.wrap(name) for name in self.co_names])
 
     def descr_code__eq__(space, w_self, w_other):
         self = space.interpclass_w(w_self)
@@ -170,7 +170,11 @@
                     self.co_flags == other.co_flags and
                     self.co_firstlineno == other.co_firstlineno and
                     self.co_code == other.co_code and
-                    len(self.co_consts_w) == len(other.co_consts_w))
+                    len(self.co_consts_w) == len(other.co_consts_w) and
+                    self.co_names == other.co_names and
+                    self.co_varnames == other.co_varnames and
+                    self.co_freevars == other.co_freevars and
+                    self.co_cellvars == other.co_cellvars)
         if not areEqual:
             return space.w_False
 
@@ -178,17 +182,6 @@
             if not space.eq_w(self.co_consts_w[i], other.co_consts_w[i]):
                 return space.w_False
 
-        if len(self.co_names_w) != len(other.co_names_w):
-            return space.w_False
-        
-        for i in range(len(self.co_names_w)):
-            if not space.eq_w(self.co_names_w[i], other.co_names_w[i]):
-                return space.w_False
-        if (self.co_varnames == other.co_varnames and
-            self.co_freevars == other.co_freevars and
-            self.co_cellvars == other.co_cellvars):
-            return space.w_True
-
         return space.w_True
     
     def descr_code__new__(space, w_subtype,
@@ -206,7 +199,7 @@
         code.co_flags      = space.int_w(w_flags)
         code.co_code       = space.str_w(w_codestring)
         code.co_consts_w   = space.unpacktuple(w_constants)
-        code.co_names_w    = space.unpacktuple(w_names)
+        code.co_names      = unpack_str_tuple(space, w_names)
         code.co_varnames   = unpack_str_tuple(space, w_varnames)
         code.co_filename   = space.str_w(w_filename)
         code.co_name       = space.str_w(w_name)

Modified: pypy/branch/dist-interpapp/pypy/interpreter/pyframe.py
==============================================================================
--- pypy/branch/dist-interpapp/pypy/interpreter/pyframe.py	(original)
+++ pypy/branch/dist-interpapp/pypy/interpreter/pyframe.py	Thu Feb 17 18:47:07 2005
@@ -5,6 +5,7 @@
 from pypy.interpreter.miscutils import Stack
 from pypy.interpreter.error import OperationError
 from pypy.interpreter import pytraceback
+from pypy.interpreter import module
 
 import __future__
 compiler_flags = 0
@@ -23,7 +24,7 @@
      * 'code' is the PyCode object this frame runs
      * 'w_locals' is the locals dictionary to use
      * 'w_globals' is the attached globals dictionary
-     * 'w_builtins' is the attached built-ins dictionary
+     * 'builtin' is the attached built-in module
      * 'valuestack', 'blockstack', 'next_instr' control the interpretation
     """
 
@@ -33,7 +34,7 @@
         self.blockstack = Stack()
         self.last_exception = None
         self.next_instr = 0
-        #self.w_builtins = self.space.w_builtins
+        self.builtin = lookup_builtin(space, w_globals)
         # regular functions always have CO_OPTIMIZED and CO_NEWLOCALS.
         # class bodies only have CO_NEWLOCALS.
         if code.dictscope_needed():
@@ -130,6 +131,31 @@
         "Returns the line number of the next instruction to execute."
         return pytraceback.offset2lineno(self.code, self.next_instr)
 
+    def fget_f_builtins(space, w_self):
+        self = space.interpclass_w(w_self)
+        return self.builtin.getdict()
+
+
+def lookup_builtin(space, w_globals):
+    "Look up the builtin module to use from the __builtins__ global"
+    try:
+        w_builtin = space.getitem(w_globals, space.wrap('__builtins__'))
+    except OperationError, e:
+        if not e.match(space, space.w_KeyError):
+            raise
+    else:
+        if w_builtin is space.builtin:   # common case
+            return space.builtin
+        if space.is_true(space.isinstance(w_builtin, space.w_dict)):
+            return module.Module(space, None, w_builtin)
+        builtin = space.interpclass_w(w_builtin)
+        if isinstance(builtin, Module):
+            return builtin
+    # no builtin! make a default one.  Given them None, at least.
+    builtin = module.Module(space, None)
+    space.setitem(builtin.w_dict, space.wrap('None'), space.w_None)
+    return builtin
+
 
 ### Frame Blocks ###
 

Modified: pypy/branch/dist-interpapp/pypy/interpreter/pyopcode.py
==============================================================================
--- pypy/branch/dist-interpapp/pypy/interpreter/pyopcode.py	(original)
+++ pypy/branch/dist-interpapp/pypy/interpreter/pyopcode.py	Thu Feb 17 18:47:07 2005
@@ -71,9 +71,11 @@
     def getconstant_w(self, index):
         return self.code.co_consts_w[index]
 
+    def getname(self, index):
+        return self.code.co_names[index]
+
     def getname_w(self, index):
-        w_varname = self.code.co_names_w[index]
-        return w_varname
+        return self.space.wrap(self.code.co_names[index])
 
 
     ################################################################
@@ -346,7 +348,7 @@
         w_prog    = f.valuestack.pop()
         w_compile_flags = f.space.wrap(f.get_compile_flags())
         w_resulttuple = prepare_exec(f.space, f.space.wrap(f), w_prog, w_globals, w_locals,
-                                       w_compile_flags, f.space.builtin.w_dict) 
+                                       w_compile_flags, space.wrap(f.builtin))
         w_prog, w_globals, w_locals = f.space.unpacktuple(w_resulttuple, 3)
 
         plain = f.space.is_true(f.space.is_(w_locals, f.w_locals))
@@ -379,7 +381,8 @@
         w_bases       = f.valuestack.pop()
         w_name        = f.valuestack.pop()
         w_metaclass = find_metaclass(f.space, w_bases,
-                                     w_methodsdict, f.w_globals, f.space.builtin) 
+                                     w_methodsdict, f.w_globals,
+                                     f.space.wrap(f.builtin)) 
         w_newclass = f.space.call_function(w_metaclass, w_name,
                                            w_bases, w_methodsdict)
         f.valuestack.push(w_newclass)
@@ -433,32 +436,19 @@
         f.space.delitem(f.w_globals, w_varname)
 
     def LOAD_NAME(f, nameindex):
-        w_varname = f.getname_w(nameindex)
-
-        if f.w_globals is f.w_locals:
-            try_list_w = [f.w_globals]
-
-        else:
-            try_list_w = [f.w_locals, f.w_globals]
-
-        for wrapped in try_list_w:
+        if f.w_locals is not f.w_globals:
+            w_varname = f.getname_w(nameindex)
             try:
-                w_value = f.space.getitem(wrapped, w_varname)
-                break 
+                w_value = f.space.getitem(f.w_locals, w_varname)
             except OperationError, e:
                 if not e.match(f.space, f.space.w_KeyError):
                     raise
-        else: 
-            w_value =  getbuiltin(f.space, w_varname)
-        f.valuestack.push(w_value) 
-        # XXX the implementation can be pushed back into app-space as an
-        # when exception handling begins to behave itself.  For now, it
-        # was getting on my nerves -- mwh
-        #    w_value = f.load_name(w_varname)
-        #    f.valuestack.push(w_value)
+            else:
+                f.valuestack.push(w_value)
+                return
+        f.LOAD_GLOBAL(nameindex)    # fall-back
 
     def LOAD_GLOBAL(f, nameindex):
-        assert f.w_globals is not None
         w_varname = f.getname_w(nameindex)
         try:
             w_value = f.space.getitem(f.w_globals, w_varname)
@@ -467,7 +457,12 @@
             if not e.match(f.space, f.space.w_KeyError):
                 raise
             # we got a KeyError, now look in the built-ins
-            w_value = getbuiltin(f.space, w_varname) 
+            varname = f.getname(nameindex)
+            w_value = f.builtin.getdictvalue(f.space, varname)
+            if w_value is None:
+                message = "global name '%s' is not defined" % varname
+                raise OperationError(space.w_NameError,
+                                     space.wrap(message))
         f.valuestack.push(w_value)
 
     def DELETE_FAST(f, varindex):
@@ -548,11 +543,8 @@
         w_modulename = f.getname_w(nameindex)
         modulename = f.space.str_w(w_modulename)
         w_fromlist = f.valuestack.pop()
-        try:
-            w_import = space.builtin.get('__import__') 
-        except OperationError, e:
-            if not e.match(space, space.w_AttributeError):
-                raise
+        w_import = f.builtin.getdictvalue(f.space, '__import__')
+        if w_import is None:
             raise OperationError(space.w_ImportError,
                                  space.wrap("__import__ not found"))
         w_locals = f.w_locals
@@ -721,19 +713,6 @@
         cls.dispatch_table = dispatch_table
 
 
-def getbuiltin(space, w_varname): 
-    varname = space.str_w(w_varname) 
-    try:
-        return space.builtin.get(space.str_w(w_varname))
-    except OperationError, e:
-        # catch AttributeErrors 
-        if not e.match(space, space.w_AttributeError):
-            raise
-        message = "global name '%s' is not defined" % space.str_w(w_varname)
-        w_exc_type = space.w_NameError
-        w_exc_value = space.wrap(message)
-        raise OperationError(w_exc_type, w_exc_value)
-
 ### helpers written at the application-level ###
 # Some of these functions are expected to be generally useful if other
 # parts of the code needs to do the same thing as a non-trivial opcode,
@@ -821,7 +800,7 @@
                 continue
             into_locals[name] = getattr(module, name)
 
-    def prepare_exec(f, prog, globals, locals, compile_flags, builtins):
+    def prepare_exec(f, prog, globals, locals, compile_flags, builtin):
         """Manipulate parameters to exec statement to (codeobject, dict, dict).
         """
         # XXX INCOMPLETE
@@ -841,7 +820,7 @@
         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 # f.space.w_builtin # f_builtins
+            globals['__builtins__'] = builtin
         if not isinstance(locals, dict):
             raise TypeError("exec: arg 3 must be a dictionary or None")
         # XXX - HACK to check for code object

Modified: pypy/branch/dist-interpapp/pypy/interpreter/typedef.py
==============================================================================
--- pypy/branch/dist-interpapp/pypy/interpreter/typedef.py	(original)
+++ pypy/branch/dist-interpapp/pypy/interpreter/typedef.py	Thu Feb 17 18:47:07 2005
@@ -265,7 +265,7 @@
     )
 
 PyFrame.typedef = TypeDef('frame',
-    #f_builtins = interp_attrproperty_w('w_builtins'),
+    f_builtins = GetSetProperty(PyFrame.fget_f_builtins.im_func),
     f_lineno = GetSetProperty(PyFrame.fget_f_lineno.im_func),
     **Frame.typedef.rawdict)
 



More information about the Pypy-commit mailing list