Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b67e15c

Browse files
committed
Better solution for attribute access on integer literals.
1 parent 81ad8cc commit b67e15c

1 file changed

Lines changed: 8 additions & 14 deletions

File tree

Demo/parser/unparse.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -316,22 +316,11 @@ def _Repr(self, t):
316316
self.write("`")
317317

318318
def _Num(self, t):
319-
if isinstance(t.n, float):
320-
# A float literal should be nonnegative, and not a nan.
321-
# It could be an infinity, though; in that case we
322-
# substitute an overflowing decimal value.
323-
assert not math.isnan(t.n)
324-
assert math.copysign(1.0, t.n) > 0.0
325-
if math.isinf(t.n):
326-
self.write("1e" + repr(sys.float_info.max_10_exp + 1))
327-
else:
328-
self.write(repr(t.n))
319+
if isinstance(t.n, float) and math.isinf(t.n):
320+
# Subsitute overflowing decimal literal for AST infinity
321+
self.write("1e" + repr(sys.float_info.max_10_exp + 1))
329322
else:
330-
# Parenthesize integer literals to avoid turning
331-
# "3 .__abs__()" into "3.__abs__()".
332-
self.write("(")
333323
self.write(repr(t.n))
334-
self.write(")")
335324

336325
def _List(self, t):
337326
self.write("[")
@@ -449,6 +438,11 @@ def _BoolOp(self, t):
449438

450439
def _Attribute(self,t):
451440
self.dispatch(t.value)
441+
# Special case: 3.__abs__() is a syntax error, so if t.value
442+
# is an integer literal then we need to either parenthesize
443+
# it or add an extra space to get 3 .__abs__().
444+
if isinstance(t.value, ast.Num) and isinstance(t.value.n, int):
445+
self.write(" ")
452446
self.write(".")
453447
self.write(t.attr)
454448

0 commit comments

Comments
 (0)