[Zope] NameError (global name 'namespace' is not defined) - more diagnos tics and code

Chris McDonough chrism@zope.com
Fri, 7 Dec 2001 16:02:41 -0500


The PythonScript in question likely does not have its "namespace"
helper variable bound to the name "namespace".  You can look on the
"bindings" tab of the script to see if this is true.

----- Original Message -----
From: <Ronald.Chichester@bakerbotts.com>
To: <zope@zope.org>
Sent: Friday, December 07, 2001 3:35 PM
Subject: [Zope] NameError (global name 'namespace' is not defined) -
more diagnos tics and code


> Even more diagnostic information the pecurliar 'namespace' error.
>
> I loaded Zope 2-4-3 onto my box and loaded in the product and folder
(with
> info) in their appropriate places.  Then I loaded the python scripts
into
> the editor and pressed the "Save Changes" button do recompile.  It
didn't
> help.  Same problem.  NameError: global name 'namespace' is not
defined.
>
> I'm wondering if my technique for getting information from an object
isn't
> frought with peril.  Is there a technique where I could return a
particular
> object from a list of objects?  Getting an actual object is easy to
do.
> However, the problem comes when the search is unsuccessful.  What do
you
> return?  The below code is what I developed to remedy that problem.
The
> problem with the remedy is that it doesn't work in Zope 2-4-3
(although it
> did in 2-3-1).  Note, the DTML code from which the python script is
called
> used the contents namespace.  Here is the original python script
code:
>
> found    = 0   # flag to indicate that the object was/wasn't
found...
> contents = []  # the return list...
>
> # Run through the objects in the current (context) folder to see if
> there is a match (with given_title)...
> for obj in context.objectValues('Image'):
>   if found == 0:
>     if given_title == obj.title:
>       url            = obj.absolute_url
>       title          = obj.title
>       contents.append(namespace(url=url, title=title)[0])
>       found = 1                  # Set flag on success...
>
> # If you can't find it in the context, then check the container...
> for obj in container.objectValues('Image'):
>   if found == 0:
>     if given_title == obj.title:
>       url            = obj.absolute_url
>       title          = obj.title
>       contents.append(namespace(url=url, title=title)[0])
>       found = 1                  # Set flag on success...
>
> # Return indicative values if the desired object was not found...
> if found == 0:
>   contents.append(namespace(url='', title='')[0])
>
> vars = namespace(contents=contents)
>
> return vars
>
> -----
>
> As the namespace issue wss causing the problem, I tried to get
around it by
> doing the following:
>
> found    = 0   # flag to indicate that the object was/wasn't
found...
>
> # Run through the objects in the current (context) folder to see if
> there is a match (with given_title)...
> for obj in context.objectValues('Image'):
>   if found == 0:
>     if given_title == obj.title:
>       ret_obj = obj
>       found = 1                  # Set flag on success...
>
> # If you can't find it in the context, then check the container...
> for obj in container.objectValues('Image'):
>   if found == 0:
>     if given_title == obj.title:
>       ret_obj = obj
>       found = 1                  # Set flag on success...
>
> return ret_obj
>
> ###
>
> ... which works just fine _IF_ you find the object.  If not, the
DTML code
> is still going to look for a property and not find one (because
nothing is
> returned).  To fix that, I added a "dummy" object for errors, to
wit:
>
> class myErrorClass:
>   element_number = -1
>
> ... was tacked on at the beginning of the script, and ...
>
> if found == 0:
>   an_Error = myErrorClass()
>   return an_Error
> else:
>   return ret_obj
>
> ... was tacked on at the end.
>
> Unfortunately, when you run into a condition where no legitimate
object was
> found (i.e., found == 0), you get a screwy response from Zope.  The
> username/password challenge comes up (even if you are in the manage
session
> as the manager).  No username/password combination exists to get
through
> that.  In short, something about instantiating the class screwed up
Zope.
>
> What does one do in such a situation?
>
> Thank you all in advance.
>
> Ron
>
> ---
>
> complaw@hal-pc.org wrote:
> >As I said before, the above code works just fine in Zope 2-3-1.
> >However, it fails (gets the NameError) in both versions 2-4-2 and
> >2-4-3.  Is this because Zope went to python 2.x instead of 1.5?
> >
> >Do I have to rewrite my code?  Do I have to tell my client to get
2-3-1?
> >
> >Any hints would be greatly appreciated.
> >
> >Thanks,
> >
> >Ron
> >
>
>
> >
> > This may be a version problem, or it may not.
> >
> > I wrote a fairly straightforward Zope application in Zope 2-3-1 on
Linux.
> (That
> > instance of Zope has been working just fine for months and hey, it
wasn't
> broke
> > so I didn't fix it.  The Linux box has been up for 132 days and I
like the
> fact
> > that I only have to fuss with Zope, if anything at all.)
> >
> > I created a simple product and associated folder (with a slew of
Folders,
> DTML
> > Documents, python scripts, and DTML Methods.  Then I exported them
and
> shipped
> > both the product and the site off to a client who had downloaded
and
> installed
> > Zope 2-3-4 for Windows.
> >
> > The client got into the Zope management screen (after a little
> hand-holding, but
> > he didn't read any documentation, so I can't complain).  I talked
him
> through
> > the processes of unzip/untar the product, and loading the
particular site.
> >
> > Now the client can pull up a browser and accurately read
index_html of the
> main
> > folder.  However, when he tries to read any of the DTML Documents,
> Folders, etc.
> > he gets:
> >
> > Zope Error
> >
> > Error Type: NameError
> > Error Value: global name 'namespace' is not defined
> >
> > Is this problem between different versions of Zope?  namespace is
pretty
> > fundamental.  I don't see how it could have worked fine under
2-3-1 and
> not
> > under 2-4-3.
> >
> > Thanks in advance for any help.
> >
> > Ron
> >
> > _______________________________________________
> > Zope maillist  -  Zope@zope.org
> > http://lists.zope.org/mailman/listinfo/zope
> > **   No cross posts or HTML encoding!  **
> > (Related lists -
> >  http://lists.zope.org/mailman/listinfo/zope-announce
> >  http://lists.zope.org/mailman/listinfo/zope-dev )
>
> _______________________________________________
> Zope maillist  -  Zope@zope.org
> http://lists.zope.org/mailman/listinfo/zope
> **   No cross posts or HTML encoding!  **
> (Related lists -
>  http://lists.zope.org/mailman/listinfo/zope-announce
>  http://lists.zope.org/mailman/listinfo/zope-dev )
>
>
> _______________________________________________
> Zope maillist  -  Zope@zope.org
> http://lists.zope.org/mailman/listinfo/zope
> **   No cross posts or HTML encoding!  **
> (Related lists -
>  http://lists.zope.org/mailman/listinfo/zope-announce
>  http://lists.zope.org/mailman/listinfo/zope-dev )
>