[Zope] Globals disappearing in extmethod?

Thomas B. Passin tpassin@mitretek.org
Mon, 4 Feb 2002 13:12:14 -0500


You need to declare "global Request' in the definition of getFormData(),
too.  If it's defined in another module, remember that "global" means
"global withing the module", not "global within all the Python code".

I'm not clear why it works sometimes, though. I'd suggest passing the
request explictly; it wouldn't affect your maintaining the state of the
module:

 def getFormData(key,Request):
     return Request.get(key) #Don't need the "global"

Cheers

Tom P

[Joseph A Knapka]

> It may be that I am misunderstanding something fundamental.
> I have code that does essentially the following:
>
> (1) Defines an external method for processing form data:
>
> # In module MyModule:
> def myExtMethod(req):
>
>     global Request
>     Request = req
>
>     action = getFormData("action")
>     #...process request...
>
> # And a utility function to help out - this is not an
> # external method, but is used by myExtMethod.
> def getFormData(key):
>     return Request.get(key)
>
> (2) Defines a DTML document that just passes its input
> along to myExtMethod():
>
> ...
>
> <dtml-var expr="myExtMethod(REQUEST)">
> ...
>
> The point of this is that myExtMethod() is a wrapper
> around some old CGI code; I want the state of MyModule
> to be preserved between invocations of myExtMethod(),
> for performance reasons. I've been working with Zope
> for only a very short time (days), so I don't know if
> this is the "right" way to do this; I'd appreciate any
> advice on this matter.
>
> Now the problem. This code works most of the time.
> However, apparently at random, when executing
> a request, I will get an exception from getFormData():
> "global name Request does not exist." This is very
> strange, because /every/ invocation of getFormData()
> is called from myExtMethod(), so there's simply no way
> the Request global can be unset, as far as I can see.
>