diff MoinMoin/action/AttachFile.py @ 5901:b9450db6c129

Attachment handler: catch all Zip-related errors I had faced the problem with attached .ppt file: MoinMoin tried to check if the file is a Zip file, but zipfile module throwed BadZipfile("Bad magic number for central directory"). Since, according to http://docs.python.org/dev/library/zipfile.html zipfile doesn't have a common base exception we can try to handle, we can only enumerate all possible exceptions. Currently, they are zipfile.BadZipfile, zipfile.LargeZipFile and RuntimeError. With Python 3.2 there will be zipfile.BadZipFile. Signed-off-by: Eygene Ryabinkin <rea@codelabs.ru>
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 08 Dec 2012 22:54:04 +0100
parents 78f1be3c8777
children 7e7e1cbb9d3f
line wrap: on
line diff
--- a/MoinMoin/action/AttachFile.py	Sat Dec 08 21:55:29 2012 +0100
+++ b/MoinMoin/action/AttachFile.py	Sat Dec 08 22:54:04 2012 +0100
@@ -394,7 +394,7 @@
                         links.append(fmt.url(1, getAttachUrl(pagename, file, request, do='unzip')) +
                                      fmt.text(label_unzip) +
                                      fmt.url(0))
-            except RuntimeError:
+            except (RuntimeError, zipfile.BadZipfile, zipfile.LargeZipFile):
                 # We don't want to crash with a traceback here (an exception
                 # here could be caused by an uploaded defective zip file - and
                 # if we crash here, the user does not get a UI to remove the
@@ -402,6 +402,8 @@
                 # RuntimeError is raised by zipfile stdlib module in case of
                 # problems (like inconsistent slash and backslash usage in the
                 # archive).
+                # BadZipfile/LargeZipFile are raised when there are some
+                # specific problems with the archive file.
                 logging.exception("An exception within zip file attachment handling occurred:")
 
             html.append(fmt.listitem(1))
@@ -989,7 +991,7 @@
                         'filelist': ', '.join(not_overwritten), }
             else:
                 msg = _("Attachment '%(filename)s' unzipped.") % {'filename': filename}
-    except RuntimeError, err:
+    except (RuntimeError, zipfile.BadZipfile, zipfile.LargeZipFile), err:
         # We don't want to crash with a traceback here (an exception
         # here could be caused by an uploaded defective zip file - and
         # if we crash here, the user does not get a UI to remove the
@@ -997,6 +999,8 @@
         # RuntimeError is raised by zipfile stdlib module in case of
         # problems (like inconsistent slash and backslash usage in the
         # archive).
+        # BadZipfile/LargeZipFile are raised when there are some
+        # specific problems with the archive file.
         logging.exception("An exception within zip file attachment handling occurred:")
         msg = _("A severe error occurred:") + ' ' + str(err)
 
@@ -1068,7 +1072,7 @@
                 request.write(wikiutil.escape("%-46s %s %12d\n" % (zinfo.filename, date, zinfo.file_size)))
             request.write("</pre>")
             return
-    except RuntimeError:
+    except (RuntimeError, zipfile.BadZipfile, zipfile.LargeZipFile):
         # We don't want to crash with a traceback here (an exception
         # here could be caused by an uploaded defective zip file - and
         # if we crash here, the user does not get a UI to remove the
@@ -1076,6 +1080,8 @@
         # RuntimeError is raised by zipfile stdlib module in case of
         # problems (like inconsistent slash and backslash usage in the
         # archive).
+        # BadZipfile/LargeZipFile are raised when there are some
+        # specific problems with the archive file.
         logging.exception("An exception within zip file attachment handling occurred:")
         return