bpo-32892: Use ast.Constant instead of specific constant AST types. (… · python/cpython@3f22811 (original) (raw)
`@@ -48,10 +48,8 @@ def literal_eval(node_or_string):
`
48
48
`node_or_string = node_or_string.body
`
49
49
`def _convert_num(node):
`
50
50
`if isinstance(node, Constant):
`
51
``
`-
if isinstance(node.value, (int, float, complex)):
`
``
51
`+
if type(node.value) in (int, float, complex):
`
52
52
`return node.value
`
53
``
`-
elif isinstance(node, Num):
`
54
``
`-
return node.n
`
55
53
`raise ValueError('malformed node or string: ' + repr(node))
`
56
54
`def _convert_signed_num(node):
`
57
55
`if isinstance(node, UnaryOp) and isinstance(node.op, (UAdd, USub)):
`
`@@ -64,10 +62,6 @@ def _convert_signed_num(node):
`
64
62
`def _convert(node):
`
65
63
`if isinstance(node, Constant):
`
66
64
`return node.value
`
67
``
`-
elif isinstance(node, (Str, Bytes)):
`
68
``
`-
return node.s
`
69
``
`-
elif isinstance(node, Num):
`
70
``
`-
return node.n
`
71
65
`elif isinstance(node, Tuple):
`
72
66
`return tuple(map(_convert, node.elts))
`
73
67
`elif isinstance(node, List):
`
`@@ -77,8 +71,6 @@ def _convert(node):
`
77
71
`elif isinstance(node, Dict):
`
78
72
`return dict(zip(map(_convert, node.keys),
`
79
73
`map(_convert, node.values)))
`
80
``
`-
elif isinstance(node, NameConstant):
`
81
``
`-
return node.value
`
82
74
`elif isinstance(node, BinOp) and isinstance(node.op, (Add, Sub)):
`
83
75
`left = _convert_signed_num(node.left)
`
84
76
`right = _convert_num(node.right)
`
`@@ -329,3 +321,66 @@ def generic_visit(self, node):
`
329
321
`else:
`
330
322
`setattr(node, field, new_node)
`
331
323
`return node
`
``
324
+
``
325
+
``
326
`+
The following code is for backward compatibility.
`
``
327
`+
It will be removed in future.
`
``
328
+
``
329
`+
def _getter(self):
`
``
330
`+
return self.value
`
``
331
+
``
332
`+
def _setter(self, value):
`
``
333
`+
self.value = value
`
``
334
+
``
335
`+
Constant.n = property(_getter, _setter)
`
``
336
`+
Constant.s = property(_getter, _setter)
`
``
337
+
``
338
`+
class _ABC(type):
`
``
339
+
``
340
`+
def instancecheck(cls, inst):
`
``
341
`+
if not isinstance(inst, Constant):
`
``
342
`+
return False
`
``
343
`+
if cls in _const_types:
`
``
344
`+
try:
`
``
345
`+
value = inst.value
`
``
346
`+
except AttributeError:
`
``
347
`+
return False
`
``
348
`+
else:
`
``
349
`+
return type(value) in _const_types[cls]
`
``
350
`+
return type.instancecheck(cls, inst)
`
``
351
+
``
352
`+
def _new(cls, *args, **kwargs):
`
``
353
`+
if cls in _const_types:
`
``
354
`+
return Constant(*args, **kwargs)
`
``
355
`+
return Constant.new(cls, *args, **kwargs)
`
``
356
+
``
357
`+
class Num(Constant, metaclass=_ABC):
`
``
358
`+
_fields = ('n',)
`
``
359
`+
new = _new
`
``
360
+
``
361
`+
class Str(Constant, metaclass=_ABC):
`
``
362
`+
_fields = ('s',)
`
``
363
`+
new = _new
`
``
364
+
``
365
`+
class Bytes(Constant, metaclass=_ABC):
`
``
366
`+
_fields = ('s',)
`
``
367
`+
new = _new
`
``
368
+
``
369
`+
class NameConstant(Constant, metaclass=_ABC):
`
``
370
`+
new = _new
`
``
371
+
``
372
`+
class Ellipsis(Constant, metaclass=_ABC):
`
``
373
`+
_fields = ()
`
``
374
+
``
375
`+
def new(cls, *args, **kwargs):
`
``
376
`+
if cls is Ellipsis:
`
``
377
`+
return Constant(..., *args, **kwargs)
`
``
378
`+
return Constant.new(cls, *args, **kwargs)
`
``
379
+
``
380
`+
_const_types = {
`
``
381
`+
Num: (int, float, complex),
`
``
382
`+
Str: (str,),
`
``
383
`+
Bytes: (bytes,),
`
``
384
`+
NameConstant: (type(None), bool),
`
``
385
`+
Ellipsis: (type(...),),
`
``
386
`+
}
`