bpo-32892: Use ast.Constant instead of specific constant AST types. (… · python/cpython@3f22811

@@ -48,10 +48,8 @@ def literal_eval(node_or_string):

4848

node_or_string = node_or_string.body

4949

def _convert_num(node):

5050

if isinstance(node, Constant):

51-

if isinstance(node.value, (int, float, complex)):

51+

if type(node.value) in (int, float, complex):

5252

return node.value

53-

elif isinstance(node, Num):

54-

return node.n

5553

raise ValueError('malformed node or string: ' + repr(node))

5654

def _convert_signed_num(node):

5755

if isinstance(node, UnaryOp) and isinstance(node.op, (UAdd, USub)):

@@ -64,10 +62,6 @@ def _convert_signed_num(node):

6462

def _convert(node):

6563

if isinstance(node, Constant):

6664

return node.value

67-

elif isinstance(node, (Str, Bytes)):

68-

return node.s

69-

elif isinstance(node, Num):

70-

return node.n

7165

elif isinstance(node, Tuple):

7266

return tuple(map(_convert, node.elts))

7367

elif isinstance(node, List):

@@ -77,8 +71,6 @@ def _convert(node):

7771

elif isinstance(node, Dict):

7872

return dict(zip(map(_convert, node.keys),

7973

map(_convert, node.values)))

80-

elif isinstance(node, NameConstant):

81-

return node.value

8274

elif isinstance(node, BinOp) and isinstance(node.op, (Add, Sub)):

8375

left = _convert_signed_num(node.left)

8476

right = _convert_num(node.right)

@@ -329,3 +321,66 @@ def generic_visit(self, node):

329321

else:

330322

setattr(node, field, new_node)

331323

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+

}