except clause appears to be being skipped?

AWasilenko at gmail.com AWasilenko at gmail.com
Sat Mar 24 02:51:01 CET 2007


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.




More information about the Python-list mailing list