except clause appears to be being skipped?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • AWasilenko@gmail.com

    #1

    except clause appears to be being skipped?

    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.expos e
    def index(self, pageid = "Index"):
    selection = html.Page()
    return selection.makei t(dex = pageid)


    cherrypy.config .update({'serve r.socket_port': 2572, 'log.screen':
    False})
    cherrypy.quicks tart(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.headerinse rt(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.readli ne()

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

    #Return the results
    return pagetitle,htmlb ody

    def headerinsert(se lf,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(paget itle) + headerp3 +
    headerp4


    def err404(self,wha titis="N/A"):
    #Page not found error page
    return """<body bgcolor="#66666 6">
    <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
    <center>Sorry the page <em>""" + str(whatitis) + """</emdoes 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.respon se.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.makei t(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.readli ne()
    UnboundLocalErr or: 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.

  • John Machin

    #2
    Re: except clause appears to be being skipped?

    On Mar 24, 12:51 pm, AWasile...@gmai l.com 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.readli ne()
    >
    [snip]
    File "/home2/awasilenko/webapps/cp/html.py", line 25, in pager
    pagetitle = textfile.readli ne()
    UnboundLocalErr or: 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(de x)" -- 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

    Comment

    • AWasilenko@gmail.com

      #3
      Re: except clause appears to be being skipped?

      On Mar 23, 10:29 pm, "John Machin" <sjmac...@lexic on.netwrote:
      It *is* "pulling up the 404 function", which *is* returning your error
      page. However all your except clause does is "self.err404(de x)" -- 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 :)

      Comment

      Working...