* Got rid of PythonExpr in favour of zope.tales.pythonexpr.

* Made ZRPythonExpr subclass the one from zope.tales.pythonexpr.
* Made Expressions use ZRPythonExpr instead of the standard one
  from zope.tales.pythonexpr.
parent 17f98b70
......@@ -21,25 +21,21 @@ from zope.tales.tales import ExpressionEngine, Context
from zope.tales.expressions import PathExpr, StringExpr, NotExpr
from zope.tales.expressions import DeferExpr, SubPathExpr
from zope.tales.expressions import SimpleModuleImporter
from zope.tales.pythonexpr import PythonExpr
from zope.i18n import translate
from zope.traversing.adapters import traversePathElement
from zExceptions import NotFound, Unauthorized
from OFS.interfaces import ITraversable
from Products.PageTemplates.DeferExpr import LazyExpr
from Products.PageTemplates.GlobalTranslationService import getGlobalTranslationService
from Products.PageTemplates.ZRPythonExpr import _SecureModuleImporter
from Products.PageTemplates.ZRPythonExpr import PythonExpr, _SecureModuleImporter
SecureModuleImporter = _SecureModuleImporter()
# BBB 2005/05/01 -- remove after 12 months
import zope.deferredimport
zope.deferredimport.deprecatedFrom(
"Use the Zope 3 ZPT engine instead of the Zope 2 one. Expression "
"types can be imported from zope.tales.expressions. This reference "
"will be gone in Zope 2.12.",
"zope.tales.expressions",
"StringExpr", "NotExpr"
import zope.deprecation
zope.deprecation.deprecated(
("StringExpr", "NotExpr", "PathExpr", "SubPathExpr"),
"Zope 2 uses the Zope 3 ZPT engine now. Expression types can be "
"imported from zope.tales.expressions."
)
def boboTraverseAwareSimpleTraverse(object, path_items, econtext):
......
......@@ -14,71 +14,11 @@
$Id$
"""
from TALES import CompilerError
from sys import exc_info
from zope.tales.expressions import DeferWrapper
class getSecurityManager:
'''Null security manager'''
def validate(self, *args, **kwargs):
return 1
addContext = removeContext = validate
class PythonExpr:
def __init__(self, name, expr, engine):
self.expr = expr = expr.strip().replace('\n', ' ')
try:
d = {}
exec 'def f():\n return %s\n' % expr.strip() in d
self._f = d['f']
except:
raise CompilerError, ('Python expression error:\n'
'%s: %s') % exc_info()[:2]
self._get_used_names()
def _get_used_names(self):
self._f_varnames = vnames = []
for vname in self._f.func_code.co_names:
if vname[0] not in '$_':
vnames.append(vname)
def _bind_used_names(self, econtext, _marker=[]):
# Bind template variables
names = {'CONTEXTS': econtext.contexts}
vars = econtext.vars
getType = econtext.getCompiler().getTypes().get
for vname in self._f_varnames:
val = vars.get(vname, _marker)
if val is _marker:
has = val = getType(vname)
if has:
val = ExprTypeProxy(vname, val, econtext)
names[vname] = val
else:
names[vname] = val
for key, val in names.items():
if isinstance(val, DeferWrapper):
names[key] = val()
return names
def __call__(self, econtext):
__traceback_info__ = self.expr
f = self._f
f.func_globals.update(self._bind_used_names(econtext))
return f()
def __str__(self):
return 'Python expression "%s"' % self.expr
def __repr__(self):
return '<PythonExpr %s>' % self.expr
class ExprTypeProxy:
'''Class that proxies access to an expression type handler'''
def __init__(self, name, handler, econtext):
self._name = name
self._handler = handler
self._econtext = econtext
def __call__(self, text):
return self._handler(self._name, text,
self._econtext.getCompiler())(self._econtext)
# BBB 2005/05/01 -- remove after 12 months
import zope.deferredimport
zope.deferredimport.deprecatedFrom(
"Zope 2 uses the Zope 3 ZPT engine now. The PythonExpr type can be "
"imported from zope.tales.pythonexpr.",
"zope.tales.pythonexpr",
"PythonExpr", "ExprTypeProxy"
)
......@@ -19,9 +19,8 @@ $Id$
from AccessControl import safe_builtins
from AccessControl.ZopeGuards import guarded_getattr, get_safe_globals
from RestrictedPython import compile_restricted_eval
from TALES import CompilerError
from PythonExpr import PythonExpr
from zope.tales.tales import CompilerError
from zope.tales.pythonexpr import PythonExpr
class PythonExpr(PythonExpr):
_globals = get_safe_globals()
......@@ -29,20 +28,19 @@ class PythonExpr(PythonExpr):
_globals['__debug__' ] = __debug__
def __init__(self, name, expr, engine):
self.expr = expr = expr.strip().replace('\n', ' ')
code, err, warn, use = compile_restricted_eval(expr, str(self))
self.text = text = expr.strip().replace('\n', ' ')
code, err, warn, use = compile_restricted_eval(text, str(self))
if err:
raise CompilerError, ('Python expression error:\n%s' %
'\n'.join(err) )
self._f_varnames = use.keys()
raise engine.getCompilerError()('Python expression error:\n%s' %
'\n'.join(err))
self._varnames = use.keys()
self._code = code
def __call__(self, econtext):
__traceback_info__ = self.expr
code = self._code
g = self._bind_used_names(econtext)
g.update(self._globals)
return eval(code, g, {})
__traceback_info__ = self.text
vars = self._bind_used_names(econtext, {})
vars.update(self._globals)
return eval(self._code, vars, {})
class _SecureModuleImporter:
__allow_access_to_unprotected_subobjects__ = 1
......
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