changeset 45962:3db545fccac1

extensions: avoid a crash when the version isn't properly byteified on py3 We already force bytestr on the `testedwith` and `buglink` attributes in dispatch.py when generating a bug report with a similar comment about not every extension being ported to py3. We should do the same here, so the function can be properly typed. Differential Revision: https://phab.mercurial-scm.org/D9433
author Matt Harbison <matt_harbison@yahoo.com>
date Fri, 27 Nov 2020 14:54:37 -0500
parents 464539c305aa
children 250e18437e30
files mercurial/extensions.py tests/test-extension.t
diffstat 2 files changed, 12 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/extensions.py	Fri Nov 27 19:35:37 2020 -0500
+++ b/mercurial/extensions.py	Fri Nov 27 14:54:37 2020 -0500
@@ -936,6 +936,10 @@
         version = b''
     if isinstance(version, (list, tuple)):
         version = b'.'.join(pycompat.bytestr(o) for o in version)
+    else:
+        # version data should be bytes, but not all extensions are ported
+        # to py3.
+        version = stringutil.forcebytestr(version)
     return version
 
 
--- a/tests/test-extension.t	Fri Nov 27 19:35:37 2020 -0500
+++ b/tests/test-extension.t	Fri Nov 27 14:54:37 2020 -0500
@@ -1399,12 +1399,20 @@
   > cmdtable = {}
   > command = registrar.command(cmdtable)
   > class Bogon(Exception): pass
+  > # NB: version should be bytes; simulating extension not ported to py3
+  > __version__ = '1.0.0'
   > @command(b'throw', [], b'hg throw', norepo=True)
   > def throw(ui, **opts):
   >     """throws an exception"""
   >     raise Bogon()
   > EOF
 
+Test extension without proper byteification of key attributes doesn't crash when
+accessed.
+
+  $ hg version -v --config extensions.throw=throw.py | grep '^ '
+    throw  external  1.0.0
+
 No declared supported version, extension complains:
   $ hg --config extensions.throw=throw.py throw 2>&1 | egrep '^\*\*'
   ** Unknown exception encountered with possibly-broken third-party extension throw