extension: access special module members using sysstr
authorPierre-Yves David <pierre-yves.david@octobus.net>
Thu, 31 Aug 2023 01:47:07 +0200
changeset 50921 538c5a48e8f4
parent 50920 882687259181
child 50922 0e6cea0c3113
extension: access special module members using sysstr These extensions variables and mapping are module attributes so they should be dealt with unicode `str` in Python 3. We move the part that deal with reading theses variable and checking their validity to use unicode `str` string.
mercurial/extensions.py
--- a/mercurial/extensions.py	Wed Aug 30 11:50:01 2023 +0200
+++ b/mercurial/extensions.py	Thu Aug 31 01:47:07 2023 +0200
@@ -155,10 +155,11 @@
     elif isinstance(xs, dict):
         for k, v in xs.items():
             _rejectunicode(name, k)
-            _rejectunicode(b'%s.%s' % (name, stringutil.forcebytestr(k)), v)
-    elif isinstance(xs, type(u'')):
+            k = pycompat.sysstr(k)
+            _rejectunicode('%s.%s' % (name, k), v)
+    elif isinstance(xs, str):
         raise error.ProgrammingError(
-            b"unicode %r found in %s" % (xs, name),
+            b"unicode %r found in %s" % (xs, stringutil.forcebytestr(name)),
             hint=b"use b'' to make it byte string",
         )
 
@@ -182,15 +183,15 @@
 
 def _validatetables(ui, mod):
     """Sanity check for loadable tables provided by extension module"""
-    for t in [b'cmdtable', b'colortable', b'configtable']:
+    for t in ['cmdtable', 'colortable', 'configtable']:
         _rejectunicode(t, getattr(mod, t, {}))
     for t in [
-        b'filesetpredicate',
-        b'internalmerge',
-        b'revsetpredicate',
-        b'templatefilter',
-        b'templatefunc',
-        b'templatekeyword',
+        'filesetpredicate',
+        'internalmerge',
+        'revsetpredicate',
+        'templatefilter',
+        'templatefunc',
+        'templatekeyword',
     ]:
         o = getattr(mod, t, None)
         if o:
@@ -349,7 +350,7 @@
     #
     # This one is for the list of item that must be run before running any setup
     earlyextraloaders = [
-        (b'configtable', configitems, b'loadconfigtable'),
+        ('configtable', configitems, 'loadconfigtable'),
     ]
 
     ui.log(b'extension', b'- loading configtable attributes\n')
@@ -434,14 +435,14 @@
     #   which takes (ui, extensionname, extraobj) arguments
     ui.log(b'extension', b'- loading extension registration objects\n')
     extraloaders = [
-        (b'cmdtable', commands, b'loadcmdtable'),
-        (b'colortable', color, b'loadcolortable'),
-        (b'filesetpredicate', fileset, b'loadpredicate'),
-        (b'internalmerge', filemerge, b'loadinternalmerge'),
-        (b'revsetpredicate', revset, b'loadpredicate'),
-        (b'templatefilter', templatefilters, b'loadfilter'),
-        (b'templatefunc', templatefuncs, b'loadfunction'),
-        (b'templatekeyword', templatekw, b'loadkeyword'),
+        ('cmdtable', commands, 'loadcmdtable'),
+        ('colortable', color, 'loadcolortable'),
+        ('filesetpredicate', fileset, 'loadpredicate'),
+        ('internalmerge', filemerge, 'loadinternalmerge'),
+        ('revsetpredicate', revset, 'loadpredicate'),
+        ('templatefilter', templatefilters, 'loadfilter'),
+        ('templatefunc', templatefuncs, 'loadfunction'),
+        ('templatekeyword', templatekw, 'loadkeyword'),
     ]
     with util.timedcm('load registration objects') as stats:
         _loadextra(ui, newindex, extraloaders)