Jim
Jim

Reputation: 530

Google Appengine and Python exceptions

In my Google Appengine application I have defined a custom exception InvalidUrlException(Exception) in the module 'gvu'. Somewhere in my code I do:

try:
    results = gvu.article_parser.parse(source_url)
except gvu.InvalidUrlException as e:
    self.redirect('/home?message='+str(e))
...

which works fine in the local GAE development server, but raises

<type 'exceptions.SyntaxError'>: invalid syntax (translator.py, line 18)

when I upload it. (line 18 is the line starting with 'except')

The problem seems to come from the 'as e' part: if I remove it I don't get this exception anymore. However I would like to be able to access the raised exception. Have you ever encountered this issue? Is there an alternative syntax?

Upvotes: 1

Views: 970

Answers (3)

Kevin Wheeler
Kevin Wheeler

Reputation: 1452

I was getting the same error because I was using the pydoc command instead of the pydoc3 command on a python3 file that was using python3 print statements (print statements with parenthesis).

Upvotes: 2

Chirael
Chirael

Reputation: 3085

Just FYI, another possible cause for this error - especially if the line referenced is early in the script (like line 2) is line ending differences between Unix and Windows.

I was running Python on Windows from a Cygwin shell and got this error, and was really puzzled. I had created the file with "touch" before editing it.

I renamed the file to a temp file name, and copied another file (which I had downloaded from a Unix server) onto the original file name, then restored the contents via the temp file, and problem solved. Same exact file contents (on the screen anyway), only difference was where the file had originally been created.

Just wanted to post this in case anyone else ran across this error and was similarly puzzled.

Upvotes: 0

Max Shawabkeh
Max Shawabkeh

Reputation: 38603

You probably have an older Python version on your server. except ExceptionType as varname: is a newer syntax. Previously you needed to simply use a comma: except ExceptionType, varname:.

Upvotes: 5

Related Questions