changeset 5862:5b92be3da023

work around crash of AdvancedSearch macro rendering caused by non-ascii mimetypes.types_map entries
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 27 May 2012 20:21:32 +0200
parents b36c75bae437
children 5f5faedac588
files MoinMoin/macro/AdvancedSearch.py
diffstat 1 files changed, 8 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/macro/AdvancedSearch.py	Sat May 26 17:11:13 2012 +0400
+++ b/MoinMoin/macro/AdvancedSearch.py	Sun May 27 20:21:32 2012 +0200
@@ -7,6 +7,7 @@
 
     @license: GNU GPL, see COPYING for details.
 """
+import sys
 
 from MoinMoin import wikiutil
 from MoinMoin.i18n import languages
@@ -116,11 +117,17 @@
             searchedlang, 3, True)
 
     # mimetype selection
+    # mimetypes.types_map has non-ascii encoded bytestrings sometimes!
+    coding = sys.getdefaultencoding() # same call used by stdlib mimetypes.py on win32
+    if coding == 'ascii':  # we might get 'ascii'
+        coding = 'utf-8'  # but we upgrade to utf-8, which is sometimes used for /etc/mime.types
+    decode = lambda s: s.decode(coding, 'replace') # use 'replace' -> never crash
     mimetype = form_get(request, 'mimetype')
     mt_select = makeSelection('mimetype',
             [('', _('any mimetype'))] +
             [(type, 'all %s files' % type) for type in getMimetypes()] +
-            [(m[1], '*%s - %s' % m) for m in sorted(mimetypes.types_map.items())],
+            [(decode(m[1]), '*%s - %s' % (decode(m[0]), decode(m[1])))
+             for m in sorted(mimetypes.types_map.items())],
             mimetype, 3, True)
 
     # misc search options (checkboxes)