Issue1708080

classification
Title: float("1d") -> 1.0 (not ValueError as expected) [with patch]
Type: Severity: normal
Components: None Versions:
Milestone:
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: Nosy List: cgroves, pekka.klarck
Priority: normal Keywords:

Created on 2007-04-26.14:10:43 by pekka.klarck, last changed 2007-05-14.05:11:31 by cgroves.

Files
File name Uploaded Description Edit Remove
float.patch pekka.klarck, 2007-05-10.00:02:11
Messages
msg1562 (view) Author: Pekka Klärck (pekka.klarck) Date: 2007-04-26.14:10:43
Jython 2.2b1 on java1.5.0_10 (JIT: null)
Type "copyright", "credits" or "license" for more information.
>>> float("1d")
1.0
>>> float("1D")
1.0
>>>


Python 2.5 (r25:51908, Mar 13 2007, 08:13:14) 
[GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> float("1d")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for float(): 1d
>>> float("1D")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for float(): 1D
>>> 


msg1563 (view) Author: Pekka Klärck (pekka.klarck) Date: 2007-04-27.07:29:38
Jython 2.2b1 on java1.5.0_10 (JIT: null)
Type "copyright", "credits" or "license" for more information.
>>> import string
>>> for c in string.letters:
...     try:
...         i = float("2"+c)
...     except ValueError:
...         pass
...     else:
...         print c, i
...
d 2.0
f 2.0
D 2.0
F 2.0
msg1564 (view) Author: Pekka Klärck (pekka.klarck) Date: 2007-05-10.00:02:13
The reason for this odd behavior is that when float is created from a string java.lang.Double.valueOf is used and it allows so called format specifiers ("d" or "D" for double and "f" or "F" for float) at the end of the value. For more details see http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Double.html#valueOf(java.lang.String)

Attached patch simply throws a NumberFormatException if the string value ends with "d" or "f" (case insensitive) before even calling Double.valueOf (which will throw same exception for otherwise invalid strings). Formats like "1e5" are not affected by the change so unless there are some weird, but valid, float formats that end with "d" or "f" this fix ought to be ok. 
File Added: float.patch
msg1565 (view) Author: Pekka Klärck (pekka.klarck) Date: 2007-05-10.00:09:38
Forgot to mention that I run Lib/test/regrtest.py with and without the fix and got same failures. List of failures is below and failure in test_grammar seems to be somehow unexpected. My environment is Ubuntu 7.04 with Sun Java 1.6.0-b105.

54 tests OK.
1 test skipped:
    test_longexp
3 tests failed:
    test_cpickle test_descr test_grammar
1 fail unexpected on java1.6.0:
    test_grammar
msg1566 (view) Author: Charlie Groves (cgroves) Date: 2007-05-14.05:07:25
Applied in r3224.
msg1567 (view) Author: Charlie Groves (cgroves) Date: 2007-05-14.05:11:31
You should actually run dist/Lib/test/regrtest.py as that includes all of the CPython tests as well as the Jython specific ones in Lib/test.  It runs 145 tests OK, 55 skipped and 14 failed as of now.  

That doesn't explain your test_grammar failure though.  Could you run it directly and see what the output is?  If it's obviously a bug, create one.  If it's still nebulous, post it to jython-dev and we can dissect it further.
History
Date User Action Args
2007-04-26 14:10:43pekka.klarckcreate