Odd ValueError using float

Peter Otten __peter__ at web.de
Sat Mar 14 14:24:26 EDT 2015


emile wrote:

> On 03/14/2015 09:08 AM, Peter Otten wrote:
>> emile wrote:
>>
>>> On 03/13/2015 08:09 PM, Chris Angelico wrote:
>>>> On Sat, Mar 14, 2015 at 1:33 PM, Paul Rubin <no.email at nospam.invalid>
>>>> wrote:
>>>>> emile <emile at fenx.com> writes:
>>>>>> *** NameError: name 'val' is not defined
>>>>>> (Pdb) l
>>>>>> 139         try:
>>>>>> 140             val = round(float(decval),1)
>>>>>> 141         except:
>>>>>> 142             import pdb; pdb.set_trace()
>>>>>
>>>>> If 'float' or 'round' throw an exception, the assignment to 'val'
>>>>> never
>>>>> happens, so 'val' is undefined.  Try examining the value of 'decval'
>>>>> in the debugger to see what is making the conversion fail.
>>>>
>>>> That's exactly what the OP did :) Trouble is, it didn't help, because
>>>> it sure looked as if decval was the string '4'. My best guess was -
>>>> and is - that it's not just a string. We're looking at an SQL
>>>> interface routine here, so it may be that there's a string subclass
>>>> that length-limits itself, on the assumption that it's going into a
>>>> fixed-length database field.
>>>
>>> It ran almost to completion before generating the error again --
>>>
>>> (Pdb) decval
>>> '4'
>>> (Pdb) type(decval)
>>> <type 'str'>
>>> (Pdb) len(decval)
>>> 1
>>> (Pdb) int(decval)
>>> *** ValueError: invalid literal for int() with base 10:
>>> '41.700000000000003'
>>>
>>> So there's still something amiss.
>>
>> Why are you checking
>>
>> int(decval)
> 
> 
> because it sure smells like int should work:
> 
> (Pdb) "3"<decval<"5"
> True

That's a normal string comparison when decval is a string. This and the 
ValueError is expected Python behaviour:

Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> decval = "41.700000000000003"
>>> "3" < decval < "5"
True
>>> int(decval)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '41.700000000000003'


>> when the actual failing expression is
>>
>> round(float(decval),1)
>>
>> ? Try to evaluate the latter in the debugger, and if that gives no clue
>> change
>>
>>>>>> 139         try:
>>>>>> 140             val = round(float(decval),1)
>>>>>> 141         except:
>>>>>> 142             import pdb; pdb.set_trace()
>>
>> to just
>>
>> val = round(float(decval), 1)
>>
>> to get a meaningful traceback and post that.
> 
> I don't get a traceback -- it spews:
> 
> Fatal Python error: deletion of interned string failed
> 
> This application has requested the Runtime to terminate it in an unusual
> way.
> Please contact the application's support team for more information.
> 
> then crashes and I get a Microsoft pop-up that says python.exe has
> encountered a problem and needs to close.

That does look bad. Most likely an extension written in C corrupts the 
interpreter or it's even a bug in the interpreter itself.




More information about the Python-list mailing list