mercurial/extensions.py
branchstable
changeset 50751 b9eb65a1ec14
parent 49602 f4a363b25859
child 50753 19108906abaf
--- a/mercurial/extensions.py	Tue Jun 27 08:39:12 2023 +0200
+++ b/mercurial/extensions.py	Tue Jun 27 22:31:44 2023 +0200
@@ -885,16 +885,31 @@
     with open(path, b'rb') as src:
         root = ast.parse(src.read(), path)
     cmdtable = {}
+
+    # Python 3.12 started removing Bytes and Str and deprecate harder
+    use_constant = 'Bytes' not in vars(ast)
+
     for node in _walkcommand(root):
         if not node.args:
             continue
         a = node.args[0]
-        if isinstance(a, ast.Str):
-            name = pycompat.sysbytes(a.s)
-        elif isinstance(a, ast.Bytes):
-            name = a.s
-        else:
-            continue
+        if use_constant:  # Valid since Python 3.8
+            if isinstance(a, ast.Constant):
+                if isinstance(a.value, str):
+                    name = pycompat.sysbytes(a.value)
+                elif isinstance(a.value, bytes):
+                    name = a.value
+                else:
+                    continue
+            else:
+                continue
+        else:  # Valid until 3.11
+            if isinstance(a, ast.Str):
+                name = pycompat.sysbytes(a.s)
+            elif isinstance(a, ast.Bytes):
+                name = a.s
+            else:
+                continue
         cmdtable[name] = (None, [], b'')
     return cmdtable