From f2a6c09007d40f3058eefe16987749656a0c352d Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Fri, 20 Jun 2025 16:26:11 +0300 Subject: [PATCH 1/4] CLN: Loop with idx check location_tup len fields --- vm/src/exceptions.rs | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/vm/src/exceptions.rs b/vm/src/exceptions.rs index 3c822c430c..2f73ea5aee 100644 --- a/vm/src/exceptions.rs +++ b/vm/src/exceptions.rs @@ -1619,24 +1619,23 @@ pub(super) mod types { .clone() .downcast::() { - #[allow(clippy::len_zero)] - if location_tuple.len() >= 1 { - zelf.set_attr("filename", location_tuple.fast_getitem(0).clone(), vm)?; - } - if location_tuple.len() >= 2 { - zelf.set_attr("lineno", location_tuple.fast_getitem(1).clone(), vm)?; - } - if location_tuple.len() >= 3 { - zelf.set_attr("offset", location_tuple.fast_getitem(2).clone(), vm)?; - } - if location_tuple.len() >= 4 { - zelf.set_attr("text", location_tuple.fast_getitem(3).clone(), vm)?; - } - if location_tuple.len() >= 5 { - zelf.set_attr("end_lineno", location_tuple.fast_getitem(4).clone(), vm)?; - } - if location_tuple.len() >= 6 { - zelf.set_attr("end_offset", location_tuple.fast_getitem(5).clone(), vm)?; + let location_tup_len = location_tuple.len(); + for (i, &attr) in [ + "filename", + "lineno", + "offset", + "text", + "end_lineno", + "end_offset", + ] + .iter() + .enumerate() + { + if location_tup_len > i { + zelf.set_attr(attr, location_tuple.fast_getitem(i).clone(), vm)?; + } else { + break; + } } } } From 9574e14c0fd3de6e9045f27d392afe1ebf14be6b Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Fri, 20 Jun 2025 16:47:28 +0300 Subject: [PATCH 2/4] Use `.take` instead of slicing a vec --- vm/src/exceptions.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/vm/src/exceptions.rs b/vm/src/exceptions.rs index 2f73ea5aee..77829608b2 100644 --- a/vm/src/exceptions.rs +++ b/vm/src/exceptions.rs @@ -237,9 +237,10 @@ impl VirtualMachine { let colno = offset - 1 - spaces; let end_colno = end_offset - 1 - spaces; if colno >= 0 { - let caret_space = l_text.chars().collect::>()[..colno as usize] - .iter() - .map(|c| if c.is_whitespace() { *c } else { ' ' }) + let caret_space = l_text + .chars() + .take(colno as usize) + .map(|c| if c.is_whitespace() { c } else { ' ' }) .collect::(); let mut error_width = end_colno - colno; From eea33df6b1ae96a2db50ca4046dd07fe56998730 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Sat, 21 Jun 2025 00:50:09 +0300 Subject: [PATCH 3/4] Replace 255 with u8::MAX --- vm/src/bytes_inner.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/vm/src/bytes_inner.rs b/vm/src/bytes_inner.rs index 10394721e7..550c9f8a74 100644 --- a/vm/src/bytes_inner.rs +++ b/vm/src/bytes_inner.rs @@ -214,7 +214,7 @@ pub struct ByteInnerTranslateOptions { impl ByteInnerTranslateOptions { pub fn get_value(self, vm: &VirtualMachine) -> PyResult<(Vec, Vec)> { let table = self.table.map_or_else( - || Ok((0..=255).collect::>()), + || Ok((0..=u8::MAX).collect::>()), |v| { let bytes = v .try_into_value::(vm) @@ -424,8 +424,8 @@ impl PyBytesInner { let mut new: Vec = Vec::with_capacity(self.elements.len()); for w in &self.elements { match w { - 65..=90 => new.push(w.to_ascii_lowercase()), - 97..=122 => new.push(w.to_ascii_uppercase()), + b'A'..=b'Z' => new.push(w.to_ascii_lowercase()), + b'a'..=b'z' => new.push(w.to_ascii_uppercase()), x => new.push(*x), } } @@ -491,10 +491,11 @@ impl PyBytesInner { vm: &VirtualMachine, ) -> PyResult> { let (width, fillchar) = options.get_value("center", vm)?; - Ok(if self.len() as isize >= width { + let len = self.len(); + Ok(if len as isize >= width { Vec::from(&self.elements[..]) } else { - pad(&self.elements, width as usize, fillchar, self.len()) + pad(&self.elements, width as usize, fillchar, len) }) } @@ -564,7 +565,7 @@ impl PyBytesInner { } let mut res = vec![]; - for i in 0..=255 { + for i in 0..=u8::MAX { res.push(if let Some(position) = from.elements.find_byte(i) { to.elements[position] } else { @@ -929,7 +930,7 @@ impl PyBytesInner { for i in &self.elements { match i { - 65..=90 | 97..=122 => { + b'A'..=b'Z' | b'a'..=b'z' => { if spaced { res.push(i.to_ascii_uppercase()); spaced = false @@ -1011,6 +1012,7 @@ impl AnyStrWrapper<[u8]> for PyBytesInner { fn as_ref(&self) -> Option<&[u8]> { Some(&self.elements) } + fn is_empty(&self) -> bool { self.elements.is_empty() } @@ -1036,9 +1038,11 @@ impl anystr::AnyChar for u8 { fn is_lowercase(self) -> bool { self.is_ascii_lowercase() } + fn is_uppercase(self) -> bool { self.is_ascii_uppercase() } + fn bytes_len(self) -> usize { 1 } From fc68da7f6c5cae6c3960ddd952fea9b56e704002 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Sat, 21 Jun 2025 00:50:40 +0300 Subject: [PATCH 4/4] newlines between functions --- vm/src/dict_inner.rs | 1 + vm/src/readline.rs | 4 ++++ vm/src/sliceable.rs | 1 + 3 files changed, 6 insertions(+) diff --git a/vm/src/dict_inner.rs b/vm/src/dict_inner.rs index 1dd701b0b6..d24d8ddbbb 100644 --- a/vm/src/dict_inner.rs +++ b/vm/src/dict_inner.rs @@ -146,6 +146,7 @@ impl GenIndexes { mask, } } + fn next(&mut self) -> usize { let prev = self.idx; self.idx = prev diff --git a/vm/src/readline.rs b/vm/src/readline.rs index 54a77f1289..9a59e42dc8 100644 --- a/vm/src/readline.rs +++ b/vm/src/readline.rs @@ -138,15 +138,19 @@ impl Readline { pub fn new(helper: H) -> Self { Readline(readline_inner::Readline::new(helper)) } + pub fn load_history(&mut self, path: &Path) -> OtherResult<()> { self.0.load_history(path) } + pub fn save_history(&mut self, path: &Path) -> OtherResult<()> { self.0.save_history(path) } + pub fn add_history_entry(&mut self, entry: &str) -> OtherResult<()> { self.0.add_history_entry(entry) } + pub fn readline(&mut self, prompt: &str) -> ReadlineResult { self.0.readline(prompt) } diff --git a/vm/src/sliceable.rs b/vm/src/sliceable.rs index cbc25e4e18..1140abb398 100644 --- a/vm/src/sliceable.rs +++ b/vm/src/sliceable.rs @@ -331,6 +331,7 @@ impl SequenceIndexOp for BigInt { self.try_into().unwrap_or(len) } } + fn wrapped_at(&self, _len: usize) -> Option { unimplemented!("please add one once we need it") }