Re: Close as Many Files/External resourcs as possible in the face of exceptions
Le 21/11/11 06:46, GZ a crit: Hi, Here is my situation. A parent object owns a list of files (or other objects with a close() method). The close() method can sometimes fail and raise an exception. When the parent object's close() method is called, it needs to close down as many files it owns as possible, even if the close() function of some files fail. I also want to re-raise at least one of the original exceptions so that the outer program can handle it. What I come up is something like this, suppose L is a list that holds all the file objects. is_closed = set() try: for f in L: f.close() is_closed.add(f) except: try: raise #re-raise immediately, keeping context intact finally: for f in L: # close the rest of the file objects if f not in is_closed: try: f.close() except: pass It will re-raise the first exception and preserve the context and close as many other files as possible while ignoring any further exceptions. But this looks really awkward. And in the case that two files fail to close, I am not sure the best strategy is to ignore the second failure. What is a better way of handling such situation? Thanks, gz Not tested def close_all(L): is_not_closed = {} def close_(obj): try: obj.close() except Exception, why is_not_closed[obj] = why for f in L: close_(f) Now, your dictionnary contain the file as key and the exception message as value. -- Vincent V.V. Oqapy . Qarte+7 . PaQager -- http://mail.python.org/mailman/listinfo/python-list
Re: Close as Many Files/External resourcs as possible in the face of exceptions
GZ wrote: Here is my situation. A parent object owns a list of files (or other objects with a close() method). The close() method can sometimes fail and raise an exception. When the parent object's close() method is called, it needs to close down as many files it owns as possible, even if the close() function of some files fail. I also want to re-raise at least one of the original exceptions so that the outer program can handle it. [ ... ] It will re-raise the first exception and preserve the context and close as many other files as possible while ignoring any further exceptions. But this looks really awkward. And in the case that two files fail to close, I am not sure the best strategy is to ignore the second failure. I imagine you could save any caught exception instances in a list and study them later. Mel. -- http://mail.python.org/mailman/listinfo/python-list
Re: Close as Many Files/External resourcs as possible in the face of exceptions
On 11/21/2011 7:09 AM, Mel Wilson wrote: GZ wrote: Here is my situation. A parent object owns a list of files (or other objects with a close() method). The close() method can sometimes fail and raise an exception. When the parent object's close() method is called, it needs to close down as many files it owns as possible, even if the close() function of some files fail. I also want to re-raise at least one of the original exceptions so that the outer program can handle it. [ ... ] It will re-raise the first exception and preserve the context and close as many other files as possible while ignoring any further exceptions. But this looks really awkward. And in the case that two files fail to close, I am not sure the best strategy is to ignore the second failure. I imagine you could save any caught exception instances in a list and study them later. Yes, I would raise a custom exception instance that takes such a list of failures in its constructor. Give it a custom __str__ method to display them all. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Close as Many Files/External resourcs as possible in the face of exceptions
Hi, Here is my situation. A parent object owns a list of files (or other objects with a close() method). The close() method can sometimes fail and raise an exception. When the parent object's close() method is called, it needs to close down as many files it owns as possible, even if the close() function of some files fail. I also want to re-raise at least one of the original exceptions so that the outer program can handle it. What I come up is something like this, suppose L is a list that holds all the file objects. is_closed = set() try: for f in L: f.close() is_closed.add(f) except: try: raise #re-raise immediately, keeping context intact finally: for f in L: # close the rest of the file objects if f not in is_closed: try: f.close() except: pass It will re-raise the first exception and preserve the context and close as many other files as possible while ignoring any further exceptions. But this looks really awkward. And in the case that two files fail to close, I am not sure the best strategy is to ignore the second failure. What is a better way of handling such situation? Thanks, gz -- http://mail.python.org/mailman/listinfo/python-list