Title: Implicit Decimal conversion to float fails
Type: behaviour Severity: normal
Components: Core Versions: Jython 2.7
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: alex.gronholm, fwierzbicki, leosoto, mulleteer, pjenvey, zyasoft
Priority: Keywords:

Created on 2010-10-26.14:47:16 by mulleteer, last changed 2015-04-13.21:41:19 by zyasoft.

msg6210 (view) Author: Teemu (mulleteer) Date: 2010-10-26.14:47:15
Implicit Decimal conversion fails 

Jython 2.5.2rc2 (Release_2_5_2rc2:7167, Oct 24 2010, 22:48:30) 
[Java HotSpot(TM) 64-Bit Server VM (Sun Microsystems Inc.)] on java1.6.0_21
Type "help", "copyright", "credits" or "license" for more information.
>>> import math
>>> from decimal import Decimal as D
>>> math.fabs(D('23.23'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: fabs(): 1st arg can't be coerced to double

But this works.

>>> math.fabs(float(D('23.23')))
msg6300 (view) Author: Philip Jenvey (pjenvey) Date: 2010-12-24.01:07:47
The problem here is Decimal's __tojava__ method does not handle the case of __tojava__(Double.class). We could fix that in Decimal but what would be better (in this respect) is having Decimal.__tojava__ fallback to super.__tojava__ when it doesn't know how to handle the conversion.

This would fix the problem because PyObject.__tojava__ will try conversion to Double.class via __float__.

We would have to expose __tojava__ on the object class for that fix though (it currently is not exposed to Python code). I'm not sure that's so good either
msg8689 (view) Author: Jim Baker (zyasoft) Date: 2014-06-19.00:38:15
Philip, I think it would be better if Decimal handles this explicitly rather than expose __tojava__, which seems likely to introduce other issues.
msg8690 (view) Author: Jim Baker (zyasoft) Date: 2014-06-19.00:38:27
Target beta 4
msg9808 (view) Author: Alex Grönholm (alex.gronholm) Date: 2015-04-13.17:58:43
I ended up doing it all in __tojava__. I hope this doesn't inconvenience anyone.
msg9814 (view) Author: Jim Baker (zyasoft) Date: 2015-04-13.21:41:19
Alex's approach is good, and is modeled after the similar treatment in datetime classes.

There's the somewhat related issue that 

>>> import math
>>> math.fabs
<java function fabs 0x2>

instead of CPython 2.7

>>> import math
>>> math.fabs
<built-in function fabs>

is what the math function is, which is what caused us to go with __tojava__ to begin with, which confused my thinking about this bug, but that's a larger problem.

Fixed as of
Date User Action Args
2015-04-13 21:41:19zyasoftsetmessages: + msg9814
2015-04-13 17:58:43alex.gronholmsetstatus: open -> closed
resolution: remind -> fixed
messages: + msg9808
nosy: + alex.gronholm
2014-06-19 00:38:27zyasoftsetmessages: + msg8690
versions: + Jython 2.7, - Jython 2.5
2014-06-19 00:38:15zyasoftsetnosy: + zyasoft
messages: + msg8689
2014-05-22 00:27:38zyasoftsetresolution: remind
2013-02-19 18:03:06fwierzbickisetnosy: + fwierzbicki
versions: + Jython 2.5, - 2.5.2b1
2010-12-24 01:07:48pjenveysetnosy: + pjenvey, leosoto
messages: + msg6300
2010-10-26 14:47:16mulleteercreate