Skip to content

Commit b67374e

Browse files
committed
tests: Migrate away from removed ast features
See python/cpython#119563.
1 parent 24fbeaf commit b67374e

File tree

2 files changed

+33
-40
lines changed

2 files changed

+33
-40
lines changed

tests/scripts/python/testapigen.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ def update_tree(self):
144144
}
145145
# replace variables
146146
for node in ast.walk(self.tree):
147-
if isinstance(node, ast.Str) and node.s in variables:
148-
node.s = variables[node.s]
147+
if isinstance(node, ast.Constant) and node.value in variables:
148+
node.value = variables[node.value]
149149

150150
def write_header(self, output):
151151
"""Generate script header (just comments by default)."""
@@ -336,8 +336,8 @@ def update_nodes(tree):
336336
output = StringIO()
337337
unparsed = UnparsePython(output=output)
338338
unparsed.add(node.args[0])
339-
node.args.append(ast.Str(output.getvalue()))
340-
node.args.append(ast.Str(str(node.func.lineno)))
339+
node.args.append(ast.Constant(output.getvalue()))
340+
node.args.append(ast.Constant(str(node.func.lineno)))
341341

342342

343343
def get_tests(path):

tests/scripts/python/unparse.py

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def is_bool(node):
147147
@staticmethod
148148
def is_number(node):
149149
"""Check if the node is a number."""
150-
return (isinstance(node, ast.Num) or
150+
return ((isinstance(node, ast.Constant) and isinstance(node.value, int)) or
151151
(isinstance(node, ast.UnaryOp) and
152152
isinstance(node.op, (ast.UAdd, ast.USub))))
153153

@@ -201,7 +201,7 @@ def _ast_compare(self, node):
201201

202202
def _ast_constant(self, node):
203203
"""Add an AST Constant in output."""
204-
self.add(repr(node.s))
204+
self.add(repr(node.value))
205205

206206
def _ast_dict(self, node):
207207
"""Add an AST Dict in output."""
@@ -214,7 +214,7 @@ def _ast_dict(self, node):
214214

215215
def _ast_expr(self, node):
216216
"""Add an AST Expr in output."""
217-
if not isinstance(node.value, ast.Str): # ignore docstrings
217+
if not isinstance(node.value, ast.Constant): # ignore docstrings
218218
self.add(
219219
self.fill,
220220
node.value,
@@ -283,7 +283,6 @@ def _ast_name(self, node):
283283

284284
def _ast_num(self, node):
285285
"""Add an AST Num in output."""
286-
# note: deprecated since Python 3.8, replaced by ast.Constant
287286
self.add(repr(node.n))
288287

289288
def _ast_pass(self, node): # pylint: disable=unused-argument
@@ -298,7 +297,6 @@ def _ast_return(self, node):
298297

299298
def _ast_str(self, node):
300299
"""Add an AST Str in output."""
301-
# note: deprecated since Python 3.8, replaced by ast.Constant
302300
self._ast_constant(node)
303301

304302
def _ast_subscript(self, node):
@@ -405,11 +403,11 @@ def _ast_compare(self, node):
405403
def _ast_constant(self, node):
406404
"""Add an AST Constant in output."""
407405
if isinstance(node.value, str):
408-
self.add('"%s"' % node.s.replace('$', '\\$').replace('@', '\\@'))
406+
self.add('"%s"' % node.value.replace('$', '\\$').replace('@', '\\@'))
409407
elif node.value is None:
410408
self.add('undef')
411409
else:
412-
self.add(repr(node.s))
410+
self.add(repr(node.value))
413411

414412
def _ast_dict(self, node):
415413
"""Add an AST Dict in output."""
@@ -422,7 +420,7 @@ def _ast_dict(self, node):
422420

423421
def _ast_expr(self, node):
424422
"""Add an AST Expr in output."""
425-
if not isinstance(node.value, ast.Str): # ignore docstrings
423+
if not isinstance(node.value, ast.Constant): # ignore docstrings
426424
self.add(
427425
self.fill,
428426
node.value,
@@ -512,8 +510,7 @@ def _ast_return(self, node):
512510

513511
def _ast_str(self, node):
514512
"""Add an AST Str in output."""
515-
# note: deprecated since Python 3.8, replaced by ast.Constant
516-
self.add('"%s"' % node.s.replace('$', '\\$').replace('@', '\\@'))
513+
self._ast_constant(node)
517514

518515
def _ast_subscript(self, node):
519516
"""Add an AST Subscript in output."""
@@ -548,11 +545,11 @@ def _ast_attribute(self, node):
548545
def _ast_constant(self, node):
549546
"""Add an AST Constant in output."""
550547
if isinstance(node.value, str):
551-
self.add('"%s"' % node.s.replace('#{', '\\#{'))
548+
self.add('"%s"' % node.value.replace('#{', '\\#{'))
552549
elif node.value is None:
553550
self.add('nil')
554551
else:
555-
self.add(repr(node.s))
552+
self.add(repr(node.value))
556553

557554
def _ast_dict(self, node):
558555
"""Add an AST Dict in output."""
@@ -619,8 +616,7 @@ def _ast_pass(self, node):
619616

620617
def _ast_str(self, node):
621618
"""Add an AST Str in output."""
622-
# note: deprecated since Python 3.8, replaced by ast.Constant
623-
self.add('"%s"' % node.s)
619+
self._ast_constant(node)
624620

625621

626622
class UnparseLua(UnparsePython):
@@ -663,7 +659,7 @@ def _ast_constant(self, node):
663659
if node.value is None:
664660
self.add('nil')
665661
else:
666-
self.add(repr(node.s))
662+
self.add(repr(node.value))
667663

668664
def _ast_dict(self, node):
669665
"""Add an AST Dict in output."""
@@ -744,7 +740,7 @@ def __init__(self, *args, **kwargs):
744740

745741
def _ast_assign(self, node):
746742
"""Add an AST Assign in output."""
747-
exclude_types = (ast.Dict, ast.List, ast.Str, ast.Subscript)
743+
exclude_types = (ast.Dict, ast.List, ast.Constant, ast.Subscript)
748744
self.add(
749745
self.fill,
750746
'set ',
@@ -821,11 +817,11 @@ def _ast_compare(self, node):
821817
def _ast_constant(self, node):
822818
"""Add an AST Constant in output."""
823819
if isinstance(node.value, str):
824-
self.add('"%s"' % node.s.replace('$', '\\$'))
820+
self.add('"%s"' % node.value.replace('$', '\\$'))
825821
elif node.value is None:
826822
self.add('$::weechat::WEECHAT_NULL')
827823
else:
828-
self.add(repr(node.s))
824+
self.add(repr(node.value))
829825

830826
def _ast_dict(self, node):
831827
"""Add an AST Dict in output."""
@@ -903,8 +899,7 @@ def _ast_return(self, node):
903899

904900
def _ast_str(self, node):
905901
"""Add an AST Str in output."""
906-
# note: deprecated since Python 3.8, replaced by ast.Constant
907-
self.add('"%s"' % node.s.replace('$', '\\$'))
902+
self._ast_constant(node)
908903

909904
def _ast_subscript(self, node):
910905
"""Add an AST Subscript in output."""
@@ -965,8 +960,8 @@ def _ast_attribute(self, node):
965960
def _ast_binop(self, node):
966961
"""Add an AST BinOp in output."""
967962
if isinstance(node.op, ast.Add) and \
968-
(isinstance(node.left, (ast.Name, ast.Str)) or
969-
isinstance(node.right, (ast.Name, ast.Str))):
963+
(isinstance(node.left, (ast.Name, ast.Constant)) or
964+
isinstance(node.right, (ast.Name, ast.Constant))):
970965
self.add(
971966
'(string-append ',
972967
node.left,
@@ -1014,12 +1009,12 @@ def _ast_compare(self, node):
10141009

10151010
def _ast_constant(self, node):
10161011
"""Add an AST Constant in output."""
1017-
if isinstance(node.s, str):
1018-
self.add('"%s"' % node.s)
1012+
if isinstance(node.value, str):
1013+
self.add('"%s"' % node.value)
10191014
elif node.value is None:
10201015
self.add('#nil')
10211016
else:
1022-
self.add(repr(node.s))
1017+
self.add(repr(node.value))
10231018

10241019
def _ast_dict(self, node):
10251020
"""Add an AST Dict in output."""
@@ -1111,8 +1106,7 @@ def _ast_return(self, node):
11111106

11121107
def _ast_str(self, node):
11131108
"""Add an AST Str in output."""
1114-
# note: deprecated since Python 3.8, replaced by ast.Constant
1115-
self.add('"%s"' % node.s)
1109+
self._ast_constant(node)
11161110

11171111
def _ast_subscript(self, node):
11181112
"""Add an AST Subscript in output."""
@@ -1149,7 +1143,7 @@ def _ast_constant(self, node):
11491143
if node.value is None:
11501144
self.add('null')
11511145
else:
1152-
self.add(repr(node.s))
1146+
self.add(repr(node.value))
11531147

11541148
def _ast_functiondef(self, node):
11551149
"""Add an AST FunctionDef in output."""
@@ -1227,8 +1221,8 @@ def _ast_attribute(self, node):
12271221
def _ast_binop(self, node):
12281222
"""Add an AST BinOp in output."""
12291223
if isinstance(node.op, ast.Add) and \
1230-
(isinstance(node.left, (ast.Name, ast.Str)) or
1231-
isinstance(node.right, (ast.Name, ast.Str))):
1224+
(isinstance(node.left, (ast.Name, ast.Constant)) or
1225+
isinstance(node.right, (ast.Name, ast.Constant))):
12321226
str_op = '.'
12331227
else:
12341228
str_op = self.binop[node.op.__class__.__name__]
@@ -1253,12 +1247,12 @@ def _ast_call(self, node):
12531247

12541248
def _ast_constant(self, node):
12551249
"""Add an AST Constant in output."""
1256-
if isinstance(node.s, str):
1257-
self.add('"%s"' % node.s.replace('$', '\\$'))
1250+
if isinstance(node.value, str):
1251+
self.add('"%s"' % node.value.replace('$', '\\$'))
12581252
elif node.value is None:
12591253
self.add('NULL')
12601254
else:
1261-
self.add(repr(node.s))
1255+
self.add(repr(node.value))
12621256

12631257
def _ast_dict(self, node):
12641258
"""Add an AST Dict in output."""
@@ -1271,7 +1265,7 @@ def _ast_dict(self, node):
12711265

12721266
def _ast_expr(self, node):
12731267
"""Add an AST Expr in output."""
1274-
if not isinstance(node.value, ast.Str): # ignore docstrings
1268+
if not isinstance(node.value, ast.Constant): # ignore docstrings
12751269
self.add(
12761270
self.fill,
12771271
node.value,
@@ -1353,8 +1347,7 @@ def _ast_return(self, node):
13531347

13541348
def _ast_str(self, node):
13551349
"""Add an AST Str in output."""
1356-
# note: deprecated since Python 3.8, replaced by ast.Constant
1357-
self.add('"%s"' % node.s.replace('$', '\\$'))
1350+
self._ast_constant(node)
13581351

13591352
def _ast_subscript(self, node):
13601353
"""Add an AST Subscript in output."""

0 commit comments

Comments
 (0)