[Zope] Evaluating python with my own variable bindings

Ross Boylan RossBoylan@stanfordalumni.org
Sun, 11 Aug 2002 11:03:38 -0700


On Sun, Aug 11, 2002 at 09:36:05AM -0700, Ross Boylan wrote:
[snip]
> In particular, how do I get my namespace to the function?  Here's my
> guess at how to use it:
>   c= globals().copy()
>   c.update({'Q2':0, 'Q3':1})
>   e= PythonExpr('','Q2 or Q3', None)
>   result = e(c)
> 
> Is that right?  (I want to evaluate Q2 or Q3 with those symbols bound
> to false and true respectively).
> 

No, it doesn't work.  Here's what happened:
ross@wheat:/usr/lib/zope/lib/python$ python
Python 2.1.3 (#1, Jul 29 2002, 22:34:51)
[GCC 2.95.4 20011002 (Debian prerelease)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> from Products.PageTemplates.ZRPythonExpr import PythonExpr
>>> e=PythonExpr('','Q1 or Q2', None)
>>> c=globals().copy()
>>> c.update({'Q1':1, 'Q2':0})
>>> c
{'__doc__': None, '__name__': '__main__', '__builtins__': <module '__builtin__' (built-in)>, 'PythonExpr': <class Products.PageTemplates.ZRPythonExpr.PythonExpr at 0x82b19ec>, 'e': <PythonExpr Q1
or Q2>, 'Q2': 0, 'Q1': 1}
>>> e(c)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/zope/lib/python/Products/PageTemplates/ZRPythonExpr.py", line 47, in __call__
    g = self._bind_used_names(econtext)
  File "/usr/lib/zope/lib/python/Products/PageTemplates/PythonExpr.py", line 50, in _bind_used_names
    vars = econtext.vars
AttributeError: vars


The solution is not immediately obvious to me.  I see the econtext
passed in (where i put c) needs not only .vars but ._engine and some
other stuff (in PythonExpr.py).