From 24e3a8eba6c74309c64c5c0e54302bd66a681c9e Mon Sep 17 00:00:00 2001 From: alinioan Date: Wed, 17 Jan 2024 09:37:37 +0200 Subject: [PATCH 01/13] Update complex.rs Added deprecation warning for when returning an instance of a strict subclass of complex. --- vm/src/builtins/complex.rs | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/vm/src/builtins/complex.rs b/vm/src/builtins/complex.rs index 609dcb9b6c..f32fdf4489 100644 --- a/vm/src/builtins/complex.rs +++ b/vm/src/builtins/complex.rs @@ -10,7 +10,7 @@ use crate::{ identifier, protocol::PyNumberMethods, types::{AsNumber, Comparable, Constructor, Hashable, PyComparisonOp, Representable}, - AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine, + AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine, stdlib::warnings, }; use num_complex::Complex64; use num_traits::Zero; @@ -59,14 +59,32 @@ impl PyObjectRef { } if let Some(method) = vm.get_method(self.clone(), identifier!(vm, __complex__)) { let result = method?.call((), vm)?; - // TODO: returning strict subclasses of complex in __complex__ is deprecated - return match result.payload::() { - Some(complex_obj) => Ok(Some((complex_obj.value, true))), - None => Err(vm.new_type_error(format!( - "__complex__ returned non-complex (type '{}')", - result.class().name() - ))), - }; + + let ret_class = result.class().to_owned(); + if let Some(ret) = result.downcast_ref::() { + warnings::warn( + vm.ctx.exceptions.deprecation_warning, + format!( + "__complex__ returned non-complex (type {}). \ + The ability to return an instance of a strict subclass of complex \ + is deprecated, and may be removed in a future version of Python.", + ret_class + ), + 1, + vm, + )?; + + return Ok(Some((ret.value, true))) + } else { + return match result.payload::() { + Some(complex_obj) => Ok(Some((complex_obj.value, true))), + None => Err(vm.new_type_error(format!( + "__complex__ returned non-complex (type '{}')", + result.class().name() + ))), + }; + } + } // `complex` does not have a `__complex__` by default, so subclasses might not either, // use the actual stored value in this case From bf74b6f338384a18d0f16f99b0f3291a1783a0fd Mon Sep 17 00:00:00 2001 From: alinioan Date: Wed, 17 Jan 2024 13:05:58 +0200 Subject: [PATCH 02/13] Update test_complex.py removed label from unit test script for test_constructor --- Lib/test/test_complex.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/Lib/test/test_complex.py b/Lib/test/test_complex.py index fd6e6de5fc..f4ede9838b 100644 --- a/Lib/test/test_complex.py +++ b/Lib/test/test_complex.py @@ -307,8 +307,6 @@ def test_boolcontext(self): def test_conjugate(self): self.assertClose(complex(5.3, 9.8).conjugate(), 5.3-9.8j) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_constructor(self): class NS: def __init__(self, value): self.value = value From 676a720c6bca9ccb78785ce25a0f66d2bb2aceca Mon Sep 17 00:00:00 2001 From: alinioan Date: Mon, 22 Jan 2024 16:36:43 +0200 Subject: [PATCH 03/13] fmt fix ran cargo fmt --all --- compiler/codegen/src/compile.rs | 5 ++--- vm/src/builtins/complex.rs | 8 ++++---- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/compiler/codegen/src/compile.rs b/compiler/codegen/src/compile.rs index 217c2dc02b..49de61622f 100644 --- a/compiler/codegen/src/compile.rs +++ b/compiler/codegen/src/compile.rs @@ -507,9 +507,8 @@ impl Compiler { SymbolScope::Cell => { cache = &mut info.cellvar_cache; NameOpType::Deref - } - // // TODO: is this right? - // SymbolScope::Unknown => NameOpType::Global, + } // // TODO: is this right? + // SymbolScope::Unknown => NameOpType::Global, }; if NameUsage::Load == usage && name == "__debug__" { diff --git a/vm/src/builtins/complex.rs b/vm/src/builtins/complex.rs index f32fdf4489..dc8f055e23 100644 --- a/vm/src/builtins/complex.rs +++ b/vm/src/builtins/complex.rs @@ -9,8 +9,9 @@ use crate::{ }, identifier, protocol::PyNumberMethods, + stdlib::warnings, types::{AsNumber, Comparable, Constructor, Hashable, PyComparisonOp, Representable}, - AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine, stdlib::warnings, + AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine, }; use num_complex::Complex64; use num_traits::Zero; @@ -59,7 +60,7 @@ impl PyObjectRef { } if let Some(method) = vm.get_method(self.clone(), identifier!(vm, __complex__)) { let result = method?.call((), vm)?; - + let ret_class = result.class().to_owned(); if let Some(ret) = result.downcast_ref::() { warnings::warn( @@ -74,7 +75,7 @@ impl PyObjectRef { vm, )?; - return Ok(Some((ret.value, true))) + return Ok(Some((ret.value, true))); } else { return match result.payload::() { Some(complex_obj) => Ok(Some((complex_obj.value, true))), @@ -84,7 +85,6 @@ impl PyObjectRef { ))), }; } - } // `complex` does not have a `__complex__` by default, so subclasses might not either, // use the actual stored value in this case From ed46fd6b3df71ef5a728342a0ec4a4da6da346fb Mon Sep 17 00:00:00 2001 From: Alin-Ioan Alexandru <118962201+alinioan@users.noreply.github.com> Date: Tue, 23 Jan 2024 13:44:23 +0200 Subject: [PATCH 04/13] Update vm/src/builtins/complex.rs Co-authored-by: Alexandru Radovici --- vm/src/builtins/complex.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/src/builtins/complex.rs b/vm/src/builtins/complex.rs index dc8f055e23..e39626c3aa 100644 --- a/vm/src/builtins/complex.rs +++ b/vm/src/builtins/complex.rs @@ -75,7 +75,7 @@ impl PyObjectRef { vm, )?; - return Ok(Some((ret.value, true))); + Ok(Some((ret.value, true))) } else { return match result.payload::() { Some(complex_obj) => Ok(Some((complex_obj.value, true))), From 546b20da1d3bba948426f11752c4f0b658b62a3e Mon Sep 17 00:00:00 2001 From: Alin-Ioan Alexandru <118962201+alinioan@users.noreply.github.com> Date: Tue, 23 Jan 2024 13:44:31 +0200 Subject: [PATCH 05/13] Update vm/src/builtins/complex.rs Co-authored-by: Alexandru Radovici --- vm/src/builtins/complex.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vm/src/builtins/complex.rs b/vm/src/builtins/complex.rs index e39626c3aa..d1b2825abf 100644 --- a/vm/src/builtins/complex.rs +++ b/vm/src/builtins/complex.rs @@ -77,13 +77,13 @@ impl PyObjectRef { Ok(Some((ret.value, true))) } else { - return match result.payload::() { + match result.payload::() { Some(complex_obj) => Ok(Some((complex_obj.value, true))), None => Err(vm.new_type_error(format!( "__complex__ returned non-complex (type '{}')", result.class().name() ))), - }; + } } } // `complex` does not have a `__complex__` by default, so subclasses might not either, From 3d293b262bb028c5eff39dabd24f765070e07bb7 Mon Sep 17 00:00:00 2001 From: alinioan Date: Tue, 23 Jan 2024 17:39:03 +0200 Subject: [PATCH 06/13] merge fix merge fix --- compiler/codegen/src/compile.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/codegen/src/compile.rs b/compiler/codegen/src/compile.rs index 49de61622f..e5976047a3 100644 --- a/compiler/codegen/src/compile.rs +++ b/compiler/codegen/src/compile.rs @@ -507,7 +507,7 @@ impl Compiler { SymbolScope::Cell => { cache = &mut info.cellvar_cache; NameOpType::Deref - } // // TODO: is this right? + } // TODO: is this right? // SymbolScope::Unknown => NameOpType::Global, }; From e6a0e992113fd2e53e0a10300ef2ebd053fd56a4 Mon Sep 17 00:00:00 2001 From: Alin-Ioan Alexandru <118962201+alinioan@users.noreply.github.com> Date: Tue, 23 Jan 2024 17:54:11 +0200 Subject: [PATCH 07/13] Update vm/src/builtins/complex.rs Co-authored-by: Alexandru Radovici --- vm/src/builtins/complex.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/src/builtins/complex.rs b/vm/src/builtins/complex.rs index d1b2825abf..85e9a0e564 100644 --- a/vm/src/builtins/complex.rs +++ b/vm/src/builtins/complex.rs @@ -75,7 +75,7 @@ impl PyObjectRef { vm, )?; - Ok(Some((ret.value, true))) + return Ok(Some((ret.value, true))); } else { match result.payload::() { Some(complex_obj) => Ok(Some((complex_obj.value, true))), From 3c87b75fdef5a395a53ddd58c10b8cefeceed24a Mon Sep 17 00:00:00 2001 From: Alin-Ioan Alexandru <118962201+alinioan@users.noreply.github.com> Date: Tue, 23 Jan 2024 17:54:18 +0200 Subject: [PATCH 08/13] Update vm/src/builtins/complex.rs Co-authored-by: Alexandru Radovici --- vm/src/builtins/complex.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vm/src/builtins/complex.rs b/vm/src/builtins/complex.rs index 85e9a0e564..dc8f055e23 100644 --- a/vm/src/builtins/complex.rs +++ b/vm/src/builtins/complex.rs @@ -77,13 +77,13 @@ impl PyObjectRef { return Ok(Some((ret.value, true))); } else { - match result.payload::() { + return match result.payload::() { Some(complex_obj) => Ok(Some((complex_obj.value, true))), None => Err(vm.new_type_error(format!( "__complex__ returned non-complex (type '{}')", result.class().name() ))), - } + }; } } // `complex` does not have a `__complex__` by default, so subclasses might not either, From fcf70d0044162cc86a66e8ac1e042c474ada3d30 Mon Sep 17 00:00:00 2001 From: Alin-Ioan Alexandru <118962201+alinioan@users.noreply.github.com> Date: Tue, 23 Jan 2024 18:12:38 +0200 Subject: [PATCH 09/13] Update vm/src/builtins/complex.rs Co-authored-by: Alexandru Radovici --- vm/src/builtins/complex.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/src/builtins/complex.rs b/vm/src/builtins/complex.rs index dc8f055e23..ba878e124c 100644 --- a/vm/src/builtins/complex.rs +++ b/vm/src/builtins/complex.rs @@ -77,7 +77,7 @@ impl PyObjectRef { return Ok(Some((ret.value, true))); } else { - return match result.payload::() { + match result.payload::() { Some(complex_obj) => Ok(Some((complex_obj.value, true))), None => Err(vm.new_type_error(format!( "__complex__ returned non-complex (type '{}')", From a19c9b7337fbde0358ad48a11e88a0d2edcb564e Mon Sep 17 00:00:00 2001 From: alinioan Date: Tue, 23 Jan 2024 18:17:11 +0200 Subject: [PATCH 10/13] Update complex.rs --- vm/src/builtins/complex.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vm/src/builtins/complex.rs b/vm/src/builtins/complex.rs index d1b2825abf..ee34de7ca2 100644 --- a/vm/src/builtins/complex.rs +++ b/vm/src/builtins/complex.rs @@ -75,9 +75,9 @@ impl PyObjectRef { vm, )?; - Ok(Some((ret.value, true))) + return Ok(Some((ret.value, true))) } else { - match result.payload::() { + return match result.payload::() { Some(complex_obj) => Ok(Some((complex_obj.value, true))), None => Err(vm.new_type_error(format!( "__complex__ returned non-complex (type '{}')", From 8a021c2473c453041fd6d0983d19b4ae5b875281 Mon Sep 17 00:00:00 2001 From: alinioan Date: Tue, 23 Jan 2024 18:18:16 +0200 Subject: [PATCH 11/13] Revert "Update complex.rs" This reverts commit a19c9b7337fbde0358ad48a11e88a0d2edcb564e. --- vm/src/builtins/complex.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vm/src/builtins/complex.rs b/vm/src/builtins/complex.rs index a1fe144219..a5e067eca0 100644 --- a/vm/src/builtins/complex.rs +++ b/vm/src/builtins/complex.rs @@ -75,9 +75,9 @@ impl PyObjectRef { vm, )?; - return Ok(Some((ret.value, true))) + Ok(Some((ret.value, true))) } else { - return match result.payload::() { + match result.payload::() { Some(complex_obj) => Ok(Some((complex_obj.value, true))), None => Err(vm.new_type_error(format!( "__complex__ returned non-complex (type '{}')", From 1c0493be07cafd118ca30a1aac9b98b051c2cc7a Mon Sep 17 00:00:00 2001 From: alinioan Date: Tue, 23 Jan 2024 18:18:24 +0200 Subject: [PATCH 12/13] Revert "Revert "Update complex.rs"" This reverts commit 8a021c2473c453041fd6d0983d19b4ae5b875281. --- vm/src/builtins/complex.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vm/src/builtins/complex.rs b/vm/src/builtins/complex.rs index a5e067eca0..a1fe144219 100644 --- a/vm/src/builtins/complex.rs +++ b/vm/src/builtins/complex.rs @@ -75,9 +75,9 @@ impl PyObjectRef { vm, )?; - Ok(Some((ret.value, true))) + return Ok(Some((ret.value, true))) } else { - match result.payload::() { + return match result.payload::() { Some(complex_obj) => Ok(Some((complex_obj.value, true))), None => Err(vm.new_type_error(format!( "__complex__ returned non-complex (type '{}')", From 3a274b54f8274506f117b5086b73ad9c49f16c06 Mon Sep 17 00:00:00 2001 From: Alin-Ioan Alexandru <118962201+alinioan@users.noreply.github.com> Date: Wed, 24 Jan 2024 13:24:03 +0200 Subject: [PATCH 13/13] Update vm/src/builtins/complex.rs Co-authored-by: Jeong, YunWon <69878+youknowone@users.noreply.github.com> --- vm/src/builtins/complex.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/src/builtins/complex.rs b/vm/src/builtins/complex.rs index a1fe144219..dc8f055e23 100644 --- a/vm/src/builtins/complex.rs +++ b/vm/src/builtins/complex.rs @@ -75,7 +75,7 @@ impl PyObjectRef { vm, )?; - return Ok(Some((ret.value, true))) + return Ok(Some((ret.value, true))); } else { return match result.payload::() { Some(complex_obj) => Ok(Some((complex_obj.value, true))),