Skip to content

Commit caa25c5

Browse files
committed
Use PyBytesLike rather than PyBytesInner
1 parent c07d3f0 commit caa25c5

File tree

4 files changed

+18
-18
lines changed

4 files changed

+18
-18
lines changed

vm/src/builtins.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ mod decl {
1414
use rustpython_parser::parser;
1515

1616
use super::to_ascii;
17-
use crate::bytesinner::PyBytesInner;
17+
use crate::byteslike::PyBytesLike;
1818
use crate::exceptions::PyBaseExceptionRef;
1919
use crate::function::{single_or_tuple_any, Args, KwArgs, OptionalArg, PyFuncArgs};
2020
use crate::obj::objbool::{self, IntoPyBool};
@@ -554,18 +554,18 @@ mod decl {
554554
}
555555

556556
#[pyfunction]
557-
fn ord(string: Either<PyBytesInner, PyStringRef>, vm: &VirtualMachine) -> PyResult<u32> {
557+
fn ord(string: Either<PyBytesLike, PyStringRef>, vm: &VirtualMachine) -> PyResult<u32> {
558558
match string {
559-
Either::A(bytes) => {
560-
let bytes_len = bytes.elements.len();
559+
Either::A(bytes) => bytes.with_ref(|bytes| {
560+
let bytes_len = bytes.len();
561561
if bytes_len != 1 {
562562
return Err(vm.new_type_error(format!(
563563
"ord() expected a character, but string of length {} found",
564564
bytes_len
565565
)));
566566
}
567-
Ok(u32::from(bytes.elements[0]))
568-
}
567+
Ok(u32::from(bytes[0]))
568+
}),
569569
Either::B(string) => {
570570
let string = string.as_str();
571571
let string_len = string.chars().count();

vm/src/bytesinner.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl TryFromObject for PyBytesInner {
4444
k @ PyMemoryView => Ok(PyBytesInner {
4545
elements: k.try_value().unwrap()
4646
}),
47-
l @ PyList => l.get_byte_inner(vm),
47+
l @ PyList => l.to_byte_inner(vm),
4848
obj => {
4949
let iter = vm.get_method_or_type_error(obj.clone(), "__iter__", || {
5050
format!("a bytes-like object is required, not {}", obj.class())

vm/src/obj/objint.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use super::objfloat;
1212
use super::objmemory::PyMemoryView;
1313
use super::objstr::{PyString, PyStringRef};
1414
use super::objtype::{self, PyClassRef};
15-
use crate::bytesinner::PyBytesInner;
15+
use crate::byteslike::PyBytesLike;
1616
use crate::format::FormatSpec;
1717
use crate::function::{OptionalArg, PyFuncArgs};
1818
use crate::pyobject::{
@@ -554,14 +554,14 @@ impl PyInt {
554554
};
555555

556556
let x = match args.byteorder.as_str() {
557-
"big" => match signed {
558-
true => BigInt::from_signed_bytes_be(&args.bytes.elements),
559-
false => BigInt::from_bytes_be(Sign::Plus, &args.bytes.elements),
560-
},
561-
"little" => match signed {
562-
true => BigInt::from_signed_bytes_le(&args.bytes.elements),
563-
false => BigInt::from_bytes_le(Sign::Plus, &args.bytes.elements),
564-
},
557+
"big" => args.bytes.with_ref(|bytes| match signed {
558+
true => BigInt::from_signed_bytes_be(bytes),
559+
false => BigInt::from_bytes_be(Sign::Plus, bytes),
560+
}),
561+
"little" => args.bytes.with_ref(|bytes| match signed {
562+
true => BigInt::from_signed_bytes_le(bytes),
563+
false => BigInt::from_bytes_le(Sign::Plus, bytes),
564+
}),
565565
_ => {
566566
return Err(
567567
vm.new_value_error("byteorder must be either 'little' or 'big'".to_owned())
@@ -714,7 +714,7 @@ impl IntOptions {
714714
#[derive(FromArgs)]
715715
struct IntFromByteArgs {
716716
#[pyarg(positional_or_keyword)]
717-
bytes: PyBytesInner,
717+
bytes: PyBytesLike,
718718
#[pyarg(positional_or_keyword)]
719719
byteorder: PyStringRef,
720720
#[pyarg(keyword_only, optional = true)]

vm/src/obj/objlist.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl PyList {
6262
self.elements.write()
6363
}
6464

65-
pub(crate) fn get_byte_inner(&self, vm: &VirtualMachine) -> PyResult<bytesinner::PyBytesInner> {
65+
pub(crate) fn to_byte_inner(&self, vm: &VirtualMachine) -> PyResult<bytesinner::PyBytesInner> {
6666
let mut elements = Vec::<u8>::with_capacity(self.borrow_elements().len());
6767
for elem in self.borrow_elements().iter() {
6868
match PyIntRef::try_from_object(vm, elem.clone()) {

0 commit comments

Comments
 (0)