Commit 2732042e authored by Jim Fulton's avatar Jim Fulton

Removed validate attribute.

Added template dict to override arguments.
parent 36a17903
"""Very Safe Python Expressions
"""
__rcs_id__='$Id: VSEval.py,v 1.2 1997/10/27 17:40:35 jim Exp $'
__rcs_id__='$Id: VSEval.py,v 1.3 1997/10/28 21:51:20 jim Exp $'
############################################################################
# Copyright
......@@ -55,7 +55,7 @@ __rcs_id__='$Id: VSEval.py,v 1.2 1997/10/27 17:40:35 jim Exp $'
# (540) 371-6909
#
############################################################################
__version__='$Revision: 1.2 $'[11:-2]
__version__='$Revision: 1.3 $'[11:-2]
from string import join
import new, sys
......@@ -75,7 +75,6 @@ class Eval:
or to disallow or limit attribute access.
"""
validate=None
def __init__(self, expr, custom=None, globals={'__builtins__':{}}, **kw):
"""Create a 'safe' expression
......@@ -101,8 +100,6 @@ class Eval:
custom=d
else: custom=kw
if custom.has_key('validate'): self.validate=custom['validate']
self.expr=expr
self.globals=globals
......@@ -136,8 +133,12 @@ class Eval:
raise TypeError, ("illegal operation, %s, in %s"
% (name, expr))
# custop(op1,op2,env)
self._const(out, custop, consts)
self._bfunc(out)
out.append(ROT_THREE)
envpos=self._push_env(out, envpos, names)
out[len(out):]=[CALL_FUNCTION, 3, 0]
i=i+1
elif unops.has_key(c) and custom.has_key(unops[c]):
......@@ -147,9 +148,11 @@ class Eval:
raise TypeError, ("illegal operation, %s, in %s"
% (name, expr))
# Old code, simple getattr insertion
# custop(op,env)
self._const(out, custop, consts)
self._ufunc(out)
out.append(ROT_TWO)
envpos=self._push_env(out, envpos, names)
out[len(out):]=[CALL_FUNCTION, 2, 0]
i=i+1
elif c==LOAD_ATTR and custom.has_key('__getattr__'):
......@@ -161,42 +164,16 @@ class Eval:
Attribute access is not allowed.
"""
% expr)
# Old code that uses a simple getattr method:
#self._const(out, names[ord(code[i+1])+256*ord(code[i+2])],
# consts)
#self._const(out, custop, consts)
#self._bfunc(out)
# New code that assumes a three-parameter method that
# take's three arguments, an environment object, an
# instance, and a name
# push name of special environment variable, '_env':
if envpos < 0:
envpos=len(names)
names.append('_env')
out.append(LOAD_NAME)
out.append(envpos%256)
out.append(envpos/256)
out.append(ROT_TWO)
# push function:
# custop(inst,name,env)
self._const(out, custop, consts)
out.append(ROT_THREE)
# push name as constant:
out.append(ROT_TWO)
self._const(out, names[ord(code[i+1])+256*ord(code[i+2])],
consts)
# Call function w 3 args
out.append(CALL_FUNCTION)
out.append(3)
out.append(0)
envpos=self._push_env(out, envpos, names)
out[len(out):]=[CALL_FUNCTION, 3, 0]
i=i+3
elif c==LOAD_NAME:
out.append(c)
out.append(ord(code[i+1]))
......@@ -231,28 +208,22 @@ class Eval:
out.append(i%256)
out.append(i/256)
def _bfunc(self, out):
out.append(ROT_THREE)
out.append(CALL_FUNCTION)
out.append(2)
out.append(0)
def _ufunc(self, out):
out.append(ROT_TWO)
out.append(CALL_FUNCTION)
out.append(1)
out.append(0)
def _push_env(self, out, envpos, names):
# push name of special environment variable, '_env':
if envpos < 0:
envpos=len(names)
names.append('_env')
out.append(LOAD_NAME)
out.append(envpos%256)
out.append(envpos/256)
return envpos
def eval(self, mapping):
d={'_env': mapping}
code=self.code
validate=self.validate
for name in self.used:
try:
v=mapping[name]
if validate is not None: validate(name, v)
d[name]=v
except: pass
try: d[name]=mapping.get(name,0)
except KeyError: pass
return eval(code,self.globals,d)
......@@ -297,6 +268,9 @@ BINARY_ADD=23
BINARY_SUBTRACT=24
BINARY_SUBSCR=25
SLICE= 30
SLICE_b_= 31
SLICE__e= 32
SLICE_b_e= 33
STORE_SLICE=40
DELETE_SLICE=50
STORE_SUBSCR=60
......@@ -443,6 +417,10 @@ if __name__=='__main__': globals()[sys.argv[1]]()
############################################################################
#
# $Log: VSEval.py,v $
# Revision 1.3 1997/10/28 21:51:20 jim
# Removed validate attribute.
# Added template dict to override arguments.
#
# Revision 1.2 1997/10/27 17:40:35 jim
# Added some new experimental validation machinery.
# This is, still a work in progress.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment