开发者

Google Appengine and Python exceptions

开发者 https://www.devze.com 2022-12-18 12:05 出处:网络
In my Google Appengine application I have defined a custom exception InvalidUrlException(Exception) in the module \'gvu\'. Somewhere in my code I do:

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?


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:.


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).


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消