Patchwork D1080: hgweb: fix logging to use native strings as appropriate

login
register
mail settings
Submitter phabricator
Date Oct. 15, 2017, 3:59 a.m.
Message ID <a91da8e695ff45bb6357ad9c02d58604@localhost.localdomain>
Download mbox | patch
Permalink /patch/24927/
State Not Applicable
Headers show

Comments

phabricator - Oct. 15, 2017, 3:59 a.m.
This revision was automatically updated to reflect the committed changes.
Closed by commit rHG8782076874f5: hgweb: fix logging to use native strings as appropriate (authored by durin42, committed by ).

CHANGED PRIOR TO COMMIT
  https://phab.mercurial-scm.org/D1080?vs=2744&id=2760#toc

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D1080?vs=2744&id=2760

REVISION DETAIL
  https://phab.mercurial-scm.org/D1080

AFFECTED FILES
  mercurial/hgweb/server.py

CHANGE DETAILS




To: durin42, #hg-reviewers, yuja
Cc: yuja, indygreg, mercurial-devel

Patch

diff --git a/mercurial/hgweb/server.py b/mercurial/hgweb/server.py
--- a/mercurial/hgweb/server.py
+++ b/mercurial/hgweb/server.py
@@ -17,6 +17,7 @@ 
 from ..i18n import _
 
 from .. import (
+    encoding,
     error,
     pycompat,
     util,
@@ -67,25 +68,26 @@ 
         httpservermod.basehttprequesthandler.__init__(self, *args, **kargs)
 
     def _log_any(self, fp, format, *args):
-        fp.write("%s - - [%s] %s\n" % (self.client_address[0],
-                                       self.log_date_time_string(),
-                                       format % args))
+        fp.write(pycompat.sysbytes(
+            r"%s - - [%s] %s" % (self.client_address[0],
+                                 self.log_date_time_string(),
+                                 format % args)) + '\n')
         fp.flush()
 
     def log_error(self, format, *args):
         self._log_any(self.server.errorlog, format, *args)
 
     def log_message(self, format, *args):
         self._log_any(self.server.accesslog, format, *args)
 
-    def log_request(self, code='-', size='-'):
+    def log_request(self, code=r'-', size=r'-'):
         xheaders = []
         if util.safehasattr(self, 'headers'):
             xheaders = [h for h in self.headers.items()
-                        if h[0].startswith('x-')]
-        self.log_message('"%s" %s %s%s',
+                        if h[0].startswith(r'x-')]
+        self.log_message(r'"%s" %s %s%s',
                          self.requestline, str(code), str(size),
-                         ''.join([' %s:%s' % h for h in sorted(xheaders)]))
+                         r''.join([r' %s:%s' % h for h in sorted(xheaders)]))
 
     def do_write(self):
         try:
@@ -101,9 +103,13 @@ 
             self._start_response("500 Internal Server Error", [])
             self._write("Internal Server Error")
             self._done()
-            tb = "".join(traceback.format_exception(*sys.exc_info()))
-            self.log_error("Exception happened during processing "
-                           "request '%s':\n%s", self.path, tb)
+            tb = r"".join(traceback.format_exception(*sys.exc_info()))
+            # We need a native-string newline to poke in the log
+            # message, because we won't get a newline when using an
+            # r-string. This is the easy way out.
+            newline = chr(10)
+            self.log_error(r"Exception happened during processing "
+                           r"request '%s':%s%s", self.path, newline, tb)
 
     def do_GET(self):
         self.do_POST()
@@ -331,4 +337,4 @@ 
         return cls(ui, app, (address, port), handler)
     except socket.error as inst:
         raise error.Abort(_("cannot start server at '%s:%d': %s")
-                         % (address, port, inst.args[1]))
+                          % (address, port, encoding.strtolocal(inst.args[1])))