Skip to content

Commit b8d3e5f

Browse files
committed
Fixed NaN handling in float() constructor to support subclasses
1 parent 859e7de commit b8d3e5f

File tree

2 files changed

+7
-18
lines changed

2 files changed

+7
-18
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_float.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,3 +879,8 @@ def test_short_repr(self):
879879
# Since Python 3.2, repr and str are identical
880880
self.assertEqual(repr(float(s)), str(float(s)))
881881
self.assertEqual(repr(float(negs)), str(float(negs)))
882+
883+
884+
class SubclassTests(unittest.TestCase):
885+
def test_subclass_nan(self):
886+
self.assertEqual(MyFloat, type(MyFloat('nan')))

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinConstructors.java

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,6 @@ public abstract static class FloatNode extends PythonBinaryBuiltinNode {
778778
@Child private LookupAndCallUnaryNode callReprNode;
779779

780780
@Child private IsBuiltinClassProfile isPrimitiveProfile = IsBuiltinClassProfile.create();
781-
private ConditionProfile isNanProfile;
782781

783782
public abstract Object executeWith(VirtualFrame frame, Object cls, Object arg);
784783

@@ -828,7 +827,7 @@ Object floatFromDouble(Object cls, double arg) {
828827
if (isPrimitiveFloat(cls)) {
829828
return arg;
830829
}
831-
return factoryCreateFloat(cls, arg);
830+
return factory().createFloat(cls, arg);
832831
}
833832

834833
@Specialization(guards = "!isNativeClass(cls)")
@@ -837,7 +836,7 @@ Object floatFromString(VirtualFrame frame, Object cls, String arg) {
837836
if (isPrimitiveFloat(cls)) {
838837
return value;
839838
}
840-
return factoryCreateFloat(cls, value);
839+
return factory().createFloat(cls, value);
841840
}
842841

843842
private double convertBytesToDouble(VirtualFrame frame, PBytesLike arg) {
@@ -980,21 +979,6 @@ private byte[] getByteArray(VirtualFrame frame, PBytesLike pByteArray) {
980979
}
981980
return toByteArrayNode.execute(frame, pByteArray);
982981
}
983-
984-
private PFloat factoryCreateFloat(Object cls, double arg) {
985-
if (isNaN(arg)) {
986-
return getCore().getNaN();
987-
}
988-
return factory().createFloat(cls, arg);
989-
}
990-
991-
private boolean isNaN(double d) {
992-
if (isNanProfile == null) {
993-
CompilerDirectives.transferToInterpreterAndInvalidate();
994-
isNanProfile = ConditionProfile.createBinaryProfile();
995-
}
996-
return isNanProfile.profile(Double.isNaN(d));
997-
}
998982
}
999983

1000984
// frozenset([iterable])

0 commit comments

Comments
 (0)