Re: [Zope] Trapping zope exceptions in python script

2005-12-15 Thread Andreas Jung



--On 15. Dezember 2005 08:03:22 -0500 Jonathan [EMAIL PROTECTED] wrote:



- Original Message - From: J Cameron Cooper



It seems strange that one can, using plain vanilla python scripts,  trap
bare 'try/excepts' (and I agree with Andreas that this is not a good
thing to do!) and that one can trap python built-in exceptions, but that
one cannot trap zope exceptions.  Why allow python scripts to trap zope
exceptions using a bare try/except (and then have to re-raise everything
except the target zope exception), but not allow python scripts to target
specific zope exceptions?  I don't see the reasoning behind this approach.

My 2 cents: PythonScripts are restricted and are *not* thought to be a full 
replacement for Python modules. If you need this functionaltiy consider 
writing a Zope Product, using external methods or using TrustedExecutables.


-aj

pgpQyEjsXil5w.pgp
Description: PGP signature
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] Trapping zope exceptions in python script

2005-12-15 Thread Tino Wildenhain

Jonathan schrieb:



...
It seems strange that one can, using plain vanilla python scripts,  trap 
bare 'try/excepts' (and I agree with Andreas that this is not a good 
thing to do!) and that one can trap python built-in exceptions, but that 
one cannot trap zope exceptions.  Why allow python scripts to trap zope 
exceptions using a bare try/except (and then have to re-raise everything 
except the target zope exception), but not allow python scripts to 
target specific zope exceptions?  I don't see the reasoning behind this 
approach.


Well you can trap any exception which happens between your
try and except. But not outside of these, of course.
This is true everywhere (not even python only ;)

Which exceptions do you believe you might be not able
to catch?

HTH
Tino
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
http://mail.zope.org/mailman/listinfo/zope-announce

http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] Trapping zope exceptions in python script

2005-12-15 Thread Jonathan


- Original Message - 
From: Tino Wildenhain [EMAIL PROTECTED]

To: Jonathan [EMAIL PROTECTED]
Cc: zope@zope.org
Sent: Thursday, December 15, 2005 8:18 AM
Subject: Re: [Zope] Trapping zope exceptions in python script



Jonathan schrieb:



...
It seems strange that one can, using plain vanilla python scripts,  trap 
bare 'try/excepts' (and I agree with Andreas that this is not a good 
thing to do!) and that one can trap python built-in exceptions, but that 
one cannot trap zope exceptions.  Why allow python scripts to trap zope 
exceptions using a bare try/except (and then have to re-raise everything 
except the target zope exception), but not allow python scripts to target 
specific zope exceptions?  I don't see the reasoning behind this 
approach.


Well you can trap any exception which happens between your
try and except. But not outside of these, of course.
This is true everywhere (not even python only ;)

Which exceptions do you believe you might be not able
to catch?


I encountered this situation while trying to trap 'BadRequest'.

Jonathan 



___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
http://mail.zope.org/mailman/listinfo/zope-announce

http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] Trapping zope exceptions in python script

2005-12-15 Thread Jonathan

Andreas wrote:



My 2 cents: PythonScripts are restricted and are *not* thought to be a full
replacement for Python modules. If you need this functionaltiy consider
writing a Zope Product, using external methods or using TrustedExecutables.




If python scripts are restricted from accessing zExceptions (for security 
reasons???) then why allow python scripts to trap zExceptions in bare 
try/excepts?  If the logic for not allowing zExceptions in plain vanilla 
python scripts is for security reasons, then allowing bare try/excepts would 
seem to be a security hole (though, I don't see the rationale for this).


My 2 cents.

Jonathan


___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
http://mail.zope.org/mailman/listinfo/zope-announce

http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] Trapping zope exceptions in python script

2005-12-15 Thread Tino Wildenhain

...


Try:

context.afolder.manage_delObjects(['someitem'])

where 'someitem' does not exist in 'afolder'


works:

try:
   context.afolder.manage_adlObjects(['nonexistent'])
except Exception,x:
   if 'BadRequest' in repr(x):
  print bad request
   else:
  raise x

:-)

(Ok, this _is_ ugly, but at least works. Certainly these
exception types should be importable or even immediately
available for python scripts)



___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
http://mail.zope.org/mailman/listinfo/zope-announce

http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] Trapping zope exceptions in python script

2005-12-15 Thread Jonathan
- Original Message - 
From: Tino Wildenhain [EMAIL PROTECTED]

To: Jonathan [EMAIL PROTECTED]; zope@zope.org
Sent: Thursday, December 15, 2005 8:48 AM
Subject: Re: [Zope] Trapping zope exceptions in python script



...


Try:

context.afolder.manage_delObjects(['someitem'])

where 'someitem' does not exist in 'afolder'


works:

try:
   context.afolder.manage_adlObjects(['nonexistent'])
except Exception,x:
   if 'BadRequest' in repr(x):
  print bad request
   else:
  raise x

:-)

(Ok, this _is_ ugly, but at least works. Certainly these
exception types should be importable or even immediately
available for python scripts)



Interesting work-around, but I agree that these exception types should be 
immediately available in plain vanilla python scripts.


Jonathan 



___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
http://mail.zope.org/mailman/listinfo/zope-announce

http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] Trapping zope exceptions in python script

2005-12-15 Thread Andreas Jung



--On 15. Dezember 2005 09:08:20 -0500 Jonathan [EMAIL PROTECTED] wrote:


- Original Message - From: Tino Wildenhain [EMAIL PROTECTED]
Interesting work-around, but I agree that these exception types should be
immediately available in plain vanilla python scripts.




Patches + tests are welcome :-)

-aj

pgp4Ivu8J9Dnz.pgp
Description: PGP signature
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] Trapping zope exceptions in python script

2005-12-15 Thread Tino Wildenhain

Andreas Jung schrieb:



--On 15. Dezember 2005 09:08:20 -0500 Jonathan [EMAIL PROTECTED] wrote:


- Original Message - From: Tino Wildenhain [EMAIL PROTECTED]
Interesting work-around, but I agree that these exception types should be
immediately available in plain vanilla python scripts.




Patches + tests are welcome :-)



Yes, Jonathan: please file a bug report - best with a list
of Exceptions to be made available :-)

++Tino
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
http://mail.zope.org/mailman/listinfo/zope-announce

http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] Trapping zope exceptions in python script

2005-12-15 Thread J Cameron Cooper

Jonathan wrote:

Andreas wrote:




My 2 cents: PythonScripts are restricted and are *not* thought to be a full
replacement for Python modules. If you need this functionaltiy consider
writing a Zope Product, using external methods or using TrustedExecutables.





If python scripts are restricted from accessing zExceptions (for 
security reasons???) then why allow python scripts to trap zExceptions 
in bare try/excepts?  If the logic for not allowing zExceptions in plain 
vanilla python scripts is for security reasons, then allowing bare 
try/excepts would seem to be a security hole (though, I don't see the 
rationale for this).


I would imagine that not allowing these exceptions to be imported in 
trusted code is simply an oversight. The mechanism involved is not a 
you may not import this type of thing, but rather a you may import 
this statement. It is easy to miss safe but rarely used pieces.


If you have a list of exceptions you would like to have available, go 
and file a bug report. A patch would be even better.


--jcc
--
Building Websites with Plone
http://plonebook.packtpub.com/

Enfold Systems, LLC
http://www.enfoldsystems.com
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
http://mail.zope.org/mailman/listinfo/zope-announce

http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] Trapping zope exceptions in python script

2005-12-15 Thread Nikko Wolf

Andreas Jung wrote:




--On 14. Dezember 2005 12:54:56 -0700 Nikko Wolf 
[EMAIL PROTECTED] wrote:



So you could try this instead:

for item in itemList:
   try:
  context.afolder.manage_delObjects([item])
   except:
  continue



Wa... DON'T DO THAT. You should *never* use bare try..excepts 
within Zope applications. This can lead to strange and unpredictable 
behavior.


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.


Cheers,
Nikko


___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
http://mail.zope.org/mailman/listinfo/zope-announce

http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] Trapping zope exceptions in python script

2005-12-15 Thread Andrew Langmead


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.



___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
http://mail.zope.org/mailman/listinfo/zope-announce

http://mail.zope.org/mailman/listinfo/zope-dev )


[Zope] Trapping zope exceptions in python script

2005-12-14 Thread Jonathan
I am trying to catch a 'BadRequest' exception in the following python script 
excerpt:


for item in itemList:
  try:
 context.afolder.manage_delObjects([item])
  except BadRequest:
 continue


The rationale behind this is that itemList may contain entries that no 
longer exist in 'afolder', but I would like to delete whatever does exist. 
'BadRequest' is not a python built-in exception, so whenever the above 
except clause is invoked it actually causes another error:


Traceback (innermost last):
 Module ZPublisher.Publish, line 98, in publish
 Module ZPublisher.mapply, line 88, in mapply
 Module ZPublisher.Publish, line 39, in call_object
 Module OFS.DTMLMethod, line 126, in __call__
 Module DocumentTemplate.DT_String, line 474, in __call__
 Module DocumentTemplate.DT_In, line 705, in renderwob
 Module DocumentTemplate.DT_Util, line 201, in eval
  - __traceback_info__: _
 Module string, line 2, in f
 Module Shared.DC.Scripts.Bindings, line 252, in __call__
 Module Shared.DC.Scripts.Bindings, line 283, in _bindAndExec
 Module Products.PythonScripts.PythonScript, line 315, in _exec
 Module Script (Python), line 25, in subDeleteAd
  - PythonScript at /fas/subDeleteAd
  - Line 25
NameError: global name 'BadRequest' is not defined


I can work around this problem by checking to see if the item exists in 
'afolder' before I try to delete it, but I was curious as to how you go 
about trapping a 'BadRequest' error in a python script?


Thanks,

Jonathan 



___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
http://mail.zope.org/mailman/listinfo/zope-announce

http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] Trapping zope exceptions in python script

2005-12-14 Thread Nikko Wolf

Jonathan wrote:
I am trying to catch a 'BadRequest' exception in the following python 
script excerpt:


for item in itemList:
  try:
 context.afolder.manage_delObjects([item])
  except BadRequest:
 continue




NameError: global name 'BadRequest' is not defined


The following would normally be required, but it fails with an ImportError:

   from zExceptions import BadRequest

So you could try this instead:

   for item in itemList:
  try:
 context.afolder.manage_delObjects([item])
  except:
 continue

In general, TTW coding is very limited; you can use External Methods 
(a.k.a. Extensions) or Products to avoid this.


___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
http://mail.zope.org/mailman/listinfo/zope-announce

http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] Trapping zope exceptions in python script

2005-12-14 Thread Andreas Jung



--On 14. Dezember 2005 12:54:56 -0700 Nikko Wolf [EMAIL PROTECTED] 
wrote:

So you could try this instead:

for item in itemList:
   try:
  context.afolder.manage_delObjects([item])
   except:
  continue


Wa... DON'T DO THAT. You should *never* use bare try..excepts within 
Zope applications. This can lead to strange and unpredictable behavior.


-aj

pgpfQQjIMbwun.pgp
Description: PGP signature
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] Trapping zope exceptions in python script

2005-12-14 Thread Jonathan
Thanks to everyone for the feedback... the bottom line seems to be that you 
can NOT trap zope exceptions in a python script... which seems a bit odd.



Jonathan


- Original Message - 
From: Andreas Jung [EMAIL PROTECTED]

To: Nikko Wolf [EMAIL PROTECTED]; zope@zope.org
Sent: Wednesday, December 14, 2005 3:06 PM
Subject: Re: [Zope] Trapping zope exceptions in python script



___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists -
http://mail.zope.org/mailman/listinfo/zope-announce
http://mail.zope.org/mailman/listinfo/zope-dev )




___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
http://mail.zope.org/mailman/listinfo/zope-announce

http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] Trapping zope exceptions in python script

2005-12-14 Thread Tino Wildenhain
Am Mittwoch, den 14.12.2005, 16:20 -0500 schrieb Jonathan:
 Thanks to everyone for the feedback... the bottom line seems to be that you 
 can NOT trap zope exceptions in a python script... which seems a bit odd.
 
No, you can. But you should know what you do and 
reraise critical (e.g. Conflict exeptions and
the like).

___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] Trapping zope exceptions in python script

2005-12-14 Thread J Cameron Cooper

Jonathan wrote:
Thanks to everyone for the feedback... the bottom line seems to be that 
you can NOT trap zope exceptions in a python script... which seems a bit 
odd.


Of course you can. You just are restricted from importing certain 
things, which happens to include this class of exception. Probably it is 
safe for Zope to allow importing this in restricted code; feel free to 
file a bug report.


But you have easy avenues to deal with this: either declare that class 
safe for import or take a couple extra seconds and write it in an 
External Method.


--jcc
--
Building Websites with Plone
http://plonebook.packtpub.com/

Enfold Systems, LLC
http://www.enfoldsystems.com
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
http://mail.zope.org/mailman/listinfo/zope-announce

http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] Trapping zope exceptions in python script

2005-12-14 Thread Andreas Jung



--On 14. Dezember 2005 16:20:57 -0500 Jonathan [EMAIL PROTECTED] wrote:


Thanks to everyone for the feedback... the bottom line seems to be that
you can NOT trap zope exceptions in a python script... which seems a bit
odd.



Nothing is odd. I just told you to avoid *bare* try..excepts..as Tino 
pointed out you should must reraise re-raise excepctions like 
ConflictErrors. Otherwise if you don#t know what you're doing, don't use 
bare try...excepts.



-aj

pgpKwTfDJEqDB.pgp
Description: PGP signature
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )