[pypy-svn] r14632 - in pypy/branch/pypy-translation-snapshot: interpreter interpreter/test module/__builtin__ objspace objspace/std

pedronis at codespeak.net pedronis at codespeak.net
Wed Jul 13 19:28:09 CEST 2005


Author: pedronis
Date: Wed Jul 13 19:27:58 2005
New Revision: 14632

Modified:
   pypy/branch/pypy-translation-snapshot/interpreter/error.py
   pypy/branch/pypy-translation-snapshot/interpreter/eval.py
   pypy/branch/pypy-translation-snapshot/interpreter/executioncontext.py
   pypy/branch/pypy-translation-snapshot/interpreter/gateway.py
   pypy/branch/pypy-translation-snapshot/interpreter/generator.py
   pypy/branch/pypy-translation-snapshot/interpreter/nestedscope.py
   pypy/branch/pypy-translation-snapshot/interpreter/pyframe.py
   pypy/branch/pypy-translation-snapshot/interpreter/pyopcode.py
   pypy/branch/pypy-translation-snapshot/interpreter/pytraceback.py
   pypy/branch/pypy-translation-snapshot/interpreter/test/test_eval.py
   pypy/branch/pypy-translation-snapshot/interpreter/typedef.py
   pypy/branch/pypy-translation-snapshot/module/__builtin__/compiling.py
   pypy/branch/pypy-translation-snapshot/objspace/std/fake.py
   pypy/branch/pypy-translation-snapshot/objspace/trace.py
Log:
merge 14631 from trunk.



Modified: pypy/branch/pypy-translation-snapshot/interpreter/error.py
==============================================================================
--- pypy/branch/pypy-translation-snapshot/interpreter/error.py	(original)
+++ pypy/branch/pypy-translation-snapshot/interpreter/error.py	Wed Jul 13 19:27:58 2005
@@ -85,7 +85,7 @@
             import linecache
             print >> file, "Traceback (application-level):"
             while tb is not None:
-                co = tb.frame.code
+                co = tb.frame.pycode
                 lineno = tb.lineno
                 fname = co.co_filename
                 if fname.startswith('<inline>\n'):

Modified: pypy/branch/pypy-translation-snapshot/interpreter/eval.py
==============================================================================
--- pypy/branch/pypy-translation-snapshot/interpreter/eval.py	(original)
+++ pypy/branch/pypy-translation-snapshot/interpreter/eval.py	Wed Jul 13 19:27:58 2005
@@ -54,38 +54,29 @@
     """A frame is an environment supporting the execution of a code object.
     Abstract base class."""
 
-    def __init__(self, space, code, w_globals=None, numlocals=-1):
+    def __init__(self, space, w_globals=None, numlocals=-1):
         self.space      = space
-        self.code       = code       # Code instance
         self.w_globals  = w_globals  # wrapped dict of globals
         self.w_locals   = None       # wrapped dict of locals
         if numlocals < 0:  # compute the minimal size based on arguments
-            numlocals = len(code.getvarnames())
+            numlocals = len(self.getcode().getvarnames())
         self.numlocals = numlocals
 
-    def resume(self):
-        "Resume the execution of the frame from its current state."
-        executioncontext = self.space.getexecutioncontext()
-        executioncontext.enter(self)
-        try:
-            result = self.eval(executioncontext)
-        finally:
-            executioncontext.leave(self)
-        return result
-
-    # running a frame is usually the same as resuming it from its
-    # initial state, but not for generator frames
-    run = resume
-
-    def eval(self, executioncontext):
-        "Abstract method to override."
+    def run(self):
+        "Abstract method to override. Runs the frame"
         raise TypeError, "abstract"
-
+    
     def getdictscope(self):
         "Get the locals as a dictionary."
         self.fast2locals()
         return self.w_locals
 
+    def getcode(self):
+        return None
+
+    def fget_code(space, self):
+        return space.wrap(self.getcode())
+
     def fget_getdictscope(space, self): # unwrapping through unwrap_spec in typedef.py
         return self.getdictscope()
 
@@ -100,14 +91,14 @@
 
     def setfastscope(self, scope_w):
         """Abstract. Initialize the fast locals from a list of values,
-        where the order is according to self.code.signature()."""
+        where the order is according to self.getcode().signature()."""
         raise TypeError, "abstract"
 
     def fast2locals(self):
         # Copy values from self.fastlocals_w to self.w_locals
         if self.w_locals is None:
             self.w_locals = self.space.newdict([])
-        varnames = self.code.getvarnames()
+        varnames = self.getcode().getvarnames()
         fastscope_w = self.getfastscope()
         for i in range(min(len(varnames), len(fastscope_w))):
             name = varnames[i]
@@ -119,7 +110,7 @@
     def locals2fast(self):
         # Copy values from self.w_locals to self.fastlocals_w
         assert self.w_locals is not None
-        varnames = self.code.getvarnames()
+        varnames = self.getcode().getvarnames()
 
         new_fastlocals_w = [None]*self.numlocals
         
@@ -134,3 +125,27 @@
                 new_fastlocals_w[i] = w_value
 
         self.setfastscope(new_fastlocals_w)
+
+
+class EvalFrame(Frame):
+
+    def resume(self):
+        "Resume the execution of the frame from its current state."
+        executioncontext = self.space.getexecutioncontext()
+        executioncontext.enter(self)
+        try:
+            result = self.eval(executioncontext)
+        finally:
+            executioncontext.leave(self)
+        return result
+
+    # running a frame is usually the same as resuming it from its
+    # initial state, but not for generator frames
+    run = resume
+
+    def eval(self, executioncontext):
+        "Abstract method to override."
+        raise TypeError, "abstract"
+
+    def hide(self):
+        return False

Modified: pypy/branch/pypy-translation-snapshot/interpreter/executioncontext.py
==============================================================================
--- pypy/branch/pypy-translation-snapshot/interpreter/executioncontext.py	(original)
+++ pypy/branch/pypy-translation-snapshot/interpreter/executioncontext.py	Wed Jul 13 19:27:58 2005
@@ -25,14 +25,14 @@
         except:
             frame.f_back = None
 
-        if not frame.code.hidden_applevel:
+        if not frame.hide():
             self.framestack.push(frame)
 
     def leave(self, frame):
         if self.w_profilefunc:
             self._trace(frame, 'leaveframe', None)
                 
-        if not frame.code.hidden_applevel:
+        if not frame.hide():
             self.framestack.pop()
 
     def get_builtin(self):
@@ -61,7 +61,7 @@
         "Trace function called before each bytecode."
         if self.is_tracing or frame.w_f_trace is None:
             return
-        code = getattr(frame, 'code')
+        code = getattr(frame, 'pycode')
         if frame.instr_lb <= frame.last_instr < frame.instr_ub:
             return
 
@@ -144,7 +144,7 @@
             self.is_tracing = is_tracing
 
     def _trace(self, frame, event, w_arg, operr=None):
-        if self.is_tracing or frame.code.hidden_applevel:
+        if self.is_tracing or frame.hide():
             return
 
         space = self.space

Modified: pypy/branch/pypy-translation-snapshot/interpreter/gateway.py
==============================================================================
--- pypy/branch/pypy-translation-snapshot/interpreter/gateway.py	(original)
+++ pypy/branch/pypy-translation-snapshot/interpreter/gateway.py	Wed Jul 13 19:27:58 2005
@@ -235,6 +235,13 @@
     # Initialization of locals is already done by the time run() is called,
     # via the interface defined in eval.Frame.
 
+    def __init__(self, space, code, w_globals=None, numlocals=-1):
+        self.bltn_code = code
+        eval.Frame.__init__(self, space, w_globals, numlocals)
+
+    def getcode(self):
+        return self.bltn_code
+
     def setfastscope(self, scope_w):
         """Subclasses with behavior specific for an unwrap spec are generated"""
         raise TypeError, "abstract"

Modified: pypy/branch/pypy-translation-snapshot/interpreter/generator.py
==============================================================================
--- pypy/branch/pypy-translation-snapshot/interpreter/generator.py	(original)
+++ pypy/branch/pypy-translation-snapshot/interpreter/generator.py	Wed Jul 13 19:27:58 2005
@@ -1,6 +1,6 @@
 from pypy.interpreter.error import OperationError
 from pypy.interpreter.baseobjspace import Wrappable
-from pypy.interpreter.eval import Frame
+from pypy.interpreter.eval import EvalFrame
 from pypy.interpreter.pyframe import ControlFlowException, ExitFrame
 
 #
@@ -12,7 +12,7 @@
 # that return iterators).
 #
 
-class GeneratorFrame(Frame):
+class GeneratorFrame(EvalFrame):
     "A frame attached to a generator."
 
     def run(self):

Modified: pypy/branch/pypy-translation-snapshot/interpreter/nestedscope.py
==============================================================================
--- pypy/branch/pypy-translation-snapshot/interpreter/nestedscope.py	(original)
+++ pypy/branch/pypy-translation-snapshot/interpreter/nestedscope.py	Wed Jul 13 19:27:58 2005
@@ -65,14 +65,14 @@
         self.cells = [Cell() for i in range(ncellvars)] + closure
 
     def getclosure(self):
-        ncellvars = len(self.code.co_cellvars)  # not part of the closure
+        ncellvars = len(self.pycode.co_cellvars)  # not part of the closure
         return self.cells[ncellvars:]
 
     def fast2locals(self):
         PyInterpFrame.fast2locals(self)
         # cellvars are values exported to inner scopes
         # freevars are values coming from outer scopes 
-        freevarnames = self.code.co_cellvars + self.code.co_freevars
+        freevarnames = self.pycode.co_cellvars + self.pycode.co_freevars
         for i in range(len(freevarnames)):
             name = freevarnames[i]
             cell = self.cells[i]
@@ -86,7 +86,7 @@
 
     def locals2fast(self):
         PyInterpFrame.locals2fast(self)
-        freevarnames = self.code.co_cellvars + self.code.co_freevars
+        freevarnames = self.pycode.co_cellvars + self.pycode.co_freevars
         for i in range(len(freevarnames)):
             name = freevarnames[i]
             cell = self.cells[i]
@@ -101,10 +101,10 @@
 
     def setfastscope(self, scope_w):
         PyInterpFrame.setfastscope(self, scope_w)
-        if self.code.co_cellvars:
+        if self.pycode.co_cellvars:
             # the first few cell vars could shadow already-set arguments,
             # in the same order as they appear in co_varnames
-            code     = self.code
+            code     = self.pycode
             argvars  = code.co_varnames
             cellvars = code.co_cellvars
             next     = 0
@@ -121,12 +121,12 @@
                         break   # all cell vars initialized this way
 
     def getfreevarname(self, index):
-        freevarnames = self.code.co_cellvars + self.code.co_freevars
+        freevarnames = self.pycode.co_cellvars + self.pycode.co_freevars
         return freevarnames[index]
 
     def iscellvar(self, index):
         # is the variable given by index a cell or a free var?
-        return index < len(self.code.co_cellvars)
+        return index < len(self.pycode.co_cellvars)
 
     ### extra opcodes ###
 

Modified: pypy/branch/pypy-translation-snapshot/interpreter/pyframe.py
==============================================================================
--- pypy/branch/pypy-translation-snapshot/interpreter/pyframe.py	(original)
+++ pypy/branch/pypy-translation-snapshot/interpreter/pyframe.py	Wed Jul 13 19:27:58 2005
@@ -21,7 +21,7 @@
    return sys.exc_info()[2]   
 cpython_tb._annspecialcase_ = "override:ignore"
 
-class PyFrame(eval.Frame):
+class PyFrame(eval.EvalFrame):
     """Represents a frame for a regular Python function
     that needs to be interpreted.
 
@@ -37,7 +37,8 @@
     """
 
     def __init__(self, space, code, w_globals, closure):
-        eval.Frame.__init__(self, space, code, w_globals, code.co_nlocals)
+        self.pycode = code
+        eval.Frame.__init__(self, space, w_globals, code.co_nlocals)
         self.valuestack = Stack()
         self.blockstack = Stack()
         self.last_exception = None
@@ -52,11 +53,17 @@
         self.w_f_trace = None
         self.last_instr = -1
         self.f_back = None
-        self.f_lineno = self.code.co_firstlineno
+        self.f_lineno = self.pycode.co_firstlineno
         
         # For tracing
         self.instr_lb = 0
         self.instr_ub = -1
+
+    def hide(self):
+        return self.pycode.hidden_applevel
+
+    def getcode(self):
+        return self.pycode
         
     def getfastscope(self):
         "Get the fast locals as a list."
@@ -64,7 +71,7 @@
 
     def setfastscope(self, scope_w):
         """Initialize the fast locals from a list of values,
-        where the order is according to self.code.signature()."""
+        where the order is according to self.pycode.signature()."""
         if len(scope_w) > len(self.fastlocals_w):
             raise ValueError, "new fastscope is longer than the allocated area"
         self.fastlocals_w[:len(scope_w)] = scope_w
@@ -145,15 +152,15 @@
             raise OperationError(space.w_ValueError,
                   space.wrap("f_lineo can only be set by a trace function."))
 
-        if new_lineno < self.code.co_firstlineno:
+        if new_lineno < self.pycode.co_firstlineno:
             raise OperationError(space.w_ValueError,
                   space.wrap("line %d comes before the current code." % new_lineno))
-        code = self.code.co_code
+        code = self.pycode.co_code
         addr = 0
-        line = self.code.co_firstlineno
+        line = self.pycode.co_firstlineno
         new_lasti = -1
         offset = 0
-        lnotab = self.code.co_lnotab
+        lnotab = self.pycode.co_lnotab
         for offset in xrange(0, len(lnotab), 2):
             addr += ord(lnotab[offset])
             line += ord(lnotab[offset + 1])
@@ -259,11 +266,11 @@
             
     def get_last_lineno(self):
         "Returns the line number of the instruction currently being executed."
-        return pytraceback.offset2lineno(self.code, self.next_instr-1)
+        return pytraceback.offset2lineno(self.pycode, self.next_instr-1)
 
     def get_next_lineno(self):
         "Returns the line number of the next instruction to execute."
-        return pytraceback.offset2lineno(self.code, self.next_instr)
+        return pytraceback.offset2lineno(self.pycode, self.next_instr)
 
     def fget_f_builtins(space, self):
         return self.builtin.getdict()

Modified: pypy/branch/pypy-translation-snapshot/interpreter/pyopcode.py
==============================================================================
--- pypy/branch/pypy-translation-snapshot/interpreter/pyopcode.py	(original)
+++ pypy/branch/pypy-translation-snapshot/interpreter/pyopcode.py	Wed Jul 13 19:27:58 2005
@@ -57,7 +57,7 @@
             fn(self)
 
     def nextop(self):
-        c = self.code.co_code[self.next_instr]
+        c = self.pycode.co_code[self.next_instr]
         self.next_instr += 1
         return ord(c)
 
@@ -69,16 +69,16 @@
     ### accessor functions ###
 
     def getlocalvarname(self, index):
-        return self.code.co_varnames[index]
+        return self.pycode.co_varnames[index]
 
     def getconstant_w(self, index):
-        return self.code.co_consts_w[index]
+        return self.pycode.co_consts_w[index]
 
     def getname_u(self, index):
-        return self.code.co_names[index]
+        return self.pycode.co_names[index]
 
     def getname_w(self, index):
-        return self.space.wrap(self.code.co_names[index])
+        return self.space.wrap(self.pycode.co_names[index])
 
 
     ################################################################
@@ -113,9 +113,9 @@
         #    print " varindex:", varindex
         #    print " len(locals_w)", len(f.locals_w)
         #    import dis
-        #    print dis.dis(f.code)
-        #    print "co_varnames", f.code.co_varnames
-        #    print "co_nlocals", f.code.co_nlocals
+        #    print dis.dis(f.pycode)
+        #    print "co_varnames", f.pycode.co_varnames
+        #    print "co_nlocals", f.pycode.co_nlocals
         #    raise
 
     def POP_TOP(f):
@@ -346,7 +346,7 @@
         w_locals  = f.valuestack.pop()
         w_globals = f.valuestack.pop()
         w_prog    = f.valuestack.pop()
-        flags = f.space.getexecutioncontext().compiler.getcodeflags(f.code)
+        flags = f.space.getexecutioncontext().compiler.getcodeflags(f.pycode)
         w_compile_flags = f.space.wrap(flags)
         w_resulttuple = prepare_exec(f.space, f.space.wrap(f), w_prog,
                                      w_globals, w_locals,

Modified: pypy/branch/pypy-translation-snapshot/interpreter/pytraceback.py
==============================================================================
--- pypy/branch/pypy-translation-snapshot/interpreter/pytraceback.py	(original)
+++ pypy/branch/pypy-translation-snapshot/interpreter/pytraceback.py	Wed Jul 13 19:27:58 2005
@@ -20,9 +20,9 @@
 
 
 def record_application_traceback(space, operror, frame, last_instruction):
-    if frame.code.hidden_applevel:
+    if frame.pycode.hidden_applevel:
         return
-    lineno = offset2lineno(frame.code, last_instruction)
+    lineno = offset2lineno(frame.pycode, last_instruction)
     tb = operror.application_traceback
     tb = PyTraceback(space, frame, last_instruction, lineno, tb)
     operror.application_traceback = tb

Modified: pypy/branch/pypy-translation-snapshot/interpreter/test/test_eval.py
==============================================================================
--- pypy/branch/pypy-translation-snapshot/interpreter/test/test_eval.py	(original)
+++ pypy/branch/pypy-translation-snapshot/interpreter/test/test_eval.py	Wed Jul 13 19:27:58 2005
@@ -13,9 +13,13 @@
         class ConcreteFastscopeFrame(Frame):
             
             def __init__(self, space, code, numlocals):
-                Frame.__init__(self, space, code, numlocals=numlocals)
+                self.code = code
+                Frame.__init__(self, space, numlocals=numlocals)
                 self.fastlocals_w = [None] * self.numlocals
 
+            def getcode(self):
+                return self.code
+
             def setfastscope(self, scope_w):
                 self.fastlocals_w = scope_w
 

Modified: pypy/branch/pypy-translation-snapshot/interpreter/typedef.py
==============================================================================
--- pypy/branch/pypy-translation-snapshot/interpreter/typedef.py	(original)
+++ pypy/branch/pypy-translation-snapshot/interpreter/typedef.py	Wed Jul 13 19:27:58 2005
@@ -366,7 +366,7 @@
     )
 
 Frame.typedef = TypeDef('internal-frame',
-    f_code = interp_attrproperty('code', cls=Frame),
+    f_code = GetSetProperty(Frame.fget_code),
     f_locals = GetSetProperty(Frame.fget_getdictscope),
     f_globals = interp_attrproperty_w('w_globals', cls=Frame),
     )

Modified: pypy/branch/pypy-translation-snapshot/module/__builtin__/compiling.py
==============================================================================
--- pypy/branch/pypy-translation-snapshot/module/__builtin__/compiling.py	(original)
+++ pypy/branch/pypy-translation-snapshot/module/__builtin__/compiling.py	Wed Jul 13 19:27:58 2005
@@ -20,7 +20,7 @@
         except IndexError:
             pass
         else:
-            flags |= ec.compiler.getcodeflags(caller.code)
+            flags |= ec.compiler.getcodeflags(caller.getcode())
 
     code = ec.compiler.compile(str_, filename, mode, flags)
     return space.wrap(code)

Modified: pypy/branch/pypy-translation-snapshot/objspace/std/fake.py
==============================================================================
--- pypy/branch/pypy-translation-snapshot/objspace/std/fake.py	(original)
+++ pypy/branch/pypy-translation-snapshot/objspace/std/fake.py	Wed Jul 13 19:27:58 2005
@@ -136,13 +136,20 @@
 
 class CPythonFakeFrame(eval.Frame):
 
+    def __init__(self, space, code, w_globals=None, numlocals=-1):
+        self.fakecode = code
+        eval.Frame.__init__(self, space, w_globals, numlocals)
+
+    def getcode(self):
+        return self.fakecode
+
     def setfastscope(self, scope_w):
         w_args, w_kwds = scope_w
         try:
             self.unwrappedargs = self.space.unwrap(w_args)
             self.unwrappedkwds = self.space.unwrap(w_kwds)
         except UnwrapError, e:
-            code = self.code
+            code = self.fakecode
             assert isinstance(code, CPythonFakeCode)
             raise UnwrapError('calling %s: %s' % (code.cpy_callable, e))
 
@@ -150,7 +157,7 @@
         raise OperationError(self.space.w_TypeError,
           self.space.wrap("cannot get fastscope of a CPythonFakeFrame"))                           
     def run(self):
-        code = self.code
+        code = self.fakecode
         assert isinstance(code, CPythonFakeCode)
         fn = code.cpy_callable
         try:

Modified: pypy/branch/pypy-translation-snapshot/objspace/trace.py
==============================================================================
--- pypy/branch/pypy-translation-snapshot/objspace/trace.py	(original)
+++ pypy/branch/pypy-translation-snapshot/objspace/trace.py	Wed Jul 13 19:27:58 2005
@@ -18,7 +18,7 @@
     """ bytecode trace. """
     def __init__(self, frame):
         self.frame = frame
-        self.code = frame.code
+        self.code = frame.pycode
         self.index = frame.next_instr
 
 class EnterFrame(object):
@@ -85,9 +85,9 @@
         """ return (possibly cached) pydis result for the given frame. """
 
         try:
-            return _cache[id(frame.code)]
+            return _cache[id(frame.pycode)]
         except KeyError:
-            res = _cache[id(frame.code)] = pydis.pydis(frame.code)
+            res = _cache[id(frame.pycode)] = pydis.pydis(frame.pycode)
             assert res is not None
             return res
 



More information about the Pypy-commit mailing list