[Tutor] Server Error

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Sun Aug 22 09:07:15 CEST 2004



On Sat, 21 Aug 2004, Danny Yoo wrote:

> ###
> >>> message = "he said: " +
>   File "<stdin>", line 1
>     message = "he said: " +
>                           ^
> SyntaxError: invalid syntax
> ###


As one more note: it's usually pretty easy to detect SyntaxErrors: just
run it locally through Python, even before posting it to your web server.
If there's any SyntaxErrors, stop and fix them before going on, because
they're showstoppers.


Semantic errors are a little tougher, and that's when tracebacks will be
useful.  But there's also a nice tool for checking programs called
'pychecker':

    http://pychecker.sourceforge.net/


Here's what it says when I run it through your program (with the
SyntaxErrors from our previous email all fixed up):

###
volado:~ dyoo$ pychecker basic4.py
Processing basic4...

Warnings...

basic4.py:36: Raising string exceptions are deprecated
(BadTemplateException)
###



'pychecker' is reporting the use of string exceptions here:

###
    # This defines an exception string in case
    # our template file is messed up.
    BadTemplateException = "There was a problem with the HTML template."

    SubResult = re.subn("<!--***INSERT CONTENT HERE***--",
                        Content, TemplateInput)
    if SubResult[1] == 0:
        raise BadTemplateException
###


Raising strings as exceptions isn't really kosher; ancient versions of
Python supported this, which is probably the only reason they're still
supported.  But use a class definition instead, like this:

###
class BadTemplateException(Exception):
    def __str__(self):
        return "There was a problem with the HTML template."
###


See:

    http://www.python.org/doc/tut/node10.html#SECTION0010500000000000000000

for more information on User Defined Exceptions.


Good luck!



More information about the Tutor mailing list