Skip to content

Commit 6d71b1c

Browse files
committed
Merge remote-tracking branch 'upstream/main' into clippy-lints
2 parents 83a1076 + f402dee commit 6d71b1c

22 files changed

+82
-94
lines changed

common/src/str.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ pub mod levenshtein {
487487
if a == b { CASE_COST } else { MOVE_COST }
488488
}
489489

490-
pub fn levenshtein_distance(a: &str, b: &str, max_cost: usize) -> usize {
490+
pub fn levenshtein_distance(a: &[u8], b: &[u8], max_cost: usize) -> usize {
491491
thread_local! {
492492
#[allow(clippy::declare_interior_mutable_const)]
493493
static BUFFER: RefCell<[usize; MAX_STRING_SIZE]> = const {
@@ -499,7 +499,7 @@ pub mod levenshtein {
499499
return 0;
500500
}
501501

502-
let (mut a_bytes, mut b_bytes) = (a.as_bytes(), b.as_bytes());
502+
let (mut a_bytes, mut b_bytes) = (a, b);
503503
let (mut a_begin, mut a_end) = (0usize, a.len());
504504
let (mut b_begin, mut b_end) = (0usize, b.len());
505505

vm/src/builtins/bytearray.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,9 @@ pub(crate) fn init(context: &Context) {
7373
}
7474

7575
impl PyByteArray {
76+
#[deprecated(note = "use PyByteArray::from(...).into_ref() instead")]
7677
pub fn new_ref(data: Vec<u8>, ctx: &Context) -> PyRef<Self> {
77-
PyRef::new_ref(Self::from(data), ctx.types.bytearray_type.to_owned(), None)
78+
Self::from(data).into_ref(ctx)
7879
}
7980

8081
const fn from_inner(inner: PyBytesInner) -> Self {
@@ -328,7 +329,7 @@ impl PyByteArray {
328329

329330
#[pyclassmethod]
330331
fn fromhex(cls: PyTypeRef, string: PyStrRef, vm: &VirtualMachine) -> PyResult {
331-
let bytes = PyBytesInner::fromhex(string.as_str(), vm)?;
332+
let bytes = PyBytesInner::fromhex(string.as_bytes(), vm)?;
332333
let bytes = vm.ctx.new_bytes(bytes);
333334
let args = vec![bytes.into()].into();
334335
PyType::call(&cls, args, vm)

vm/src/builtins/bytes.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,9 @@ impl Constructor for PyBytes {
9999
}
100100

101101
impl PyBytes {
102+
#[deprecated(note = "use PyBytes::from(...).into_ref() instead")]
102103
pub fn new_ref(data: Vec<u8>, ctx: &Context) -> PyRef<Self> {
103-
PyRef::new_ref(Self::from(data), ctx.types.bytes_type.to_owned(), None)
104+
Self::from(data).into_ref(ctx)
104105
}
105106

106107
fn _getitem(&self, needle: &PyObject, vm: &VirtualMachine) -> PyResult {
@@ -264,7 +265,7 @@ impl PyBytes {
264265

265266
#[pyclassmethod]
266267
fn fromhex(cls: PyTypeRef, string: PyStrRef, vm: &VirtualMachine) -> PyResult {
267-
let bytes = PyBytesInner::fromhex(string.as_str(), vm)?;
268+
let bytes = PyBytesInner::fromhex(string.as_bytes(), vm)?;
268269
let bytes = vm.ctx.new_bytes(bytes).into();
269270
PyType::call(&cls, vec![bytes].into(), vm)
270271
}

vm/src/builtins/classmethod.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,9 @@ impl Initializer for PyClassMethod {
111111
}
112112

113113
impl PyClassMethod {
114+
#[deprecated(note = "use PyClassMethod::from(...).into_ref() instead")]
114115
pub fn new_ref(callable: PyObjectRef, ctx: &Context) -> PyRef<Self> {
115-
PyRef::new_ref(
116-
Self {
117-
callable: PyMutex::new(callable),
118-
},
119-
ctx.types.classmethod_type.to_owned(),
120-
None,
121-
)
116+
Self::from(callable).into_ref(ctx)
122117
}
123118
}
124119

vm/src/builtins/complex.rs

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,6 @@ pub struct PyComplex {
2929
value: Complex64,
3030
}
3131

32-
impl PyComplex {
33-
pub const fn to_complex64(self) -> Complex64 {
34-
self.value
35-
}
36-
}
37-
3832
impl PyPayload for PyComplex {
3933
#[inline]
4034
fn class(ctx: &Context) -> &'static Py<PyType> {
@@ -234,13 +228,30 @@ impl Constructor for PyComplex {
234228
}
235229

236230
impl PyComplex {
231+
#[deprecated(note = "use PyComplex::from(...).into_ref() instead")]
237232
pub fn new_ref(value: Complex64, ctx: &Context) -> PyRef<Self> {
238-
PyRef::new_ref(Self::from(value), ctx.types.complex_type.to_owned(), None)
233+
Self::from(value).into_ref(ctx)
234+
}
235+
236+
pub const fn to_complex64(self) -> Complex64 {
237+
self.value
239238
}
240239

241240
pub const fn to_complex(&self) -> Complex64 {
242241
self.value
243242
}
243+
244+
fn number_op<F, R>(a: &PyObject, b: &PyObject, op: F, vm: &VirtualMachine) -> PyResult
245+
where
246+
F: FnOnce(Complex64, Complex64, &VirtualMachine) -> R,
247+
R: ToPyResult,
248+
{
249+
if let (Some(a), Some(b)) = (to_op_complex(a, vm)?, to_op_complex(b, vm)?) {
250+
op(a, b, vm).to_pyresult(vm)
251+
} else {
252+
Ok(vm.ctx.not_implemented())
253+
}
254+
}
244255
}
245256

246257
#[pyclass(
@@ -503,20 +514,6 @@ impl Representable for PyComplex {
503514
}
504515
}
505516

506-
impl PyComplex {
507-
fn number_op<F, R>(a: &PyObject, b: &PyObject, op: F, vm: &VirtualMachine) -> PyResult
508-
where
509-
F: FnOnce(Complex64, Complex64, &VirtualMachine) -> R,
510-
R: ToPyResult,
511-
{
512-
if let (Some(a), Some(b)) = (to_op_complex(a, vm)?, to_op_complex(b, vm)?) {
513-
op(a, b, vm).to_pyresult(vm)
514-
} else {
515-
Ok(vm.ctx.not_implemented())
516-
}
517-
}
518-
}
519-
520517
#[derive(FromArgs)]
521518
pub struct ComplexArgs {
522519
#[pyarg(any, optional)]

vm/src/builtins/dict.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ impl PyPayload for PyDict {
5151
}
5252

5353
impl PyDict {
54+
#[deprecated(note = "use PyDict::default().into_ref() instead")]
5455
pub fn new_ref(ctx: &Context) -> PyRef<Self> {
55-
PyRef::new_ref(Self::default(), ctx.types.dict_type.to_owned(), None)
56+
Self::default().into_ref(ctx)
5657
}
5758

5859
/// escape hatch to access the underlying data structure directly. prefer adding a method on

vm/src/builtins/function.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -773,12 +773,9 @@ impl PyBoundMethod {
773773
Self { object, function }
774774
}
775775

776+
#[deprecated(note = "Use `Self::new(object, function).into_ref(ctx)` instead")]
776777
pub fn new_ref(object: PyObjectRef, function: PyObjectRef, ctx: &Context) -> PyRef<Self> {
777-
PyRef::new_ref(
778-
Self::new(object, function),
779-
ctx.types.bound_method_type.to_owned(),
780-
None,
781-
)
778+
Self::new(object, function).into_ref(ctx)
782779
}
783780
}
784781

@@ -859,10 +856,13 @@ impl Representable for PyBoundMethod {
859856
vm.get_attribute_opt(zelf.function.clone(), "__name__")?
860857
};
861858
let func_name: Option<PyStrRef> = func_name.and_then(|o| o.downcast().ok());
859+
let formatted_func_name = match func_name {
860+
Some(name) => name.to_string(),
861+
None => "?".to_string(),
862+
};
863+
let object_repr = zelf.object.repr(vm)?;
862864
Ok(format!(
863-
"<bound method {} of {}>",
864-
func_name.as_ref().map_or("?", |s| s.as_str()),
865-
&zelf.object.repr(vm)?.as_str(),
865+
"<bound method {formatted_func_name} of {object_repr}>",
866866
))
867867
}
868868
}

vm/src/builtins/genericalias.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,16 @@ impl PyGenericAlias {
122122
.get_attribute_opt(obj.clone(), identifier!(vm, __args__))?
123123
.is_some()
124124
{
125-
return Ok(obj.repr(vm)?.as_str().to_string());
125+
return Ok(obj.repr(vm)?.to_string());
126126
}
127127

128128
match (
129129
vm.get_attribute_opt(obj.clone(), identifier!(vm, __qualname__))?
130-
.and_then(|o| o.downcast_ref::<PyStr>().map(|n| n.as_str().to_string())),
130+
.and_then(|o| o.downcast_ref::<PyStr>().map(|n| n.to_string())),
131131
vm.get_attribute_opt(obj.clone(), identifier!(vm, __module__))?
132-
.and_then(|o| o.downcast_ref::<PyStr>().map(|m| m.as_str().to_string())),
132+
.and_then(|o| o.downcast_ref::<PyStr>().map(|m| m.to_string())),
133133
) {
134-
(None, _) | (_, None) => Ok(obj.repr(vm)?.as_str().to_string()),
134+
(None, _) | (_, None) => Ok(obj.repr(vm)?.to_string()),
135135
(Some(qualname), Some(module)) => Ok(if module == "builtins" {
136136
qualname
137137
} else {

vm/src/builtins/list.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ impl ToPyObject for Vec<PyObjectRef> {
6363
}
6464

6565
impl PyList {
66+
#[deprecated(note = "use PyList::from(...).into_ref() instead")]
6667
pub fn new_ref(elements: Vec<PyObjectRef>, ctx: &Context) -> PyRef<Self> {
67-
PyRef::new_ref(Self::from(elements), ctx.types.list_type.to_owned(), None)
68+
Self::from(elements).into_ref(ctx)
6869
}
6970

7071
pub fn borrow_vec(&self) -> PyMappedRwLockReadGuard<'_, [PyObjectRef]> {

vm/src/builtins/namespace.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,6 @@ impl PyPayload for PyNamespace {
2626

2727
impl DefaultConstructor for PyNamespace {}
2828

29-
impl PyNamespace {
30-
pub fn new_ref(ctx: &Context) -> PyRef<Self> {
31-
PyRef::new_ref(
32-
Self {},
33-
ctx.types.namespace_type.to_owned(),
34-
Some(ctx.new_dict()),
35-
)
36-
}
37-
}
38-
3929
#[pyclass(
4030
flags(BASETYPE, HAS_DICT),
4131
with(Constructor, Initializer, Comparable, Representable)

0 commit comments

Comments
 (0)