# HG changeset patch # User Matt Mackall # Date 2014-02-05 23:23:35 # Node ID 6863d42eb59a39ea483eec0728b3a5d6836d20b6 # Parent 7d269e7620c48eee93871d33e673b7a8acb503cc hgweb: hack around mimetypes encoding thinko (issue4160) A correct patch for this has existed in Python's BTS for 3 years (http://bugs.python.org/issue9291), so waiting for it to be fixed upstream is probably not a viable strategy. Instead, we add this horrible hack to workaround the issue in existing copies of Python 2.4-2.7. diff --git a/mercurial/hgweb/server.py b/mercurial/hgweb/server.py --- a/mercurial/hgweb/server.py +++ b/mercurial/hgweb/server.py @@ -322,7 +322,20 @@ def create_server(ui, app): cls = MercurialHTTPServer # ugly hack due to python issue5853 (for threaded use) - import mimetypes; mimetypes.init() + try: + import mimetypes + mimetypes.init() + except UnicodeDecodeError: + # Python 2.x's mimetypes module attempts to decode strings + # from Windows' ANSI APIs as ascii (fail), then re-encode them + # as ascii (clown fail), because the default Python Unicode + # codec is hardcoded as ascii. + + reload(sys) # resurrect sys.setdefaultencoding() + oldenc = sys.getdefaultencoding() + sys.setdefaultencoding("latin1") # or any full 8-bit encoding + mimetypes.init() + sys.setdefaultencoding(oldenc) address = ui.config('web', 'address', '') port = util.getport(ui.config('web', 'port', 8000))