[pypy-svn] r9549 - in pypy/dist/pypy: interpreter interpreter/test objspace/flow
pedronis at codespeak.net
pedronis at codespeak.net
Tue Mar 1 17:40:17 CET 2005
Author: pedronis
Date: Tue Mar 1 17:40:17 2005
New Revision: 9549
Modified:
pypy/dist/pypy/interpreter/eval.py
pypy/dist/pypy/interpreter/nestedscope.py
pypy/dist/pypy/interpreter/pyframe.py
pypy/dist/pypy/interpreter/pyopcode.py
pypy/dist/pypy/interpreter/test/test_eval.py
pypy/dist/pypy/objspace/flow/framestate.py
pypy/dist/pypy/objspace/flow/model.py
Log:
Use None for undefined local variables as natural.
Change flow space accordingly.
Modified: pypy/dist/pypy/interpreter/eval.py
==============================================================================
--- pypy/dist/pypy/interpreter/eval.py (original)
+++ pypy/dist/pypy/interpreter/eval.py Tue Mar 1 17:40:17 2005
@@ -49,11 +49,6 @@
def getdocstring(self):
return None
-class UndefinedClass(object):
- pass
-UNDEFINED = UndefinedClass() # marker for undefined local variables
-
-
class Frame(Wrappable):
"""A frame is an environment supporting the execution of a code object.
Abstract base class."""
@@ -114,7 +109,7 @@
self.w_locals = self.space.newdict([])
varnames = self.code.getvarnames()
for name, w_value in zip(varnames, self.getfastscope()):
- if w_value is not UNDEFINED:
+ if w_value is not None:
w_name = self.space.wrap(name)
self.space.setitem(self.w_locals, w_name, w_value)
@@ -123,7 +118,7 @@
assert self.w_locals is not None
varnames = self.code.getvarnames()
- new_fastlocals_w = [UNDEFINED]*self.numlocals
+ new_fastlocals_w = [None]*self.numlocals
for name, i in zip(varnames, range(self.numlocals)):
w_name = self.space.wrap(varnames[i])
Modified: pypy/dist/pypy/interpreter/nestedscope.py
==============================================================================
--- pypy/dist/pypy/interpreter/nestedscope.py (original)
+++ pypy/dist/pypy/interpreter/nestedscope.py Tue Mar 1 17:40:17 2005
@@ -1,5 +1,4 @@
from pypy.interpreter.error import OperationError
-from pypy.interpreter.eval import UNDEFINED
from pypy.interpreter.pyopcode import PyInterpFrame
from pypy.interpreter import function, pycode
from pypy.interpreter.baseobjspace import Wrappable
@@ -7,17 +6,17 @@
class Cell(Wrappable):
"A simple container for a wrapped value."
- def __init__(self, w_value=UNDEFINED):
+ def __init__(self, w_value=None):
self.w_value = w_value
def clone(self):
return self.__class__(self.w_value)
def empty(self):
- return self.w_value is UNDEFINED
+ return self.w_value is None
def get(self):
- if self.w_value is UNDEFINED:
+ if self.w_value is None:
raise ValueError, "get() from an empty cell"
return self.w_value
@@ -25,13 +24,13 @@
self.w_value = w_value
def delete(self):
- if self.w_value is UNDEFINED:
+ if self.w_value is None:
raise ValueError, "delete() on an empty cell"
- self.w_value = UNDEFINED
+ self.w_value = None
def __repr__(self):
""" representation for debugging purposes """
- if self.w_value is UNDEFINED:
+ if self.w_value is None:
content = ""
else:
content = repr(self.w_value)
Modified: pypy/dist/pypy/interpreter/pyframe.py
==============================================================================
--- pypy/dist/pypy/interpreter/pyframe.py (original)
+++ pypy/dist/pypy/interpreter/pyframe.py Tue Mar 1 17:40:17 2005
@@ -39,7 +39,7 @@
if code.dictscope_needed():
self.w_locals = space.newdict([]) # set to None by Frame.__init__
- self.fastlocals_w = [eval.UNDEFINED]*self.numlocals
+ self.fastlocals_w = [None]*self.numlocals
def getfastscope(self):
"Get the fast locals as a list."
Modified: pypy/dist/pypy/interpreter/pyopcode.py
==============================================================================
--- pypy/dist/pypy/interpreter/pyopcode.py (original)
+++ pypy/dist/pypy/interpreter/pyopcode.py Tue Mar 1 17:40:17 2005
@@ -5,7 +5,6 @@
"""
from pypy.interpreter.baseobjspace import OperationError, BaseWrappable
-from pypy.interpreter.eval import UNDEFINED
from pypy.interpreter import gateway, function
from pypy.interpreter import pyframe, pytraceback
from pypy.interpreter.miscutils import InitializedClass
@@ -92,7 +91,7 @@
def LOAD_FAST(f, varindex):
# access a local variable directly
w_value = f.fastlocals_w[varindex]
- if w_value is UNDEFINED:
+ if w_value is None:
varname = f.getlocalvarname(varindex)
message = "local variable '%s' referenced before assignment" % varname
raise OperationError(f.space.w_UnboundLocalError, f.space.wrap(message))
@@ -466,11 +465,12 @@
f.valuestack.push(w_value)
def DELETE_FAST(f, varindex):
- if f.fastlocals_w[varindex] is UNDEFINED:
+ if f.fastlocals_w[varindex] is None:
varname = f.getlocalvarname(varindex)
message = "local variable '%s' referenced before assignment" % varname
raise OperationError(f.space.w_UnboundLocalError, f.space.wrap(message))
- f.fastlocals_w[varindex] = UNDEFINED
+ f.fastlocals_w[varindex] = None
+
def BUILD_TUPLE(f, itemcount):
items = [f.valuestack.pop() for i in range(itemcount)]
Modified: pypy/dist/pypy/interpreter/test/test_eval.py
==============================================================================
--- pypy/dist/pypy/interpreter/test/test_eval.py (original)
+++ pypy/dist/pypy/interpreter/test/test_eval.py Tue Mar 1 17:40:17 2005
@@ -1,6 +1,6 @@
import autopath
-from pypy.interpreter.eval import Frame, UNDEFINED
+from pypy.interpreter.eval import Frame
from pypy.interpreter.pycode import PyCode
@@ -14,7 +14,7 @@
def __init__(self, space, code, numlocals):
Frame.__init__(self, space, code, numlocals=numlocals)
- self.fastlocals_w = [UNDEFINED] * self.numlocals
+ self.fastlocals_w = [None] * self.numlocals
def setfastscope(self, scope_w):
self.fastlocals_w = scope_w
@@ -45,24 +45,24 @@
def sameList(self, l1, l2):
assert len(l1) == len(l2)
for w_1, w_2 in zip(l1, l2):
- assert (w_1 is UNDEFINED) == (w_2 is UNDEFINED)
- if w_1 is not UNDEFINED:
+ 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.newdict([])
self.f.locals2fast()
- self.sameList(self.f.fastlocals_w, [UNDEFINED]*5)
+ self.sameList(self.f.fastlocals_w, [None]*5)
self.f.w_locals = self.space.newdict([
(w('x'), w(5))])
self.f.locals2fast()
- self.sameList(self.f.fastlocals_w, [w(5)] + [UNDEFINED]*4)
+ self.sameList(self.f.fastlocals_w, [w(5)] + [None]*4)
self.f.w_locals = self.space.newdict([
(w('x'), w(5)),
(w('args'), w(7))])
self.f.locals2fast()
- self.sameList(self.f.fastlocals_w, [w(5), UNDEFINED, w(7),
- UNDEFINED, UNDEFINED])
+ self.sameList(self.f.fastlocals_w, [w(5), None, w(7),
+ None, None])
Modified: pypy/dist/pypy/objspace/flow/framestate.py
==============================================================================
--- pypy/dist/pypy/objspace/flow/framestate.py (original)
+++ pypy/dist/pypy/objspace/flow/framestate.py Tue Mar 1 17:40:17 2005
@@ -7,7 +7,13 @@
def __init__(self, state):
if isinstance(state, PyFrame):
- data = state.getfastscope() + state.valuestack.items
+ data = []
+ for w in state.getfastscope():
+ if w is None:
+ data.append(UNDEFINED)
+ else:
+ data.append(w)
+ data.extend(state.valuestack.items)
if state.last_exception is None:
data.append(Constant(None))
data.append(Constant(None))
@@ -35,7 +41,13 @@
fastlocals = len(frame.fastlocals_w)
data = self.mergeable[:]
recursively_unflatten(frame.space, data)
- frame.setfastscope(data[:fastlocals])
+ fastscope = []
+ for w in data[:fastlocals]:
+ if w is UNDEFINED:
+ fastscope.append(None)
+ else:
+ fastscope.append(w)
+ frame.setfastscope(fastscope)
frame.valuestack.items[:] = data[fastlocals:-2]
if data[-2] == Constant(None):
assert data[-1] == Constant(None)
Modified: pypy/dist/pypy/objspace/flow/model.py
==============================================================================
--- pypy/dist/pypy/objspace/flow/model.py (original)
+++ pypy/dist/pypy/objspace/flow/model.py Tue Mar 1 17:40:17 2005
@@ -195,13 +195,14 @@
flags = ''
return '(%s%s)' % (r, flags)
-# hack! it is useful to have UNDEFINED be an instance of Constant too.
-# PyFrame then automatically uses this Constant as a marker for
-# non-initialized variables.
-from pypy.interpreter.eval import UNDEFINED
-UndefinedConstant = UNDEFINED.__class__
-UndefinedConstant.__bases__ += (Constant,)
-Constant.__init__(UNDEFINED, None)
+class UndefinedConstant(Constant):
+ def __init__(self):
+ Constant.__init__(self,object())
+
+ def __repr__(self):
+ return '(*undefined*)'
+
+UNDEFINED = UndefinedConstant()
class SpaceOperation:
def __init__(self, opname, args, result):
More information about the Pypy-commit
mailing list