[Python-bugs-list] [ python-Bugs-620181 ] float('+ 0.0') fails
noreply@sourceforge.net
noreply@sourceforge.net
Wed, 09 Oct 2002 12:53:11 -0700
Bugs item #620181, was opened at 2002-10-08 08:14
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=620181&group_id=5470
Category: Python Interpreter Core
>Group: None
>Status: Closed
>Resolution: Wont Fix
Priority: 2
Submitted By: Alexander Schmolck (aschmolck)
>Assigned to: Tim Peters (tim_one)
Summary: float('+ 0.0') fails
Initial Comment:
In [25]: int('+ 0')
Out[25]: 0
In [26]: float('+ 0.0')
ValueError: invalid literal for float(): + 0.0
----------------------------------------------------------------------
>Comment By: Tim Peters (tim_one)
Date: 2002-10-09 15:53
Message:
Logged In: YES
user_id=31435
I agree with Raymond. There was no intent to allow
embedded whitespace in numeric literals, and changing that
now for the special case of a sign character in an integer
literal would buy nothing except the possibility of breaking
someone's code. So closing this as WontFix: you should
consider int('+ 2') as broken code that CPython just happens
to accept. The Language Reference manual doesn't allow for
this possibility, so it's in the category of undefined behavior.
----------------------------------------------------------------------
Comment By: Raymond Hettinger (rhettinger)
Date: 2002-10-09 12:30
Message:
Logged In: YES
user_id=80475
Looking at the source for PyOS_strtol, it appears to be a
implement quirk that int() accepts whitespace between
the sign and the rest of the number. Since it's possible
that people rely on the quirk (some accounting formats do
print intermediate whitespace after the sign), this quirk
probably should be left alone.
float() uses platform's strtod() function which tries various
strategies for making sense of the string. Liberalizing float
() to match int()'s unintended quirk would involve trying to
work around the strtod() black box and cluttering the code
considerably.
I recommend living with the minor inconsistency, closing
the bug report, and moving on to bigger game.
----------------------------------------------------------------------
Comment By: Alexander Schmolck (aschmolck)
Date: 2002-10-08 13:41
Message:
Logged In: YES
user_id=559641
I don't feel strongly about this, it just seems a very minor
inconsistency to me. It is not always true that embedded
whitespace isn't accepted in numeric literals. Whitespace
between sign and digits is discarded by `int`, but not by
`float`. Also, in source code or in interactive sessions -
0.0 is treated as a float literal (not as unary negation of
0.0), wheras "2 3" just never works.
----------------------------------------------------------------------
Comment By: Tim Peters (tim_one)
Date: 2002-10-08 12:02
Message:
Logged In: YES
user_id=31435
Why do you think this is a bug? It's *generally* true that
embedded whitespace isn't accepeted in numeric literals:
>>> int("2 3")
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: invalid literal for int(): 2 3
>>> import string
>>> string.atof("- 2")
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "C:\Code\python\lib\string.py", line 201, in atof
return _float(s)
ValueError: invalid literal for float(): - 2
>>>
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=620181&group_id=5470