hgmo: cast bytearray to bytes (bug 1506297); r=sheehan
authorGregory Szorc <gps@mozilla.com>
Sun, 11 Nov 2018 15:22:00 +0000
changeset 6726 0d4e27a04fb9
parent 6725 c6c4dc140811
child 6727 b6afa72eb701
push id3330
push usercosheehan@mozilla.com
push dateSun, 11 Nov 2018 15:22:07 +0000
treeherderversion-control-tools@0d4e27a04fb9 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssheehan
bugs1506297
hgmo: cast bytearray to bytes (bug 1506297); r=sheehan Mercurial 4.8 may emit bytearray instances from its WSGI application. This is apparently against PEP-3333 and mod_wsgi complains. This commit works around the bug by filtering for bytearray in WSGI application output and casting to bytes. The upstream bug should be fixed in 4.8.1. Differential Revision: https://phabricator.services.mozilla.com/D11539
hgext/hgmo/__init__.py
--- a/hgext/hgmo/__init__.py
+++ b/hgext/hgmo/__init__.py
@@ -95,16 +95,17 @@ from mercurial import (
     revset,
     scmutil,
     templatefilters,
     templateutil,
     util,
     wireprotov1server,
 )
 from mercurial.hgweb import (
+    request as requestmod,
     webcommands,
     webutil,
 )
 from mercurial.hgweb.common import (
     ErrorResponse,
     HTTP_NOT_FOUND,
 )
 
@@ -785,16 +786,24 @@ def mozbuildinfocommand(ui, repo, *paths
 
     # TODO send data to templater.
     # Use stable output and indentation to make testing easier.
     ui.write(json.dumps(d, indent=2, sort_keys=True))
     ui.write('\n')
     return
 
 
+def wsgisendresponse(orig, self):
+    for chunk in orig(self):
+        if isinstance(chunk, bytearray):
+            chunk = bytes(chunk)
+
+        yield chunk
+
+
 def pull(orig, repo, remote, *args, **kwargs):
     """Wraps exchange.pull to fetch the remote clonebundles.manifest."""
     res = orig(repo, remote, *args, **kwargs)
 
     if not repo.ui.configbool('hgmo', 'pullclonebundlesmanifest', False):
         return res
 
     if not remote.capable('clonebundles'):
@@ -946,16 +955,20 @@ def hgwebfastannotate(orig, req, fctx, u
     import hgext.fastannotate.support as fasupport
 
     diffopts = webutil.difffeatureopts(req, ui, 'annotate')
 
     return fasupport._doannotate(fctx, diffopts=diffopts)
 
 
 def extsetup(ui):
+    # TRACKING hg49 4.8 would emit bytearray instances against PEP-3333.
+    extensions.wrapfunction(requestmod.wsgiresponse, 'sendresponse',
+                            wsgisendresponse)
+
     extensions.wrapfunction(exchange, 'pull', pull)
     extensions.wrapfunction(webutil, 'changesetentry', changesetentry)
     extensions.wrapfunction(webutil, 'changelistentry', changelistentry)
     extensions.wrapfunction(bookmarks, 'updatefromremote', bmupdatefromremote)
     extensions.wrapfunction(webcommands, 'filelog', filelog)
 
     revset.symbols['reviewer'] = revset_reviewer
     revset.safesymbols.add('reviewer')