Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Refactor byte operations
  • Loading branch information
killme2008 authored and youknowone committed Jun 14, 2022
commit 396812d66c60b87fee4d8aa50da419e317904ce2
6 changes: 3 additions & 3 deletions vm/src/builtins/bytearray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use super::{
};
use crate::{
anystr::{self, AnyStr},
byte::{bytes_from_object, value_from_object},
bytesinner::{
bytes_decode, bytes_from_object, value_from_object, ByteInnerFindOptions,
ByteInnerNewOptions, ByteInnerPaddingOptions, ByteInnerSplitOptions,
ByteInnerTranslateOptions, DecodeArgs, PyBytesInner,
bytes_decode, ByteInnerFindOptions, ByteInnerNewOptions, ByteInnerPaddingOptions,
ByteInnerSplitOptions, ByteInnerTranslateOptions, DecodeArgs, PyBytesInner,
},
class::PyClassImpl,
common::{
Expand Down
27 changes: 27 additions & 0 deletions vm/src/byte.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//! byte operation APIs
use crate::object::AsObject;
use crate::{PyObject, PyResult, VirtualMachine};
use num_traits::ToPrimitive;

pub fn bytes_from_object(vm: &VirtualMachine, obj: &PyObject) -> PyResult<Vec<u8>> {
if let Ok(elements) = obj.try_bytes_like(vm, |bytes| bytes.to_vec()) {
return Ok(elements);
}

if !obj.fast_isinstance(vm.ctx.types.str_type) {
if let Ok(elements) = vm.map_iterable_object(obj, |x| value_from_object(vm, &x)) {
return elements;
}
}

Err(vm.new_type_error(
"can assign only bytes, buffers, or iterables of ints in range(0, 256)".to_owned(),
))
}

pub fn value_from_object(vm: &VirtualMachine, obj: &PyObject) -> PyResult<u8> {
vm.to_index(obj)?
.as_bigint()
.to_u8()
.ok_or_else(|| vm.new_value_error("byte must be in range(0, 256)".to_owned()))
}
24 changes: 1 addition & 23 deletions vm/src/bytesinner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{
builtins::{
pystr, PyByteArray, PyBytes, PyBytesRef, PyInt, PyIntRef, PyStr, PyStrRef, PyTypeRef,
},
byte::bytes_from_object,
cformat::CFormatBytes,
function::{ArgIterable, Either, OptionalArg, OptionalOption, PyComparisonValue},
identifier,
Expand Down Expand Up @@ -1207,26 +1208,3 @@ pub fn bytes_to_hex(
pub const fn is_py_ascii_whitespace(b: u8) -> bool {
matches!(b, b'\t' | b'\n' | b'\x0C' | b'\r' | b' ' | b'\x0B')
}

pub fn bytes_from_object(vm: &VirtualMachine, obj: &PyObject) -> PyResult<Vec<u8>> {
if let Ok(elements) = obj.try_bytes_like(vm, |bytes| bytes.to_vec()) {
return Ok(elements);
}

if !obj.fast_isinstance(vm.ctx.types.str_type) {
if let Ok(elements) = vm.map_iterable_object(obj, |x| value_from_object(vm, &x)) {
return elements;
}
}

Err(vm.new_type_error(
"can assign only bytes, buffers, or iterables of ints in range(0, 256)".to_owned(),
))
}

pub fn value_from_object(vm: &VirtualMachine, obj: &PyObject) -> PyResult<u8> {
vm.to_index(obj)?
.as_bigint()
.to_u8()
.ok_or_else(|| vm.new_value_error("byte must be in range(0, 256)".to_owned()))
}
1 change: 1 addition & 0 deletions vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub(crate) mod macros;
mod anystr;
pub mod buffer;
pub mod builtins;
pub mod byte;
mod bytesinner;
pub mod cformat;
pub mod class;
Expand Down