diff --git a/mercurial/hgweb/request.py b/mercurial/hgweb/request.py --- a/mercurial/hgweb/request.py +++ b/mercurial/hgweb/request.py @@ -162,11 +162,11 @@ def parserequestfromenv(env, reponame=No # strings on Python 3 must be between \00000-\000FF. We deal with bytes # in Mercurial, so mass convert string keys and values to bytes. if pycompat.ispy3: - env = {k.encode('latin-1'): v for k, v in pycompat.iteritems(env)} - env = { - k: v.encode('latin-1') if isinstance(v, str) else v - for k, v in pycompat.iteritems(env) - } + def tobytes(s): + if not isinstance(s, str): + return s + return s.encode('latin-1') + env = {tobytes(k): tobytes(v) for k, v in pycompat.iteritems(env)} # Some hosting solutions are emulating hgwebdir, and dispatching directly # to an hgweb instance using this environment variable. This was always