[Zope-Checkins] CVS: Zope2 - Eval.py:1.1.2.4 RestrictionMutator.py:1.1.2.8
shane@digicool.com
shane@digicool.com
Wed, 25 Apr 2001 16:25:26 -0400 (EDT)
Update of /cvs-repository/Zope2/lib/python/RestrictedPython
In directory korak:/tmp/cvs-serv27164
Modified Files:
Tag: RestrictedPythonBranch
Eval.py RestrictionMutator.py
Log Message:
Added test of RestrictionCapableEval
--- Updated File Eval.py in package Zope2 --
--- Eval.py 2001/04/25 02:51:31 1.1.2.3
+++ Eval.py 2001/04/25 20:25:25 1.1.2.4
@@ -89,21 +89,19 @@
from string import translate, strip
import string
-compile_restricted = None
+compile_restricted_eval = None
nltosp = string.maketrans('\r\n',' ')
-default_globals={
- '__builtins__': {},
- '_guard': (lambda ob: ob),
- }
+_default_guard = lambda ob: ob # No restrictions.
-class RestrictedEval:
- """Holds a reference to compiled restricted code.
+class RestrictionCapableEval:
+ """A base class for restricted code.
"""
+ globals = {'__builtins__': {}}
- def __init__(self, expr, globals=default_globals):
+ def __init__(self, expr):
"""Create a restricted expression
where:
@@ -112,8 +110,8 @@
globals -- A global namespace.
"""
- global compile_restricted
- if compile_restricted is None:
+ global compile_restricted_eval
+ if compile_restricted_eval is None:
# Late binding because this will import the whole
# compiler suite.
from RestrictionMutator import compile_restricted_eval
@@ -122,17 +120,16 @@
self.__name__ = expr
expr = translate(expr, nltosp)
self.expr = expr
- self.globals = globals
self.code, err, warn, used = compile_restricted_eval(expr, '<string>')
if err:
raise SyntaxError, err[0]
self.used = tuple(used.keys())
def eval(self, mapping):
- d = {}
+ d = {'_read_guard': _default_guard}
for name in self.used:
try:
- d[name] = mapping.getitem(name, 0)
+ d[name] = mapping[name]
except KeyError:
# Swallow KeyErrors since the expression
# might not actually need the name. If it
@@ -141,5 +138,5 @@
return eval(self.code, self.globals, d)
def __call__(self, **kw):
- return eval(self.code, self.globals, kw)
+ return self.eval(kw)
--- Updated File RestrictionMutator.py in package Zope2 --
--- RestrictionMutator.py 2001/04/25 02:51:31 1.1.2.7
+++ RestrictionMutator.py 2001/04/25 20:25:25 1.1.2.8
@@ -56,9 +56,8 @@
class RestrictionMutator:
- funcinfo = None
-
def __init__(self):
+ self.funcinfo = FuncInfo()
self.warnings = []
self.errors = []
self.used_names = {}