Python: catch a special exception

I have this code (Python 2.5, GAE dev server):

try:
    yt_service.UpgradeToSessionToken() // this line produces TokenUpgradeFailed
except gdata.service.TokenUpgradeFailed:
    return HttpResponseRedirect(auth_sub_url()) # this line will never be executed (why?)
except Exception, exc:
    return HttpResponseRedirect(auth_sub_url()) # instead this line is executed (why?)

So, I set a breakpoint on the last line and under the debugger, I see:

"exc"   TokenUpgradeFailed: {'status': 403, 'body': 'html stripped', 'reason': 'Non 200 response on upgrade'}   
"type(exc)" type: <class 'gdata.service.TokenUpgradeFailed'>
"exc is gdata.service.TokenUpgradeFailed"   bool: False 
"exc.__class__" type: <class 'gdata.service.TokenUpgradeFailed'>
"isinstance(exc, gdata.service.TokenUpgradeFailed)" bool: False 
"exc.__class__.__name__"    str: TokenUpgradeFailed 

What did I miss in python exception handling? Why is isststance (exc, gdata.service.TokenUpgradeFailed) is False?

+3
source share
1 answer

This error may occur if your relative / absolute statements importdo not match everywhere. If there is a mismatch, the target module can be loaded more than once and in several different contexts. This is usually not a problem, but it prevents comparing classes from different loadable modules as equal (hence the catch-catch problem).

, , gdata.service gdata. gdata , service, from gdata import service, : import service.

+2

Source: https://habr.com/ru/post/1765626/


All Articles