Bug 4160 - [hg serve] Crashes on Windows
Summary: [hg serve] Crashes on Windows
Status: RESOLVED FIXED
Alias: None
Product: Mercurial
Classification: Unclassified
Component: hgweb (show other bugs)
Version: 2.9
Hardware: PC Windows
: normal bug
Assignee: Bugzilla
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-02-05 08:14 UTC by Ruslan Yushchenko
Modified: 2014-07-19 14:17 UTC (History)
4 users (show)

See Also:
Python Version: ---


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ruslan Yushchenko 2014-02-05 08:14 UTC
d:\Repo\hg>hg serve

** unknown exception encountered, please report by visiting
** http://mercurial.selenic.com/wiki/BugTracker
** Python 2.7.6 (default, Nov 10 2013, 19:24:24) [MSC v.1500 64 bit (AMD64)]
** Mercurial Distributed SCM (version 2.9)
** Extensions loaded: gpg
Traceback (most recent call last):
  File "hg", line 42, in <module>

  File "mercurial\dispatch.pyo", line 28, in run
  File "mercurial\dispatch.pyo", line 69, in dispatch
  File "mercurial\dispatch.pyo", line 134, in _runcatch
  File "mercurial\dispatch.pyo", line 806, in _dispatch
  File "mercurial\dispatch.pyo", line 586, in runcommand
  File "mercurial\dispatch.pyo", line 897, in _runcommand
  File "mercurial\dispatch.pyo", line 868, in checkargs
  File "mercurial\dispatch.pyo", line 803, in <lambda>
  File "mercurial\util.pyo", line 511, in check
  File "mercurial\commands.pyo", line 5204, in serve

  File "mercurial\cmdutil.pyo", line 514, in service
  File "mercurial\commands.pyo", line 5214, in init

  File "mercurial\hgweb\server.pyo", line 325, in create_server
  File "mimetypes.pyo", line 358, in init
  File "mimetypes.pyo", line 258, in read_windows_registry
  File "mimetypes.pyo", line 249, in enum_types
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd8 in position 0: ordinal not in range(128)
Comment 1 Matt Mackall 2014-02-05 13:06 UTC
We've seen this before but I'm not marking it a duplicate. We've got a workaround for everything except hg serve / hgweb here:

http://bz.selenic.com/show_bug.cgi?id=3624
Comment 2 Matt Mackall 2014-02-05 17:11 UTC
Proposed fix:

diff -r 4d5322dec91f mercurial/hgweb/server.py
--- a/mercurial/hgweb/server.py	Sun Nov 17 20:16:14 2013 +0100
+++ b/mercurial/hgweb/server.py	Wed Feb 05 16:11:16 2014 -0600
@@ -322,7 +322,19 @@
         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)
+        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))
Comment 3 HG Bot 2014-02-05 19:15 UTC
Fixed by http://selenic.com/repo/hg/rev/6863d42eb59a
Matt Mackall <mpm@selenic.com>
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.

(please test the fix)
Comment 4 Ruslan Yushchenko 2014-02-12 09:26 UTC
I've replaced the server.pyo file with the updated server.py and got following traceback:

** unknown exception encountered, please report by visiting
** http://mercurial.selenic.com/wiki/BugTracker
** Python 2.7.6 (default, Nov 10 2013, 19:24:24) [MSC v.1500 64 bit (AMD64)]
** Mercurial Distributed SCM (version 2.9)
** Extensions loaded:
Traceback (most recent call last):
  File "hg", line 42, in <module>
  File "mercurial\dispatch.pyo", line 28, in run
  File "mercurial\dispatch.pyo", line 69, in dispatch
  File "mercurial\dispatch.pyo", line 134, in _runcatch
  File "mercurial\dispatch.pyo", line 806, in _dispatch
  File "mercurial\dispatch.pyo", line 586, in runcommand
  File "mercurial\dispatch.pyo", line 897, in _runcommand
  File "mercurial\dispatch.pyo", line 868, in checkargs
  File "mercurial\dispatch.pyo", line 803, in <lambda>
  File "mercurial\util.pyo", line 511, in check
  File "mercurial\commands.pyo", line 5204, in serve
  File "mercurial\cmdutil.pyo", line 514, in service
  File "mercurial\commands.pyo", line 5214, in init
  File "C:\Program Files\TortoiseHg\library.zip\mercurial\hgweb\server.py", line 334, in create_server
TypeError: reload() argument must be module
Comment 5 kiilerix 2014-02-22 14:29 UTC
That should be fixed by http://selenic.com/repo/hg/rev/ca970d6acedb .
Comment 6 Ruslan Yushchenko 2014-02-24 07:04 UTC
Yes, now it works. Thank you!