[Zope-Checkins] CVS: Zope2 - Compilers.py:1.1.2.3
shane@digicool.com
shane@digicool.com
Thu, 26 Apr 2001 17:58:34 -0400 (EDT)
Update of /cvs-repository/Zope2/lib/python/RestrictedPython
In directory korak:/tmp/cvs-serv8990
Modified Files:
Tag: RestrictedPythonBranch
Compilers.py
Log Message:
Put back old code (but disabled) for speed comparison
--- Updated File Compilers.py in package Zope2 --
--- Compilers.py 2001/04/26 19:13:39 1.1.2.2
+++ Compilers.py 2001/04/26 21:58:33 1.1.2.3
@@ -149,7 +149,7 @@
visitor.walk(tree, gen)
return gen.getCode(), (), rm.warnings, rm.used_names
-def compile_restricted_exec(s, filename='<string>'):
+def compile_restricted_exec(s, filename='<string>', nested_scopes=1):
'''Compile a restricted code suite.'''
rm = RestrictionMutator()
tree, err = tryParsing(s, 'exec')
@@ -157,13 +157,18 @@
MutatingWalker.walk(tree, rm)
if rm.errors:
return None, rm.errors, rm.warnings, rm.used_names
- gen = pycodegen.NestedScopeModuleCodeGenerator(filename)
+ if nested_scopes:
+ gen = pycodegen.NestedScopeModuleCodeGenerator(filename)
+ else:
+ gen = pycodegen.ModuleCodeGenerator(filename)
visitor.walk(tree, gen)
return gen.getCode(), (), rm.warnings, rm.used_names
-def compile_restricted_eval(s, filename='<string>'):
+if 0:
+ def compile_restricted_eval(s, filename='<string>', nested_scopes=0):
'''Compile a restricted expression.'''
- r = compile_restricted_exec('def f(): return \\\n' + s, filename)
+ r = compile_restricted_exec('def f(): return \\\n' + s, filename,
+ nested_scopes)
err = r[1]
if err:
err.pop(0) # Discard first line of error
@@ -171,6 +176,26 @@
# Extract the code object representing the function body
r = (r[0].co_consts[1],) + r[1:]
return r
+
+else:
+
+ def compile_restricted_eval(s, filename='<string>'):
+ '''Compile a restricted expression.'''
+ rm = RestrictionMutator()
+ tree, err = tryParsing(s, 'eval')
+ if err:
+ err[1].pop(0) # Discard first line of error
+ return err
+ MutatingWalker.walk(tree, rm)
+ if rm.errors:
+ return None, rm.errors, rm.warnings, rm.used_names
+ # XXX No "EvalCodeGenerator" exists
+ # so here's a hack that gets around it.
+ gen = pycodegen.ModuleCodeGenerator(filename)
+ gen.emit('SET_LINENO', 0)
+ visitor.walk(tree, gen)
+ gen.emit('RETURN_VALUE')
+ return gen.getCode(), (), rm.warnings, rm.used_names
DEBUG = 0
def compile_restricted(source, filename, mode):