[Zope] Globals disappearing in extmethod?

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


[Joseph A Knapka]

> "Thomas B. Passin" wrote:
> >
> > You need to declare "global Request' in the definition of getFormData(),
> > too.
>
> No. In Python, the rule is "any variable not assigned in the body
> of a function is presumed to be a module global" (though this may
> change in the presence of nested scopes in Python 2.2).
>

Yes, I overlooked the fact that you only referred to the Request and didn't
assign to it.

> It appears that global state in Zope is just not handled the way I,
> as a Python programmer, expect. For example:
>
> # In module xtest:
> myVar = 0
>
> # External method:
> def getAndIncMyVar():
>   global myVar
>   myVar = myVar + 1
>   return "The value of myVar is now %d"%myVar
>
> # In DTML document myVarTest:
> <standard_html_header>
> <dtml-var expr="getAndIncMyVar()">
> <standard_html_footer>
>
> If you go to the /myVarTest URL and hit "Refresh Page"
> a couple of dozen times, you will see the reported
> value of myVar jump around. It will increase for a
> while, then jump backward an apparently random number.
> The violates my (Python-centered) expectations
> most horribly, and I'd really like to understand it.
>

I just did it 110 times and it incremented normally each time.  This is on
Windows 2000, IE5.5, Zope 2.3.3.  Then I loaded it into Netscape 4.73 and it
picked up where it had left off, and it incremented for another 20 tries.
So it seems to work as expected on my system.

> Are external-method modules automaically reloaded
> periodically, or something like that?
>

They would if your service died and restarted.  You'd think it would  reset
to zero, though.  Could there be some caching shenanigans going on instead?

Cheers,

Tom P