Skip to content

Commit f856bf8

Browse files
committed
support warnings in float tests
1 parent 8f9b4d2 commit f856bf8

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
import com.oracle.graal.python.builtins.CoreFunctions;
107107
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
108108
import com.oracle.graal.python.builtins.PythonBuiltins;
109+
import com.oracle.graal.python.builtins.modules.WarningsModuleBuiltins.WarnNode;
109110
import com.oracle.graal.python.builtins.modules.WeakRefModuleBuiltins.GetWeakRefsNode;
110111
import com.oracle.graal.python.builtins.objects.PEllipsis;
111112
import com.oracle.graal.python.builtins.objects.PNone;
@@ -966,6 +967,7 @@ static boolean isHandledType(PythonObjectLibrary lib, Object o) {
966967
@Specialization(guards = {"isPrimitiveFloat(cls)", "!isHandledType(objectLib, obj)"}, limit = "1")
967968
double doubleFromObject(VirtualFrame frame, @SuppressWarnings("unused") Object cls, Object obj,
968969
@CachedLibrary("obj") PythonObjectLibrary objectLib,
970+
@Cached WarnNode warnNode,
969971
@CachedLibrary(limit = "1") PythonObjectLibrary methodLib,
970972
@CachedLibrary(limit = "1") PythonObjectLibrary indexLib) {
971973
// Follows logic from PyNumber_Float:
@@ -981,7 +983,9 @@ static boolean isHandledType(PythonObjectLibrary lib, Object o) {
981983
}
982984
if (PGuards.isPFloat(result)) {
983985
if (!isPrimitiveProfile.profileObject(result, PythonBuiltinClassType.PFloat)) {
984-
// TODO deprecation warning
986+
warnNode.warnFormat(frame, null, PythonBuiltinClassType.DeprecationWarning, 1,
987+
ErrorMessages.P_RETURNED_NON_P,
988+
obj, "__float__", "float", result, "float");
985989
}
986990
return ((PFloat) result).getValue();
987991
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/ErrorMessages.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,4 +619,8 @@ public abstract class ErrorMessages {
619619
public static final String EXPECTED_BYTES_P_FOUND = "expected bytes, %p found";
620620
public static final String EMBEDDED_NULL_BYTE = "embedded null byte";
621621
public static final String CANNOT_INTERN_P = "can't intern %p";
622+
623+
public static final String P_RETURNED_NON_P = "%p.%s returned non-%s (type %p). " +
624+
"The ability to return an instance of a strict subclass of %s " +
625+
"is deprecated, and may be removed in a future version of Python.";
622626
}

graalpython/lib-python/3/test/test_float.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,10 @@ class FooStr(str):
198198
def __float__(self):
199199
return float(str(self)) + 1
200200

201-
# Assertions that check DeprecationWarnings have been temporarily disabled since
202-
# graalvm does not support warnings yet.
203-
204201
self.assertEqual(float(Foo1()), 42.)
205202
self.assertEqual(float(Foo2()), 42.)
206-
# with self.assertWarns(DeprecationWarning):
207-
self.assertEqual(float(Foo3(21)), 42.)
203+
with self.assertWarns(DeprecationWarning):
204+
self.assertEqual(float(Foo3(21)), 42.)
208205
self.assertRaises(TypeError, float, Foo4(42))
209206
self.assertEqual(float(FooStr('8')), 9.)
210207

@@ -217,14 +214,14 @@ def __float__(self):
217214
class F:
218215
def __float__(self):
219216
return OtherFloatSubclass(42.)
220-
# with self.assertWarns(DeprecationWarning):
221-
self.assertEqual(float(F()), 42.)
222-
# with self.assertWarns(DeprecationWarning):
223-
self.assertIs(type(float(F())), float)
224-
# with self.assertWarns(DeprecationWarning):
225-
self.assertEqual(FloatSubclass(F()), 42.)
226-
# with self.assertWarns(DeprecationWarning):
227-
self.assertIs(type(FloatSubclass(F())), FloatSubclass)
217+
with self.assertWarns(DeprecationWarning):
218+
self.assertEqual(float(F()), 42.)
219+
with self.assertWarns(DeprecationWarning):
220+
self.assertIs(type(float(F())), float)
221+
with self.assertWarns(DeprecationWarning):
222+
self.assertEqual(FloatSubclass(F()), 42.)
223+
with self.assertWarns(DeprecationWarning):
224+
self.assertIs(type(FloatSubclass(F())), FloatSubclass)
228225

229226
class MyIndex:
230227
def __init__(self, value):

0 commit comments

Comments
 (0)