Visitar URL original
General code nits (#8107) · RustPython/RustPython@a1b54bc · GitHub
Skip to content

Commit a1b54bc

Browse files
authored
General code nits (#8107)
* General nits * compiler * cformat * gc_state * fix release compile * Fix logic error * revert bad logic error * don't inline elapsed_time
1 parent 51b9b2e commit a1b54bc

14 files changed

Lines changed: 267 additions & 193 deletions

File tree

‎crates/stdlib/src/pystruct.rs‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@ pub(crate) mod _struct {
5151
s
5252
}
5353
b @ PyBytes => {
54-
let ascii_str = ascii::AsciiStr::from_ascii(&b).map_err(|_| {
55-
new_struct_error(vm, "bad char in struct format".to_owned())
56-
})?;
54+
let ascii_str = ascii::AsciiStr::from_ascii(&b)
55+
.map_err(|_| new_struct_error(vm, "bad char in struct format"))?;
5756
vm.ctx.new_str(ascii_str)
5857
}
5958
other =>
@@ -192,7 +191,7 @@ pub(crate) mod _struct {
192191
if format_spec.size == 0 {
193192
Err(new_struct_error(
194193
vm,
195-
"cannot iteratively unpack with a struct of length 0".to_owned(),
194+
"cannot iteratively unpack with a struct of length 0",
196195
))
197196
} else if !buffer.len().is_multiple_of(format_spec.size) {
198197
Err(new_struct_error(

‎crates/vm/src/anystr.rs‎

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1+
use core::ops::Range;
2+
3+
use icu_properties::props::{
4+
BinaryProperty, EnumeratedProperty, GeneralCategory, GeneralCategoryGroup,
5+
};
6+
use num_traits::{cast::ToPrimitive, sign::Signed};
7+
18
use crate::{
29
Py, PyObject, PyObjectRef, PyResult, TryFromObject, VirtualMachine,
310
builtins::{PyIntRef, PyTuple},
411
convert::TryFromBorrowedObject,
512
function::OptionalOption,
613
};
7-
use icu_properties::props::{
8-
BinaryProperty, EnumeratedProperty, GeneralCategory, GeneralCategoryGroup,
9-
};
10-
use num_traits::{cast::ToPrimitive, sign::Signed};
11-
12-
use core::ops::Range;
1314

1415
#[derive(FromArgs)]
1516
pub struct SplitArgs<T: TryFromObject> {
@@ -422,6 +423,7 @@ pub(crate) trait AnyStr {
422423
}
423424
lower = true;
424425
}
426+
425427
lower
426428
}
427429

@@ -439,6 +441,7 @@ pub(crate) trait AnyStr {
439441
}
440442
upper = true;
441443
}
444+
442445
upper
443446
}
444447

@@ -461,10 +464,12 @@ pub(crate) trait AnyStr {
461464
{
462465
return false;
463466
}
467+
464468
if !all_cased && VALID::for_char(c) {
465469
all_cased = true;
466470
}
467471
}
472+
468473
all_cased
469474
}
470475
}
@@ -484,18 +489,19 @@ where
484489
F: Fn(T) -> PyResult<bool>,
485490
M: Fn(&PyObject) -> String,
486491
{
487-
match obj.try_to_value::<T>(vm) {
488-
Ok(single) => (predicate)(single),
489-
Err(_) => {
490-
let tuple: &Py<PyTuple> = obj
491-
.try_to_value(vm)
492-
.map_err(|_| vm.new_type_error((message)(obj)))?;
493-
for obj in tuple {
494-
if single_or_tuple_any(obj, predicate, message, vm)? {
495-
return Ok(true);
496-
}
492+
if let Ok(single) = obj.try_to_value::<T>(vm) {
493+
(predicate)(single)
494+
} else {
495+
let tuple: &Py<PyTuple> = obj
496+
.try_to_value(vm)
497+
.map_err(|_| vm.new_type_error((message)(obj)))?;
498+
499+
for obj in tuple {
500+
if single_or_tuple_any(obj, predicate, message, vm)? {
501+
return Ok(true);
497502
}
498-
Ok(false)
499503
}
504+
505+
Ok(false)
500506
}
501507
}

‎crates/vm/src/buffer.rs‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ use crate::{
55
convert::ToPyObject,
66
function::{ArgBytesLike, ArgIntoBool, ArgIntoFloat},
77
};
8-
use alloc::fmt;
9-
use core::{iter::Peekable, mem};
8+
9+
use rustpython_common::wtf8::Wtf8Buf;
10+
11+
use core::{fmt, iter::Peekable, mem};
1012
use half::f16;
1113
use itertools::Itertools;
1214
use malachite_bigint::BigInt;
@@ -736,9 +738,8 @@ pub fn struct_error_type(vm: &VirtualMachine) -> &'static PyTypeRef {
736738
INSTANCE.get_or_init(|| vm.ctx.new_exception_type("struct", "error", None))
737739
}
738740

739-
pub fn new_struct_error(vm: &VirtualMachine, msg: impl Into<String>) -> PyBaseExceptionRef {
741+
pub fn new_struct_error<T: Into<Wtf8Buf>>(vm: &VirtualMachine, msg: T) -> PyBaseExceptionRef {
740742
// can't just STRUCT_ERROR.get().unwrap() cause this could be called before from buffer
741743
// machinery, independent of whether _struct was ever imported
742-
let msg: String = msg.into();
743744
vm.new_exception_msg(struct_error_type(vm).clone(), msg.into())
744745
}

‎crates/vm/src/builtins/float.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ impl ToPyObject for f64 {
8484
vm.ctx.new_float(self).into()
8585
}
8686
}
87+
8788
impl ToPyObject for f32 {
8889
fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef {
8990
vm.ctx.new_float(f64::from(self)).into()
@@ -156,8 +157,7 @@ fn inner_divmod(v1: f64, v2: f64, vm: &VirtualMachine) -> PyResult<(f64, f64)> {
156157

157158
pub(crate) fn float_pow(v1: f64, v2: f64, vm: &VirtualMachine) -> PyResult {
158159
if v1.is_zero() && v2.is_sign_negative() {
159-
let msg = "zero to a negative power";
160-
Err(vm.new_zero_division_error(msg.to_owned()))
160+
Err(vm.new_zero_division_error("zero to a negative power"))
161161
} else if v1.is_sign_negative() && (v2.floor() - v2).abs() > f64::EPSILON {
162162
let v1 = Complex64::new(v1, 0.);
163163
let v2 = Complex64::new(v2, 0.);

‎crates/vm/src/builtins/staticmethod.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ impl PyStaticMethod {
6868
callable: PyMutex::new(callable),
6969
}
7070
}
71+
7172
#[deprecated(note = "use PyStaticMethod::new(...).into_ref() instead")]
7273
pub fn new_ref(callable: PyObjectRef, ctx: &Context) -> PyRef<Self> {
7374
Self::new(callable).into_ref(ctx)

‎crates/vm/src/byte.rs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
//! byte operation APIs
2-
use crate::object::AsObject;
3-
use crate::{PyObject, PyResult, VirtualMachine};
2+
43
use num_traits::ToPrimitive;
54

5+
use crate::{AsObject, PyObject, PyResult, VirtualMachine};
6+
67
pub fn bytes_from_object(vm: &VirtualMachine, obj: &PyObject) -> PyResult<Vec<u8>> {
78
if let Ok(elements) = obj.try_bytes_like(vm, |bytes| bytes.to_vec()) {
89
return Ok(elements);

0 commit comments

Comments
 (0)