@@ -355,7 +355,9 @@ public abstract static class ComplexNode extends PythonBuiltinNode {
355
355
356
356
@ Child private IsBuiltinClassProfile isPrimitiveProfile = IsBuiltinClassProfile .create ();
357
357
@ Child private IsBuiltinClassProfile isComplexTypeProfile ;
358
+ @ Child private IsBuiltinClassProfile isResultComplexTypeProfile ;
358
359
@ Child private LookupAndCallUnaryNode callReprNode ;
360
+ @ Child private WarnNode warnNode ;
359
361
360
362
private PComplex createComplex (Object cls , double real , double imaginary ) {
361
363
if (isPrimitiveProfile .profileClass (cls , PythonBuiltinClassType .PComplex )) {
@@ -563,6 +565,22 @@ private IsBuiltinClassProfile getIsComplexTypeProfile() {
563
565
return isComplexTypeProfile ;
564
566
}
565
567
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
+
566
584
private PException raiseFirstArgError (Object x ) {
567
585
throw raise (PythonBuiltinClassType .TypeError , ErrorMessages .ARG_MUST_BE_STRING_OR_NUMBER , "complex() first" , x );
568
586
}
@@ -579,14 +597,14 @@ private PComplex getComplexNumberFromObject(VirtualFrame frame, Object object, P
579
597
if (complexCallable != PNone .NO_VALUE ) {
580
598
Object result = methodLib .callUnboundMethod (complexCallable , frame , object );
581
599
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
+ }
587
605
return (PComplex ) result ;
588
606
} else {
589
- throw raise (TypeError , ErrorMessages .COMPLEX_SHOULD_RETURN_COMPLEX );
607
+ throw raise (TypeError , ErrorMessages .COMPLEX_RETURNED_NON_COMPLEX , result );
590
608
}
591
609
}
592
610
if (object instanceof PComplex ) {
0 commit comments