except clause appears to be being skipped?

A

AWasilenko

I can't figure out this problem Im having, I just can't understand why
it is ignoring the call I put in. First the code (This is a cherrypy
website):

import sys, cherrypy, html

class Root:
@cherrypy.expose
def index(self, pageid = "Index"):
selection = html.Page()
return selection.makeit(dex = pageid)


cherrypy.config.update({'server.socket_port': 2572, 'log.screen':
False})
cherrypy.quickstart(Root())


If you're not familiar with cherrypy, whats going on here is its
pulling a variable from the url and just passing that to my page class
that I created. The url would look like http://nonyaz.com/index?pageid=foo
and pageid would thus be foo.

and here is the class code stored in the html.py file that's causing
me all this grief:

class Page:
#Generic webpage assembler
def __init__(self):
#Open the header txt file for later use
self.headertxt = open("pages/header.html","r")
#self.footertxt = open("pages/footer.html","r")


def makeit(self,dex=""):
pagetitle, htmlbody = self.pager(dex)
return self.headerinsert(pagetitle) + htmlbody


def pager(self,dex):
#Input page filename, Output pagetitle and the HTML output
#Find out if the file requested actually exists
try:
j = dex + ".html"
textfile = open("pages/" + j, "r")
#If not 404' it
except:
self.err404(dex)

#The first line in the .html files is the title, this reads that one
line
pagetitle = textfile.readline()

#Put the remaining lines of HTML into a var
htmlbody = textfile.read()

#Return the results
return pagetitle,htmlbody

def headerinsert(self,pagetitle):
#Processes the header.html file for use. Input a page title,
outputs
#the compleated header with pagetitle inserted
headerp1 = ""
for i in range(5):
headerp1 += self.headertxt.readline()
headerp2 = self.headertxt.readline(7)
headerp3 = self.headertxt.readline()
headerp4 = self.headertxt.read()
return headerp1 + headerp2 + str.strip(pagetitle) + headerp3 +
headerp4


def err404(self,whatitis="N/A"):
#Page not found error page
return """<body bgcolor="#666666">
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<center>Sorry the page <em>""" + str(whatitis) + """</em> does not
exist.<br />
<img src="/files/images/404.png" alt="Page cannot be found."></center>
</body>"""

This code does work when there is a valid page, aka when the try
statement executes with out exception. The the exception is raised
for when there is no file, that's where the problems come in, I want
it just to call the err404 function and return my 404 page, but it
seems to act like its not there and keeps on going, giving me this
error:

Traceback (most recent call last):
File "/home2/awasilenko/lib/python2.4/cherrypy/_cprequest.py", line
342, in respond
cherrypy.response.body = self.handler()
File "/home2/awasilenko/lib/python2.4/cherrypy/_cpdispatch.py", line
15, in __call__
return self.callable(*self.args, **self.kwargs)
File "/home2/awasilenko/webapps/cp/site.py", line 7, in index
return selection.makeit(dex = pageid)
File "/home2/awasilenko/webapps/cp/html.py", line 10, in makeit
pagetitle, htmlbody = self.pager(dex)
File "/home2/awasilenko/webapps/cp/html.py", line 25, in pager
pagetitle = textfile.readline()
UnboundLocalError: local variable 'textfile' referenced before
assignment

I know the except is being executed because I put a break in there for
testing purposes and it did break, but as for why its not pulling up
the 404 function and returning the error page, I have no idea.
 
J

John Machin

On Mar 24, 12:51 pm, (e-mail address removed) wrote:
[snip]
def pager(self,dex):
#Input page filename, Output pagetitle and the HTML output
#Find out if the file requested actually exists
try:
j = dex + ".html"
textfile = open("pages/" + j, "r")
#If not 404' it
except:
self.err404(dex)

#The first line in the .html files is the title, this reads that one
line
pagetitle = textfile.readline()
[snip]

File "/home2/awasilenko/webapps/cp/html.py", line 25, in pager
pagetitle = textfile.readline()
UnboundLocalError: local variable 'textfile' referenced before
assignment

I know the except is being executed because I put a break in there for
testing purposes and it did break, but as for why its not pulling up
the 404 function and returning the error page, I have no idea.

It *is* "pulling up the 404 function", which *is* returning your error
page. However all your except clause does is "self.err404(dex)" -- you
ignore the return value, and fall out of the except clause with
textfile undefined, with the expected consequences.

I'm not at all familiar with cherrypy, but you probably need to do
either:
errpage = self.err404(dex)
dosomethingwith(errpage, dex)
return
or simply:
return "404 page title", self.err404(dex)
[Actually it would be better style if the err404 method returned a
tuple of (pagetitle, pagebody), then your except clause contains
only:
return self.err404(dex)

The main point being to return instead of falling through the bottom
of the except clause. BTW, you should not use a bare except. Be a
little more specific, like except IOError:

Looking at the cherrypy docs would seem indicated. AFAIK there was a
v1 and a v2 with different naming conventions and there's now a v3 --
do ensure that you mention which version you are using if you need to
come back with more questions.

HTH,
John
 
A

AWasilenko

It *is* "pulling up the 404 function", which *is* returning your error
page. However all your except clause does is "self.err404(dex)" -- you
ignore the return value, and fall out of the except clause with
textfile undefined, with the expected consequences.

Humm I dident think had to put return when I called the 404 becuase
the def has a return on the end of that, but now that I think about it
more it makes sence.


I'm not at all familiar with cherrypy, but you probably need to do
either:
errpage = self.err404(dex)
dosomethingwith(errpage, dex)
return
or simply:
return "404 page title", self.err404(dex)
[Actually it would be better style if the err404 method returned a
tuple of (pagetitle, pagebody), then your except clause contains
only:
return self.err404(dex)

Both of you're suggestions worked, Change it to return the tuple.



The main point being to return instead of falling through the bottom
of the except clause. BTW, you should not use a bare except. Be a
little more specific, like except IOError:

Suggestion implemented, I figured you could be more specific with
exceptions but I must have glazed over that in my book when I read
about it.



Looking at the cherrypy docs would seem indicated.
Since I'm a noobie python'r (or programmer for that matter) most if
not all of my mistkaes are with python and the syntax, there is not
much to screw up with Cherrypy itself, but I'm sure I will find a way
to screw it up later :)
AFAIK there was a
v1 and a v2 with different naming conventions and there's now a v3 --
do ensure that you mention which version you are using if you need to
come back with more questions.

HTH,
John

Naming conventions eh? I guess I'm following conventions so far, I
havent ran into that problem yet (Knock Knock)

Thanks for you're help John, people like you make these newsgroups are
an invaluable resource :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top