Skip to content

General code clean #5808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions vm/src/bytes_inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ pub struct ByteInnerTranslateOptions {
impl ByteInnerTranslateOptions {
pub fn get_value(self, vm: &VirtualMachine) -> PyResult<(Vec<u8>, Vec<u8>)> {
let table = self.table.map_or_else(
|| Ok((0..=255).collect::<Vec<u8>>()),
|| Ok((0..=u8::MAX).collect::<Vec<u8>>()),
|v| {
let bytes = v
.try_into_value::<PyBytesInner>(vm)
Expand Down Expand Up @@ -424,8 +424,8 @@ impl PyBytesInner {
let mut new: Vec<u8> = 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),
}
}
Expand Down Expand Up @@ -491,10 +491,11 @@ impl PyBytesInner {
vm: &VirtualMachine,
) -> PyResult<Vec<u8>> {
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)
})
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
Expand All @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions vm/src/dict_inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ impl GenIndexes {
mask,
}
}

fn next(&mut self) -> usize {
let prev = self.idx;
self.idx = prev
Expand Down
42 changes: 21 additions & 21 deletions vm/src/exceptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>()[..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::<String>();

let mut error_width = end_colno - colno;
Expand Down Expand Up @@ -1619,24 +1620,23 @@ pub(super) mod types {
.clone()
.downcast::<crate::builtins::PyTuple>()
{
#[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;
}
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions vm/src/readline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,19 @@ impl<H: Helper> Readline<H> {
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)
}
Expand Down
1 change: 1 addition & 0 deletions vm/src/sliceable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ impl SequenceIndexOp for BigInt {
self.try_into().unwrap_or(len)
}
}

fn wrapped_at(&self, _len: usize) -> Option<usize> {
unimplemented!("please add one once we need it")
}
Expand Down
Loading