[Zope] Trapping zope exceptions in python script

Andrew Langmead alangmead at boston.com
Thu Dec 15 16:38:59 EST 2005


On Dec 15, 2005, at 4:01 PM, Nikko Wolf wrote:

> Can you elaborate on what "strange and unpredictable behavior" you  
> mean?  I'm curious in general, but especially w.r.t. the code above?
>
> BTW, a simple grep of sources in Zope (2.7.6-final) turns up 600+  
> places where a bare except is used, and my Zope instance (which  
> uses Plone) contains over 350 more.

Often when you see a bare except, it is after some of the specific  
important exceptions are caught and re-raised:

try:
   something()
except ConflictError:
     raise
except:
     recover()


Even this is  pretty poor style anyway (If you don't know enough  
about what is going wrong to know what is being thrown, you probably  
shouldn't be in charge of recovering from it.)

The problem with catching and swallowing ConflictError and similar  
internal Zope errors is that  you leave your ZODB in an inconsistent  
state. Roughly, one request has started making modifications to the  
ZODB, then a second request has been making modifications to the same  
object. The ZODB has noticed this, and throws this  ConflictError  
exception. It is expecting this exception to bubble up all the way  
out of your code, throwing away whatever potential modifications you  
have made to the ZODB and having Zope try your code again. (at that  
point, hopefully the task that didn't get the conflict error has  
completed and committed its changes. When your code is run again, it  
will see the new values from the other transaction.

Here an (again very rough) example:

Imagine that you have two collections of objects called "unprocessed"  
and "processed". A group of somethings or someones grab the next item  
in "unprocessed", and puts it into "processed". If two requests come  
in to grab the next item from "unprocessed" and put it one of the  
other buckets, at some point. one or both of them will get the  
ConflictError exception. If the exception is swallowed by the code  
both of them will save their copy of the object in "processed", and  
where you had one object before you now have two. If these objects  
were student quizzes, two copies of the same quiz will double the  
weight of the score. If these object were loan application, the  
amount of money you were paying out has doubled. All around bad news.




More information about the Zope mailing list