comparison mercurial/extensions.py @ 50755:b9eb65a1ec14 stable

extensions: address ast deprecations introduced in Python 3.12 Tests would fail with: .../mercurial/extensions.py:910: DeprecationWarning: ast.Str is deprecated and will be removed in Python 3.14; use ast.Constant instead if isinstance(a, ast.Str): .../mercurial/extensions.py:912: DeprecationWarning: ast.Bytes is deprecated and will be removed in Python 3.14; use ast.Constant instead elif isinstance(a, ast.Bytes): .../mercurial/extensions.py:913: DeprecationWarning: Attribute s is deprecated and will be removed in Python 3.14; use value instead name = a.s
author Mads Kiilerich <mads@kiilerich.com>
date Tue, 27 Jun 2023 22:31:44 +0200
parents f4a363b25859
children 19108906abaf
comparison
equal deleted inserted replaced
50754:f173c2c23289 50755:b9eb65a1ec14
883 This may raise IOError or SyntaxError. 883 This may raise IOError or SyntaxError.
884 """ 884 """
885 with open(path, b'rb') as src: 885 with open(path, b'rb') as src:
886 root = ast.parse(src.read(), path) 886 root = ast.parse(src.read(), path)
887 cmdtable = {} 887 cmdtable = {}
888
889 # Python 3.12 started removing Bytes and Str and deprecate harder
890 use_constant = 'Bytes' not in vars(ast)
891
888 for node in _walkcommand(root): 892 for node in _walkcommand(root):
889 if not node.args: 893 if not node.args:
890 continue 894 continue
891 a = node.args[0] 895 a = node.args[0]
892 if isinstance(a, ast.Str): 896 if use_constant: # Valid since Python 3.8
893 name = pycompat.sysbytes(a.s) 897 if isinstance(a, ast.Constant):
894 elif isinstance(a, ast.Bytes): 898 if isinstance(a.value, str):
895 name = a.s 899 name = pycompat.sysbytes(a.value)
896 else: 900 elif isinstance(a.value, bytes):
897 continue 901 name = a.value
902 else:
903 continue
904 else:
905 continue
906 else: # Valid until 3.11
907 if isinstance(a, ast.Str):
908 name = pycompat.sysbytes(a.s)
909 elif isinstance(a, ast.Bytes):
910 name = a.s
911 else:
912 continue
898 cmdtable[name] = (None, [], b'') 913 cmdtable[name] = (None, [], b'')
899 return cmdtable 914 return cmdtable
900 915
901 916
902 def _finddisabledcmd(ui, cmd, name, path, strict): 917 def _finddisabledcmd(ui, cmd, name, path, strict):