From ed36ac87bf081ced989a567f1524070872fe9ca4 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Wed, 16 Jul 2025 17:14:53 +0900 Subject: [PATCH 1/7] deprecate more payload_* functions --- vm/src/object/core.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/vm/src/object/core.rs b/vm/src/object/core.rs index 1e95c69ea2..54a6657a9f 100644 --- a/vm/src/object/core.rs +++ b/vm/src/object/core.rs @@ -633,6 +633,7 @@ impl PyObject { self.weak_ref_list().map(|wrl| wrl.get_weak_references()) } + #[deprecated(note = "use downcastable instead")] #[inline(always)] pub fn payload_is(&self) -> bool { self.0.typeid == T::payload_type_id() @@ -642,6 +643,7 @@ impl PyObject { /// /// # Safety /// The actual payload type must be T. + #[deprecated(note = "use downcast_unchecked_ref instead")] #[inline(always)] pub const unsafe fn payload_unchecked(&self) -> &T { // we cast to a PyInner first because we don't know T's exact offset because of @@ -653,7 +655,9 @@ impl PyObject { #[deprecated(note = "use downcast_ref instead")] #[inline(always)] pub fn payload(&self) -> Option<&T> { + #[allow(deprecated)] if self.payload_is::() { + #[allow(deprecated)] Some(unsafe { self.payload_unchecked() }) } else { None @@ -719,7 +723,7 @@ impl PyObject { /// Check if this object can be downcast to T. #[inline(always)] pub fn downcastable(&self) -> bool { - self.payload_is::() + self.0.typeid == T::payload_type_id() } /// Attempt to downcast this reference to a subclass. @@ -899,9 +903,9 @@ impl Py { }) } + #[inline] pub fn payload(&self) -> &T { - // SAFETY: we know the payload is T because of the type parameter - unsafe { self.as_object().payload_unchecked() } + &self.0.payload } } From 9019b6f38baeff6ee8130dee9b8277e23016907b Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Tue, 15 Jul 2025 23:29:15 +0900 Subject: [PATCH 2/7] loose trait bount for PyInterned --- vm/src/intern.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/vm/src/intern.rs b/vm/src/intern.rs index 8463e3a1c1..a5b2a798d5 100644 --- a/vm/src/intern.rs +++ b/vm/src/intern.rs @@ -128,10 +128,7 @@ impl CachedPyStrRef { } } -pub struct PyInterned -where - T: PyPayload, -{ +pub struct PyInterned { inner: Py, } @@ -173,14 +170,14 @@ impl std::hash::Hash for PyInterned { } } -impl AsRef> for PyInterned { +impl AsRef> for PyInterned { #[inline(always)] fn as_ref(&self) -> &Py { &self.inner } } -impl Deref for PyInterned { +impl Deref for PyInterned { type Target = Py; #[inline(always)] fn deref(&self) -> &Self::Target { @@ -197,7 +194,7 @@ impl PartialEq for PyInterned { impl Eq for PyInterned {} -impl std::fmt::Debug for PyInterned { +impl std::fmt::Debug for PyInterned { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { std::fmt::Debug::fmt(&**self, f)?; write!(f, "@{:p}", self.as_ptr()) From 95bf98e159174befd2c36623ec19d03c85d91a2f Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Tue, 29 Jul 2025 00:59:32 +0900 Subject: [PATCH 3/7] Fix levenstein --- common/src/str.rs | 4 ++-- vm/src/suggestion.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/common/src/str.rs b/common/src/str.rs index af30ed6dec..10d0296619 100644 --- a/common/src/str.rs +++ b/common/src/str.rs @@ -487,7 +487,7 @@ pub mod levenshtein { if a == b { CASE_COST } else { MOVE_COST } } - pub fn levenshtein_distance(a: &str, b: &str, max_cost: usize) -> usize { + pub fn levenshtein_distance(a: &[u8], b: &[u8], max_cost: usize) -> usize { thread_local! { #[allow(clippy::declare_interior_mutable_const)] static BUFFER: RefCell<[usize; MAX_STRING_SIZE]> = const { @@ -499,7 +499,7 @@ pub mod levenshtein { return 0; } - let (mut a_bytes, mut b_bytes) = (a.as_bytes(), b.as_bytes()); + let (mut a_bytes, mut b_bytes) = (a, b); let (mut a_begin, mut a_end) = (0usize, a.len()); let (mut b_begin, mut b_end) = (0usize, b.len()); diff --git a/vm/src/suggestion.rs b/vm/src/suggestion.rs index 32d4b623b4..2cc935873c 100644 --- a/vm/src/suggestion.rs +++ b/vm/src/suggestion.rs @@ -26,7 +26,7 @@ pub fn calculate_suggestions<'a>( for item in dir_iter { let item_name = item.downcast_ref::()?; - if name.as_str() == item_name.as_str() { + if name.as_bytes() == item_name.as_bytes() { continue; } // No more than 1/3 of the characters should need changed @@ -35,7 +35,7 @@ pub fn calculate_suggestions<'a>( suggestion_distance - 1, ); let current_distance = - levenshtein_distance(name.as_str(), item_name.as_str(), max_distance); + levenshtein_distance(name.as_bytes(), item_name.as_bytes(), max_distance); if current_distance > max_distance { continue; } From a581614b8b1457280bfe1e2a1f445c46bfa4d5d2 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Tue, 29 Jul 2025 00:59:56 +0900 Subject: [PATCH 4/7] Fix genericalias --- vm/src/builtins/genericalias.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/vm/src/builtins/genericalias.rs b/vm/src/builtins/genericalias.rs index 00bd65583d..667a3c1d39 100644 --- a/vm/src/builtins/genericalias.rs +++ b/vm/src/builtins/genericalias.rs @@ -122,16 +122,16 @@ impl PyGenericAlias { .get_attribute_opt(obj.clone(), identifier!(vm, __args__))? .is_some() { - return Ok(obj.repr(vm)?.as_str().to_string()); + return Ok(obj.repr(vm)?.to_string()); } match ( vm.get_attribute_opt(obj.clone(), identifier!(vm, __qualname__))? - .and_then(|o| o.downcast_ref::().map(|n| n.as_str().to_string())), + .and_then(|o| o.downcast_ref::().map(|n| n.to_string())), vm.get_attribute_opt(obj.clone(), identifier!(vm, __module__))? - .and_then(|o| o.downcast_ref::().map(|m| m.as_str().to_string())), + .and_then(|o| o.downcast_ref::().map(|m| m.to_string())), ) { - (None, _) | (_, None) => Ok(obj.repr(vm)?.as_str().to_string()), + (None, _) | (_, None) => Ok(obj.repr(vm)?.to_string()), (Some(qualname), Some(module)) => Ok(if module == "builtins" { qualname } else { From 9c649eb9c2672a0d4509b6f000f1f7e7367fbefa Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Tue, 29 Jul 2025 00:47:25 +0900 Subject: [PATCH 5/7] Fix PyBoundMethod::repr --- vm/src/builtins/function.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/vm/src/builtins/function.rs b/vm/src/builtins/function.rs index 06a91ff36c..c93b50010e 100644 --- a/vm/src/builtins/function.rs +++ b/vm/src/builtins/function.rs @@ -859,10 +859,13 @@ impl Representable for PyBoundMethod { vm.get_attribute_opt(zelf.function.clone(), "__name__")? }; let func_name: Option = func_name.and_then(|o| o.downcast().ok()); + let formatted_func_name = match func_name { + Some(name) => name.to_string(), + None => "?".to_string(), + }; + let object_repr = zelf.object.repr(vm)?; Ok(format!( - "", - func_name.as_ref().map_or("?", |s| s.as_str()), - &zelf.object.repr(vm)?.as_str(), + "", )) } } From fc2b97df25fad2bf207247441a5d1e7a883430ea Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Tue, 29 Jul 2025 01:06:45 +0900 Subject: [PATCH 6/7] fix repr --- vm/src/builtins/union.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/vm/src/builtins/union.rs b/vm/src/builtins/union.rs index 962f3b5eb2..16d2b7831c 100644 --- a/vm/src/builtins/union.rs +++ b/vm/src/builtins/union.rs @@ -59,16 +59,16 @@ impl PyUnion { .get_attribute_opt(obj.clone(), identifier!(vm, __args__))? .is_some() { - return Ok(obj.repr(vm)?.as_str().to_string()); + return Ok(obj.repr(vm)?.to_string()); } match ( vm.get_attribute_opt(obj.clone(), identifier!(vm, __qualname__))? - .and_then(|o| o.downcast_ref::().map(|n| n.as_str().to_string())), + .and_then(|o| o.downcast_ref::().map(|n| n.to_string())), vm.get_attribute_opt(obj.clone(), identifier!(vm, __module__))? - .and_then(|o| o.downcast_ref::().map(|m| m.as_str().to_string())), + .and_then(|o| o.downcast_ref::().map(|m| m.to_string())), ) { - (None, _) | (_, None) => Ok(obj.repr(vm)?.as_str().to_string()), + (None, _) | (_, None) => Ok(obj.repr(vm)?.to_string()), (Some(qualname), Some(module)) => Ok(if module == "builtins" { qualname } else { From 34b956266b9076e063f03b0289466830a379e22f Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Tue, 29 Jul 2025 01:13:50 +0900 Subject: [PATCH 7/7] Fix fromhex --- vm/src/builtins/bytearray.rs | 2 +- vm/src/builtins/bytes.rs | 2 +- vm/src/bytes_inner.rs | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/vm/src/builtins/bytearray.rs b/vm/src/builtins/bytearray.rs index ce48b2bd7c..58406e3a20 100644 --- a/vm/src/builtins/bytearray.rs +++ b/vm/src/builtins/bytearray.rs @@ -328,7 +328,7 @@ impl PyByteArray { #[pyclassmethod] fn fromhex(cls: PyTypeRef, string: PyStrRef, vm: &VirtualMachine) -> PyResult { - let bytes = PyBytesInner::fromhex(string.as_str(), vm)?; + let bytes = PyBytesInner::fromhex(string.as_bytes(), vm)?; let bytes = vm.ctx.new_bytes(bytes); let args = vec![bytes.into()].into(); PyType::call(&cls, args, vm) diff --git a/vm/src/builtins/bytes.rs b/vm/src/builtins/bytes.rs index 22c93ee929..038592e543 100644 --- a/vm/src/builtins/bytes.rs +++ b/vm/src/builtins/bytes.rs @@ -264,7 +264,7 @@ impl PyBytes { #[pyclassmethod] fn fromhex(cls: PyTypeRef, string: PyStrRef, vm: &VirtualMachine) -> PyResult { - let bytes = PyBytesInner::fromhex(string.as_str(), vm)?; + let bytes = PyBytesInner::fromhex(string.as_bytes(), vm)?; let bytes = vm.ctx.new_bytes(bytes).into(); PyType::call(&cls, vec![bytes].into(), vm) } diff --git a/vm/src/bytes_inner.rs b/vm/src/bytes_inner.rs index db1e843091..58d272e1cf 100644 --- a/vm/src/bytes_inner.rs +++ b/vm/src/bytes_inner.rs @@ -459,11 +459,11 @@ impl PyBytesInner { bytes_to_hex(self.elements.as_slice(), sep, bytes_per_sep, vm) } - pub fn fromhex(string: &str, vm: &VirtualMachine) -> PyResult> { - let mut iter = string.bytes().enumerate(); - let mut bytes: Vec = Vec::with_capacity(string.len() / 2); + pub fn fromhex(bytes: &[u8], vm: &VirtualMachine) -> PyResult> { + let mut iter = bytes.iter().enumerate(); + let mut bytes: Vec = Vec::with_capacity(bytes.len() / 2); let i = loop { - let (i, b) = match iter.next() { + let (i, &b) = match iter.next() { Some(val) => val, None => { return Ok(bytes);