[Zope] Verrryy strange behavior (NameError)

Andrew Athan aathan-zope-list%REMOVEME@memeplex.com
Fri, 6 Apr 2001 16:01:01 -0400


This is a multi-part message in MIME format.

------=_NextPart_000_00FD_01C0BEB2.C6C2B880
Content-Type: text/plain;
	charset="Windows-1252"
Content-Transfer-Encoding: 7bit


I'm sending this in case my sagga helps some other relative newcomer.

I suspect this problem has something to do with python1.5.2's (or Zope's
hackery of...) handling of namespaces within modules & classes.  What I
needed was access to a "static global" variable to store some state for me.
(No, I haven't looked into threads, their existance, or such issues in
Zope).  What I was doing was:

_stateVariable=0
....
class ....
def fun()
   ....
   _stateVariable=1


and this was causing me all kinds of grief.  By changing the code to

_stateVariable = {'a':0}
...
class ...
def fun()
   ...
   _stateVariable['a']=1

everything works just fine.  Now, can someone give me the shortcut to
understand WHHHYYY?

A.

  -----Original Message-----
  From: zope-admin@zope.org [mailto:zope-admin@zope.org]On Behalf Of Andrew
Athan
  Sent: Friday, April 06, 2001 3:50 PM
  To: zope@zope.org
  Subject: RE: [Zope] Verrryy strange behavior (NameError)



  Ok, folks, things just got really strange indeed.

  The "bad" NameError behavior seems highly dependent on a line THAT NEVER
EXECUTES and which occurs much AFTER where I get the exception.  Namely,
later on in the validate() function is the following clause (yes, I used the
eval bit so that the Python byte compiler, if it does invariant
optimizations, doesn't remove the code in the if).

  Near line 11...
  _xxxxxxxxxxxxx='sss'

  Near line 236...
          print 'testing=%s'%_xxxxxxxxxxxxx

  Near line 280...
          if hasattr(self,'loginForm') and \
             response.unauthorized.__name__=='unauthorized':
              if eval('1=1')==0:
                  print 'arghhhhhhhhhhhhhhhhhhhhhhh'
                  _xxxxxxxxxxxxx = 1

  As long as this code exists in the .py file, I get the name error.
arghhhhhhhhhhhhhhhh never prints, ever (other print statements above it do).
If I comment the _xxxxxxxxxxxx = 1 out, or change it to a different name,
the NameError exception does not occur at line 236.  If I leave it in, the
exception occurs.

  Can you say "voodoo"?
  A.


    -----Original Message-----
    From: zope-admin@zope.org [mailto:zope-admin@zope.org]On Behalf Of
Andrew Athan
    Sent: Friday, April 06, 2001 3:21 PM
    To: zope@zope.org
    Subject: [Zope] Verrryy strange behavior (NameError)



    I'm getting an intermittent NameError trying to access a variable set in
the external scope of LoginManager.py .  I call my variable _x and set it
directly above where _LogginInUser gets set.  I then try to access it
directly above a line that accesses _LoggingInUser gets accessed.  I just do
a simple print 'testing=%s'%_x.  I get an exception.  The line that accesses
_LoggingInUser doesn't.

    What gives?


    Now get this ... if I change the name to (13x's) _xxxxxxxxxxxxx , it
works.

    If I then change the name to (6x's) _xxxxxx, it continues to work.

    If I then change the name to (3x's) _xxx, it continues to work.
    (2x's) _xx works
    (1x's) _x doesn't work!!!!

    If I change the line near the top of the file from
    _xx='sss'
    to
    _xx=0
    and change the print to use a %d  I get the NameError.

    If I change the line back to _xx='sss' and the print back to a %s I
continue to get the NameError ... even though that exact combination had
worked a minute before!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    If I then change the variable back to have 13x's it starts working
again.  Is there some deep problem with the python byte compiler or access
control mangling features of Zope?  Or am I missing something stupid?

    What in the world is going on?
    A.

    Traceback (innermost last):
      File D:\CloakMail\lib\python\ZPublisher\Publish.py, line 238, in
publish_module
      File D:\CloakMail\lib\python\ZPublisher\Publish.py, line 199, in
publish
      File D:\CLOAKM~1\lib\python\Zope\__init__.py, line 221, in
zpublisher_exception_hook
        (Object: Traversable)
      File D:\CloakMail\lib\python\ZPublisher\Publish.py, line 165, in
publish
      File D:\CloakMail\lib\python\ZPublisher\BaseRequest.py, line 450, in
traverse
      File D:\CloakMail\lib\python\Products\LoginManager\LoginManager.py,
line 236, in validate
        (Object: ProviderContainer)
    NameError: (see above)





------=_NextPart_000_00FD_01C0BEB2.C6C2B880
Content-Type: text/html;
	charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Dwindows-1252">
<META content=3D"MSHTML 5.50.4611.1300" name=3DGENERATOR></HEAD>
<BODY>
<DIV><FONT face=3DArial color=3D#0000ff size=3D2></FONT>&nbsp;</DIV>
<DIV><SPAN class=3D464135619-06042001><FONT face=3DArial color=3D#0000ff =
size=3D2>I'm=20
sending this in case my sagga helps some other relative=20
newcomer.</FONT></SPAN></DIV>
<DIV><SPAN class=3D464135619-06042001><FONT face=3DArial color=3D#0000ff =

size=3D2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=3D464135619-06042001><FONT face=3DArial color=3D#0000ff =
size=3D2>I=20
suspect this problem has something to do with python1.5.2's (or Zope's =
hackery=20
of...) handling of namespaces within modules &amp; classes.&nbsp; What I =
needed=20
was access to a "static global" variable to store some state for =
me.&nbsp; (No,=20
I haven't looked into threads, their existance, or such issues in =
Zope).&nbsp;=20
What I was doing was:</FONT></SPAN></DIV>
<DIV><SPAN class=3D464135619-06042001><FONT face=3DArial color=3D#0000ff =

size=3D2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=3D464135619-06042001><FONT face=3DArial color=3D#0000ff =

size=3D2>_stateVariable=3D0</FONT></SPAN></DIV>
<DIV><SPAN class=3D464135619-06042001><FONT face=3DArial color=3D#0000ff =

size=3D2>....</FONT></SPAN></DIV>
<DIV><SPAN class=3D464135619-06042001><FONT face=3DArial color=3D#0000ff =
size=3D2>class=20
....</FONT></SPAN></DIV>
<DIV><SPAN class=3D464135619-06042001><FONT face=3DArial color=3D#0000ff =
size=3D2>def=20
fun()</FONT></SPAN></DIV>
<DIV><SPAN class=3D464135619-06042001><FONT face=3DArial color=3D#0000ff =

size=3D2>&nbsp;&nbsp; ....</FONT></SPAN></DIV>
<DIV><SPAN class=3D464135619-06042001><FONT face=3DArial color=3D#0000ff =

size=3D2>&nbsp;&nbsp; _stateVariable=3D1</FONT></SPAN></DIV>
<DIV><SPAN class=3D464135619-06042001><FONT face=3DArial color=3D#0000ff =

size=3D2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=3D464135619-06042001><FONT face=3DArial color=3D#0000ff =

size=3D2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=3D464135619-06042001><FONT face=3DArial color=3D#0000ff =
size=3D2>and=20
this was causing me all kinds of grief.&nbsp; By changing the code=20
to</FONT></SPAN></DIV>
<DIV><SPAN class=3D464135619-06042001><FONT face=3DArial color=3D#0000ff =

size=3D2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=3D464135619-06042001><FONT face=3DArial color=3D#0000ff =

size=3D2>_stateVariable =3D {'a':0}</FONT></SPAN></DIV>
<DIV><SPAN class=3D464135619-06042001><FONT face=3DArial color=3D#0000ff =

size=3D2>...</FONT></SPAN></DIV>
<DIV><SPAN class=3D464135619-06042001><FONT face=3DArial color=3D#0000ff =
size=3D2>class=20
...</FONT></SPAN></DIV>
<DIV><SPAN class=3D464135619-06042001><FONT face=3DArial color=3D#0000ff =
size=3D2>def=20
fun()</FONT></SPAN></DIV>
<DIV><SPAN class=3D464135619-06042001><FONT face=3DArial color=3D#0000ff =

size=3D2>&nbsp;&nbsp; ...</FONT></SPAN></DIV>
<DIV><SPAN class=3D464135619-06042001><FONT face=3DArial color=3D#0000ff =

size=3D2>&nbsp;&nbsp; _stateVariable['a']=3D1</FONT></SPAN></DIV>
<DIV><SPAN class=3D464135619-06042001><FONT face=3DArial color=3D#0000ff =

size=3D2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=3D464135619-06042001><FONT face=3DArial color=3D#0000ff =

size=3D2>everything works just fine.&nbsp; Now, can someone give me the =
shortcut=20
to understand WHHHYYY?</FONT></SPAN></DIV>
<DIV><SPAN class=3D464135619-06042001><FONT face=3DArial color=3D#0000ff =

size=3D2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=3D464135619-06042001><FONT face=3DArial color=3D#0000ff =

size=3D2>A.</FONT></SPAN></DIV>
<DIV><SPAN class=3D464135619-06042001></SPAN>&nbsp;</DIV>
<BLOCKQUOTE dir=3Dltr style=3D"MARGIN-RIGHT: 0px">
  <DIV class=3DOutlookMessageHeader dir=3Dltr align=3Dleft><FONT =
face=3DTahoma=20
  size=3D2>-----Original Message-----<BR><B>From:</B> =
zope-admin@zope.org=20
  [mailto:zope-admin@zope.org]<B>On Behalf Of </B>Andrew =
Athan<BR><B>Sent:</B>=20
  Friday, April 06, 2001 3:50 PM<BR><B>To:</B> =
zope@zope.org<BR><B>Subject:</B>=20
  RE: [Zope] Verrryy strange behavior (NameError)<BR><BR></FONT></DIV>
  <DIV><FONT face=3DArial color=3D#0000ff size=3D2></FONT>&nbsp;</DIV>
  <DIV><SPAN class=3D987044119-06042001><FONT face=3DArial =
color=3D#0000ff size=3D2>Ok,=20
  folks, things just got really strange indeed.</FONT></SPAN></DIV>
  <DIV><SPAN class=3D987044119-06042001><FONT face=3DArial =
color=3D#0000ff=20
  size=3D2></FONT></SPAN>&nbsp;</DIV>
  <DIV><SPAN class=3D987044119-06042001><FONT face=3DArial =
color=3D#0000ff size=3D2>The=20
  "bad" NameError behavior seems highly dependent on a line THAT NEVER =
EXECUTES=20
  and which occurs much AFTER where I get the exception.&nbsp; Namely, =
later on=20
  in the validate() function is the following clause (yes, I used the =
eval bit=20
  so that the Python byte compiler, if it does invariant optimizations, =
doesn't=20
  remove the code in the if).</FONT></SPAN></DIV>
  <DIV><SPAN class=3D987044119-06042001><FONT face=3DArial =
color=3D#0000ff=20
  size=3D2></FONT></SPAN>&nbsp;</DIV>
  <DIV><SPAN class=3D987044119-06042001><FONT face=3DArial =
color=3D#0000ff size=3D2>Near=20
  line 11...</FONT></SPAN></DIV>
  <DIV><SPAN class=3D987044119-06042001><FONT face=3DArial =
color=3D#0000ff=20
  size=3D2>_xxxxxxxxxxxxx=3D'sss'</FONT></SPAN></DIV>
  <DIV><SPAN class=3D987044119-06042001><FONT face=3DArial =
color=3D#0000ff=20
  size=3D2></FONT></SPAN>&nbsp;</DIV>
  <DIV><SPAN class=3D987044119-06042001><FONT face=3DArial =
color=3D#0000ff size=3D2>Near=20
  line 236...</FONT></SPAN></DIV>
  <DIV><SPAN class=3D987044119-06042001><FONT face=3DArial =
color=3D#0000ff=20
  size=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print=20
  'testing=3D%s'%_xxxxxxxxxxxxx</FONT></SPAN></DIV>
  <DIV><SPAN class=3D987044119-06042001><FONT face=3DArial =
color=3D#0000ff=20
  size=3D2></FONT></SPAN>&nbsp;</DIV>
  <DIV><SPAN class=3D987044119-06042001><FONT face=3DArial =
color=3D#0000ff size=3D2>Near=20
  line 280...</FONT></SPAN></DIV>
  <DIV><SPAN class=3D987044119-06042001><FONT face=3DArial =
color=3D#0000ff=20
  size=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if =
hasattr(self,'loginForm')=20
  and \<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
  =
response.unauthorized.__name__=3D=3D'unauthorized':<BR>&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
  if=20
  =
eval('1=3D1')=3D=3D0:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
  print=20
  =
'arghhhhhhhhhhhhhhhhhhhhhhh'<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
  _xxxxxxxxxxxxx =3D 1<BR></FONT></SPAN></DIV>
  <DIV><SPAN class=3D987044119-06042001><FONT face=3DArial =
color=3D#0000ff size=3D2>As=20
  long as this code exists in the .py file, I get the name error.&nbsp;=20
  arghhhhhhhhhhhhhhhh never prints, ever (other print statements above =
it=20
  do).&nbsp; If I comment the _xxxxxxxxxxxx =3D 1 out, or change it to a =
different=20
  name, the NameError exception does not occur at line 236.&nbsp; If I =
leave it=20
  in, the exception occurs.</FONT></SPAN></DIV>
  <DIV><SPAN class=3D987044119-06042001><FONT face=3DArial =
color=3D#0000ff=20
  size=3D2></FONT></SPAN>&nbsp;</DIV>
  <DIV><SPAN class=3D987044119-06042001><FONT face=3DArial =
color=3D#0000ff size=3D2>Can=20
  you say "voodoo"?</FONT></SPAN></DIV>
  <DIV><SPAN class=3D987044119-06042001><FONT face=3DArial =
color=3D#0000ff=20
  size=3D2>A.</FONT></SPAN></DIV>
  <DIV><SPAN class=3D987044119-06042001><FONT face=3DArial =
color=3D#0000ff=20
  size=3D2>&nbsp;</DIV></FONT></SPAN>
  <DIV><SPAN class=3D987044119-06042001><FONT face=3DArial =
color=3D#0000ff=20
  size=3D2>&nbsp;</DIV></FONT></SPAN>
  <BLOCKQUOTE dir=3Dltr style=3D"MARGIN-RIGHT: 0px">
    <DIV class=3DOutlookMessageHeader dir=3Dltr align=3Dleft><FONT =
face=3DTahoma=20
    size=3D2>-----Original Message-----<BR><B>From:</B> =
zope-admin@zope.org=20
    [mailto:zope-admin@zope.org]<B>On Behalf Of </B>Andrew =
Athan<BR><B>Sent:</B>=20
    Friday, April 06, 2001 3:21 PM<BR><B>To:</B>=20
    zope@zope.org<BR><B>Subject:</B> [Zope] Verrryy strange behavior=20
    (NameError)<BR><BR></FONT></DIV>
    <DIV><FONT face=3DArial size=3D2><SPAN=20
    class=3D230302904-06042001></SPAN></FONT>&nbsp;</DIV>
    <DIV><FONT face=3DArial size=3D2><SPAN =
class=3D230302904-06042001>I'm getting an=20
    intermittent NameError trying to access a variable set in the =
external scope=20
    of LoginManager.py .&nbsp; I call my variable _x and set it directly =
above=20
    where _LogginInUser gets set.&nbsp; I then try to access it directly =
above a=20
    line that accesses _LoggingInUser gets accessed.&nbsp; I just do a=20
    simple&nbsp;print 'testing=3D%s'%_x.&nbsp; I get an exception.&nbsp; =
The line=20
    that accesses _LoggingInUser doesn't.</SPAN></FONT></DIV>
    <DIV><FONT face=3DArial size=3D2><SPAN=20
    class=3D230302904-06042001></SPAN></FONT>&nbsp;</DIV>
    <DIV><FONT face=3DArial size=3D2><SPAN =
class=3D230302904-06042001>What=20
    gives?</SPAN></FONT></DIV>
    <DIV><FONT face=3DArial size=3D2><SPAN=20
    class=3D230302904-06042001></SPAN></FONT>&nbsp;</DIV>
    <DIV><FONT face=3DArial size=3D2><SPAN=20
    class=3D230302904-06042001></SPAN></FONT>&nbsp;</DIV>
    <DIV><FONT face=3DArial size=3D2><SPAN =
class=3D230302904-06042001>Now get this ...=20
    if I change the name to (13x's)&nbsp;_xxxxxxxxxxxxx , it=20
    works.</SPAN></FONT></DIV>
    <DIV><FONT face=3DArial size=3D2><SPAN=20
    class=3D230302904-06042001></SPAN></FONT>&nbsp;</DIV>
    <DIV><FONT face=3DArial size=3D2><SPAN class=3D230302904-06042001>If =
I then change=20
    the name to (6x's) _xxxxxx, it continues to =
work.</SPAN></FONT></DIV>
    <DIV><FONT face=3DArial size=3D2><SPAN=20
    class=3D230302904-06042001></SPAN></FONT>&nbsp;</DIV>
    <DIV><FONT face=3DArial size=3D2><SPAN class=3D230302904-06042001>If =
I then change=20
    the name to (3x's) _xxx, it continues to work.</SPAN></FONT></DIV>
    <DIV><FONT face=3DArial size=3D2><SPAN =
class=3D230302904-06042001>(2x's) _xx=20
    works</SPAN></FONT></DIV>
    <DIV><FONT face=3DArial size=3D2><SPAN =
class=3D230302904-06042001>(1x's) _x=20
    doesn't work!!!!</SPAN></FONT></DIV>
    <DIV><FONT face=3DArial size=3D2><SPAN=20
    class=3D230302904-06042001></SPAN></FONT>&nbsp;</DIV>
    <DIV><FONT face=3DArial size=3D2><SPAN class=3D230302904-06042001>If =
I change the=20
    line near the top of the file from</SPAN></FONT></DIV>
    <DIV><FONT face=3DArial size=3D2><SPAN=20
    class=3D230302904-06042001>_xx=3D'sss'</SPAN></FONT></DIV>
    <DIV><FONT face=3DArial size=3D2><SPAN=20
    class=3D230302904-06042001>to</SPAN></FONT></DIV>
    <DIV><FONT face=3DArial size=3D2><SPAN=20
    class=3D230302904-06042001>_xx=3D0</SPAN></FONT></DIV>
    <DIV><FONT face=3DArial size=3D2><SPAN =
class=3D230302904-06042001>and change the=20
    print to use a %d&nbsp; </SPAN></FONT><FONT face=3DArial =
size=3D2><SPAN=20
    class=3D230302904-06042001>I get the NameError.</SPAN></FONT></DIV>
    <DIV><FONT face=3DArial size=3D2><SPAN=20
    class=3D230302904-06042001></SPAN></FONT>&nbsp;</DIV>
    <DIV><FONT face=3DArial size=3D2><SPAN class=3D230302904-06042001>If =
I change the=20
    line back to _xx=3D'sss' and the print back to a %s I continue to =
get the=20
    NameError ... even though that exact combination had worked a minute =

    =
before!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!</SPAN></FONT></DIV>
    <DIV><FONT face=3DArial size=3D2><SPAN=20
    class=3D230302904-06042001></SPAN></FONT>&nbsp;</DIV>
    <DIV><FONT face=3DArial size=3D2><SPAN class=3D230302904-06042001>If =
I then change=20
    the variable back to have 13x's it starts working again.&nbsp; Is =
there some=20
    deep problem with the python byte compiler or access control =
mangling=20
    features of Zope?&nbsp; Or am I missing something=20
stupid?</SPAN></FONT></DIV>
    <DIV><FONT face=3DArial size=3D2><SPAN=20
    class=3D230302904-06042001></SPAN></FONT>&nbsp;</DIV>
    <DIV><FONT face=3DArial size=3D2><SPAN =
class=3D230302904-06042001>What in the=20
    world is going on?</SPAN></FONT></DIV>
    <DIV><FONT face=3DArial size=3D2><SPAN=20
    class=3D230302904-06042001>A.</SPAN></FONT></DIV>
    <DIV><FONT face=3DArial size=3D2><SPAN=20
    class=3D230302904-06042001></SPAN></FONT>&nbsp;</DIV>
    <DIV><FONT face=3DArial><SPAN class=3D230302904-06042001>Traceback =
(innermost=20
    last):<BR>&nbsp; File D:\CloakMail\lib\python\ZPublisher\Publish.py, =
line=20
    238, in publish_module<BR>&nbsp; File=20
    D:\CloakMail\lib\python\ZPublisher\Publish.py, line 199, in=20
    publish<BR>&nbsp; File D:\CLOAKM~1\lib\python\Zope\__init__.py, line =
221, in=20
    zpublisher_exception_hook<BR>&nbsp;&nbsp;&nbsp; (Object:=20
    Traversable)<BR>&nbsp; File =
D:\CloakMail\lib\python\ZPublisher\Publish.py,=20
    line 165, in publish<BR>&nbsp; File=20
    D:\CloakMail\lib\python\ZPublisher\BaseRequest.py, line 450, in=20
    traverse<BR>&nbsp; File=20
    D:\CloakMail\lib\python\Products\LoginManager\LoginManager.py, line =
236, in=20
    validate<BR>&nbsp;&nbsp;&nbsp; (Object: =
ProviderContainer)<BR>NameError:=20
    (see above)<BR></SPAN></FONT></DIV>
    <DIV><FONT face=3DArial size=3D2><SPAN=20
    class=3D230302904-06042001></SPAN></FONT>&nbsp;</DIV>
    <DIV><FONT face=3DArial size=3D2><SPAN=20
    class=3D230302904-06042001></SPAN></FONT>&nbsp;</DIV>
    <DIV><FONT face=3DArial size=3D2><SPAN=20
    =
class=3D230302904-06042001></SPAN></FONT>&nbsp;</DIV></BLOCKQUOTE></BLOCK=
QUOTE></BODY></HTML>

------=_NextPart_000_00FD_01C0BEB2.C6C2B880--