Better solution for attribute access on integer literals.

This commit is contained in:
Mark Dickinson 2010-06-30 09:05:47 +00:00
parent 81ad8ccdfb
commit b67e15c53c
1 changed files with 8 additions and 14 deletions

View File

@ -316,22 +316,11 @@ class Unparser:
self.write("`") self.write("`")
def _Num(self, t): def _Num(self, t):
if isinstance(t.n, float): if isinstance(t.n, float) and math.isinf(t.n):
# A float literal should be nonnegative, and not a nan. # Subsitute overflowing decimal literal for AST infinity
# It could be an infinity, though; in that case we self.write("1e" + repr(sys.float_info.max_10_exp + 1))
# substitute an overflowing decimal value.
assert not math.isnan(t.n)
assert math.copysign(1.0, t.n) > 0.0
if math.isinf(t.n):
self.write("1e" + repr(sys.float_info.max_10_exp + 1))
else:
self.write(repr(t.n))
else: else:
# Parenthesize integer literals to avoid turning
# "3 .__abs__()" into "3.__abs__()".
self.write("(")
self.write(repr(t.n)) self.write(repr(t.n))
self.write(")")
def _List(self, t): def _List(self, t):
self.write("[") self.write("[")
@ -449,6 +438,11 @@ class Unparser:
def _Attribute(self,t): def _Attribute(self,t):
self.dispatch(t.value) self.dispatch(t.value)
# Special case: 3.__abs__() is a syntax error, so if t.value
# is an integer literal then we need to either parenthesize
# it or add an extra space to get 3 .__abs__().
if isinstance(t.value, ast.Num) and isinstance(t.value.n, int):
self.write(" ")
self.write(".") self.write(".")
self.write(t.attr) self.write(t.attr)