Author: Alex Gaynor <alex.gay...@gmail.com> Branch: remove-eval-frame Changeset: r67606:d7fd8a64c1db Date: 2013-10-25 10:26 -0700 http://bitbucket.org/pypy/pypy/changeset/d7fd8a64c1db/
Log: Remove eval.Frame class that didn't add value (and added foncusion) diff --git a/pypy/interpreter/eval.py b/pypy/interpreter/eval.py --- a/pypy/interpreter/eval.py +++ b/pypy/interpreter/eval.py @@ -2,7 +2,7 @@ This module defines the abstract base classes that support execution: Code and Frame. """ -from pypy.interpreter.error import OperationError + from pypy.interpreter.baseobjspace import W_Root @@ -51,88 +51,3 @@ def funcrun_obj(self, func, w_obj, args): return self.funcrun(func, args.prepend(w_obj)) - - -class Frame(W_Root): - """A frame is an environment supporting the execution of a code object. - Abstract base class.""" - - def __init__(self, space, w_globals=None): - self.space = space - self.w_globals = w_globals # wrapped dict of globals - self.w_locals = None # wrapped dict of locals - - 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(self, space): - return space.wrap(self.getcode()) - - def fget_getdictscope(self, space): - return self.getdictscope() - - def setdictscope(self, w_locals): - "Initialize the locals from a dictionary." - self.w_locals = w_locals - self.locals2fast() - - def getfastscope(self): - "Abstract. Get the fast locals as a list." - raise TypeError("abstract") - - def setfastscope(self, scope_w): - """Abstract. Initialize the fast locals from a list of values, - where the order is according to self.getcode().signature().""" - raise TypeError("abstract") - - def getfastscopelength(self): - "Abstract. Get the expected number of locals." - raise TypeError("abstract") - - def fast2locals(self): - # Copy values from the fastlocals to self.w_locals - if self.w_locals is None: - self.w_locals = self.space.newdict() - varnames = self.getcode().getvarnames() - fastscope_w = self.getfastscope() - for i in range(min(len(varnames), self.getfastscopelength())): - name = varnames[i] - w_value = fastscope_w[i] - w_name = self.space.wrap(name) - if w_value is not None: - self.space.setitem(self.w_locals, w_name, w_value) - else: - try: - self.space.delitem(self.w_locals, w_name) - except OperationError as e: - if not e.match(self.space, self.space.w_KeyError): - raise - - def locals2fast(self): - # Copy values from self.w_locals to the fastlocals - assert self.w_locals is not None - varnames = self.getcode().getvarnames() - numlocals = self.getfastscopelength() - - new_fastlocals_w = [None] * numlocals - - for i in range(min(len(varnames), numlocals)): - w_name = self.space.wrap(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: - new_fastlocals_w[i] = w_value - - self.setfastscope(new_fastlocals_w) diff --git a/pypy/interpreter/pyframe.py b/pypy/interpreter/pyframe.py --- a/pypy/interpreter/pyframe.py +++ b/pypy/interpreter/pyframe.py @@ -1,19 +1,19 @@ """ PyFrame class implementation with the interpreter main loop. """ +from rpython.rlib import jit +from rpython.rlib.debug import make_sure_not_resized, check_nonneg +from rpython.rlib.jit import hint +from rpython.rlib.objectmodel import we_are_translated, instantiate +from rpython.rlib.rarithmetic import intmask, r_uint from rpython.tool.pairtype import extendabletype -from pypy.interpreter import eval, pycode + +from pypy.interpreter import eval, pycode, pytraceback from pypy.interpreter.argument import Arguments +from pypy.interpreter.baseobjspace import W_Root from pypy.interpreter.error import OperationError, operationerrfmt from pypy.interpreter.executioncontext import ExecutionContext -from pypy.interpreter import pytraceback -from rpython.rlib.objectmodel import we_are_translated, instantiate -from rpython.rlib.jit import hint -from rpython.rlib.debug import make_sure_not_resized, check_nonneg -from rpython.rlib.rarithmetic import intmask, r_uint -from rpython.rlib import jit from pypy.tool import stdlib_opcode -from rpython.tool.stdlib_opcode import host_bytecode_spec # Define some opcodes used for op in '''DUP_TOP POP_TOP SETUP_LOOP SETUP_EXCEPT SETUP_FINALLY @@ -21,7 +21,8 @@ globals()[op] = stdlib_opcode.opmap[op] HAVE_ARGUMENT = stdlib_opcode.HAVE_ARGUMENT -class PyFrame(eval.Frame): + +class PyFrame(W_Root): """Represents a frame for a regular Python function that needs to be interpreted. @@ -56,8 +57,10 @@ "use space.FrameClass(), not directly PyFrame()") self = hint(self, access_directly=True, fresh_virtualizable=True) assert isinstance(code, pycode.PyCode) + self.space = space + self.w_globals = w_globals + self.w_locals = None self.pycode = code - eval.Frame.__init__(self, space, w_globals) self.locals_stack_w = [None] * (code.co_nlocals + code.co_stacksize) self.valuestackdepth = code.co_nlocals self.lastblock = None @@ -458,6 +461,59 @@ self.locals_stack_w[i] = scope_w[i] self.init_cells() + def getdictscope(self): + """ + Get the locals as a dictionary + """ + self.fast2locals() + return self.w_locals + + def setdictscope(self, w_locals): + """ + Initialize the locals from a dictionary. + """ + self.w_locals = w_locals + self.locals2fast() + + def fast2locals(self): + # Copy values from the fastlocals to self.w_locals + if self.w_locals is None: + self.w_locals = self.space.newdict() + varnames = self.getcode().getvarnames() + fastscope_w = self.getfastscope() + for i in range(min(len(varnames), self.getfastscopelength())): + name = varnames[i] + w_value = fastscope_w[i] + w_name = self.space.wrap(name) + if w_value is not None: + self.space.setitem(self.w_locals, w_name, w_value) + else: + try: + self.space.delitem(self.w_locals, w_name) + except OperationError as e: + if not e.match(self.space, self.space.w_KeyError): + raise + + def locals2fast(self): + # Copy values from self.w_locals to the fastlocals + assert self.w_locals is not None + varnames = self.getcode().getvarnames() + numlocals = self.getfastscopelength() + + new_fastlocals_w = [None] * numlocals + + for i in range(min(len(varnames), numlocals)): + w_name = self.space.wrap(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: + new_fastlocals_w[i] = w_value + + self.setfastscope(new_fastlocals_w) + def init_cells(self): """Initialize cellvars from self.locals_stack_w. This is overridden in nestedscope.py""" @@ -475,6 +531,12 @@ def _setcellvars(self, cellvars): pass + def fget_code(self, space): + return space.wrap(self.getcode()) + + def fget_getdictscope(self, space): + return self.getdictscope() + ### line numbers ### def fget_f_lineno(self, space): @@ -488,7 +550,7 @@ "Returns the line number of the instruction currently being executed." try: new_lineno = space.int_w(w_new_lineno) - except OperationError, e: + except OperationError: raise OperationError(space.w_ValueError, space.wrap("lineno must be an integer")) diff --git a/pypy/interpreter/test/test_eval.py b/pypy/interpreter/test/test_eval.py deleted file mode 100644 --- a/pypy/interpreter/test/test_eval.py +++ /dev/null @@ -1,69 +0,0 @@ - -from pypy.interpreter.eval import Frame -from pypy.interpreter.pycode import PyCode - - -class TestFrame: - def setup_method(self, method): - def c(x, y, *args): - pass - code = PyCode._from_code(self.space, c.func_code) - - class ConcreteFastscopeFrame(Frame): - - def __init__(self, space, code, numlocals): - self.code = code - Frame.__init__(self, space) - self.numlocals = numlocals - self._fastlocals_w = [None] * self.numlocals - - def getcode(self): - return self.code - - def setfastscope(self, scope_w): - self._fastlocals_w = scope_w - - def getfastscope(self): - return self._fastlocals_w - - def getfastscopelength(self): - return self.numlocals - - self.f = ConcreteFastscopeFrame(self.space, code, numlocals=5) - - - def test_fast2locals(self): - space = self.space - w = space.wrap - self.f.fast2locals() - assert space.eq_w(self.f.w_locals, self.space.wrap({})) - - self.f._fastlocals_w[0] = w(5) - self.f.fast2locals() - assert space.eq_w(self.f.w_locals, self.space.wrap({'x': 5})) - - self.f._fastlocals_w[2] = w(7) - self.f.fast2locals() - assert space.eq_w(self.f.w_locals, self.space.wrap({'x': 5, 'args': 7})) - - def sameList(self, l1, l2): - assert len(l1) == len(l2) - for w_1, w_2 in zip(l1, l2): - assert (w_1 is None) == (w_2 is None) - if w_1 is not None: - assert self.space.eq_w(w_1, w_2) - - def test_locals2fast(self): - w = self.space.wrap - self.f.w_locals = self.space.wrap({}) - self.f.locals2fast() - self.sameList(self.f._fastlocals_w, [None]*5) - - self.f.w_locals = self.space.wrap({'x': 5}) - self.f.locals2fast() - self.sameList(self.f._fastlocals_w, [w(5)] + [None]*4) - - self.f.w_locals = self.space.wrap({'x':5, 'args':7}) - self.f.locals2fast() - self.sameList(self.f._fastlocals_w, [w(5), None, w(7), - None, None]) diff --git a/pypy/interpreter/typedef.py b/pypy/interpreter/typedef.py --- a/pypy/interpreter/typedef.py +++ b/pypy/interpreter/typedef.py @@ -605,7 +605,7 @@ # # Definition of the type's descriptors for all the internal types -from pypy.interpreter.eval import Code, Frame +from pypy.interpreter.eval import Code from pypy.interpreter.pycode import PyCode, CO_VARARGS, CO_VARKEYWORDS from pypy.interpreter.pyframe import PyFrame from pypy.interpreter.pyopcode import SuspendedUnroller @@ -711,13 +711,6 @@ BuiltinCode.typedef.acceptable_as_base_class = False -Frame.typedef = TypeDef('internal-frame', - f_code = GetSetProperty(Frame.fget_code), - f_locals = GetSetProperty(Frame.fget_getdictscope), - f_globals = interp_attrproperty_w('w_globals', cls=Frame), - ) -Frame.typedef.acceptable_as_base_class = False - PyCode.typedef = TypeDef('code', __new__ = interp2app(PyCode.descr_code__new__.im_func), __eq__ = interp2app(PyCode.descr_code__eq__), @@ -756,7 +749,10 @@ f_exc_value = GetSetProperty(PyFrame.fget_f_exc_value), f_exc_traceback = GetSetProperty(PyFrame.fget_f_exc_traceback), f_restricted = GetSetProperty(PyFrame.fget_f_restricted), - **Frame.typedef.rawdict) + f_code = GetSetProperty(PyFrame.fget_code), + f_locals = GetSetProperty(PyFrame.fget_getdictscope), + f_globals = interp_attrproperty_w('w_globals', cls=PyFrame), +) PyFrame.typedef.acceptable_as_base_class = False Module.typedef = TypeDef("module", _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit