Skip to content

Commit c879c77

Browse files
committed
raise warning in test_complex
1 parent dd2597a commit c879c77

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

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

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,9 @@ public abstract static class ComplexNode extends PythonBuiltinNode {
355355

356356
@Child private IsBuiltinClassProfile isPrimitiveProfile = IsBuiltinClassProfile.create();
357357
@Child private IsBuiltinClassProfile isComplexTypeProfile;
358+
@Child private IsBuiltinClassProfile isResultComplexTypeProfile;
358359
@Child private LookupAndCallUnaryNode callReprNode;
360+
@Child private WarnNode warnNode;
359361

360362
private PComplex createComplex(Object cls, double real, double imaginary) {
361363
if (isPrimitiveProfile.profileClass(cls, PythonBuiltinClassType.PComplex)) {
@@ -563,6 +565,22 @@ private IsBuiltinClassProfile getIsComplexTypeProfile() {
563565
return isComplexTypeProfile;
564566
}
565567

568+
private IsBuiltinClassProfile getIsResultComplexTypeProfile() {
569+
if (isResultComplexTypeProfile == null) {
570+
CompilerDirectives.transferToInterpreterAndInvalidate();
571+
isResultComplexTypeProfile = insert(IsBuiltinClassProfile.create());
572+
}
573+
return isResultComplexTypeProfile;
574+
}
575+
576+
private WarnNode getWarnNode() {
577+
if (warnNode == null) {
578+
CompilerDirectives.transferToInterpreterAndInvalidate();
579+
warnNode = insert(WarnNode.create());
580+
}
581+
return warnNode;
582+
}
583+
566584
private PException raiseFirstArgError(Object x) {
567585
throw raise(PythonBuiltinClassType.TypeError, ErrorMessages.ARG_MUST_BE_STRING_OR_NUMBER, "complex() first", x);
568586
}
@@ -579,14 +597,14 @@ private PComplex getComplexNumberFromObject(VirtualFrame frame, Object object, P
579597
if (complexCallable != PNone.NO_VALUE) {
580598
Object result = methodLib.callUnboundMethod(complexCallable, frame, object);
581599
if (result instanceof PComplex) {
582-
// TODO we need pass here deprecation warning
583-
// DeprecationWarning: __complex__ returned non-complex (type %p).
584-
// The ability to return an instance of a strict subclass of complex is
585-
// deprecated,
586-
// and may be removed in a future version of Python.
600+
if (!getIsResultComplexTypeProfile().profileObject(result, PythonBuiltinClassType.PComplex)) {
601+
getWarnNode().warnFormat(frame, null, PythonBuiltinClassType.DeprecationWarning, 1,
602+
ErrorMessages.P_RETURNED_NON_P,
603+
object, "__complex__", "complex", result, "complex");
604+
}
587605
return (PComplex) result;
588606
} else {
589-
throw raise(TypeError, ErrorMessages.COMPLEX_SHOULD_RETURN_COMPLEX);
607+
throw raise(TypeError, ErrorMessages.COMPLEX_RETURNED_NON_COMPLEX, result);
590608
}
591609
}
592610
if (object instanceof PComplex) {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ public abstract class ErrorMessages {
182182
public static final String COMPLEX_ZERO_TO_NEGATIVE_POWER = "0.0 to a negative or complex power";
183183
public static final String COMPLEX_MODULO = "complex modulo";
184184
public static final String COMPLEX_RETURNED_NON_COMPLEX = "__complex__ returned non-complex (type %p)";
185-
public static final String COMPLEX_SHOULD_RETURN_COMPLEX = "__complex__ should return a complex object";
186185
public static final String CONTIGUOUS_BUFFER = "contiguous buffer";
187186
public static final String CONVERTER_FUNC_FAILED_TO_SET_ERROR = "converter function failed to set an error on failure";
188187
public static final String CORRUPTED_CAPI_LIB_OBJ = "corrupted C API library object: %s";

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -405,11 +405,8 @@ def __complex__(self):
405405

406406
self.assertEqual(complex(complex0(1j)), 42j)
407407

408-
# Assertions that check DeprecationWarnings have been temporarily disabled since
409-
# graalvm does not support warnings yet.
410-
411-
# with self.assertWarns(DeprecationWarning):
412-
self.assertEqual(complex(complex1(1j)), 2j)
408+
with self.assertWarns(DeprecationWarning):
409+
self.assertEqual(complex(complex1(1j)), 2j)
413410
self.assertRaises(TypeError, complex, complex2(1j))
414411

415412
@support.requires_IEEE_754

0 commit comments

Comments
 (0)