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
Prev Previous commit
Fix fromhex
  • Loading branch information
youknowone committed Jul 29, 2025
commit 34b956266b9076e063f03b0289466830a379e22f
2 changes: 1 addition & 1 deletion vm/src/builtins/bytearray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ impl PyByteArray {

#[pyclassmethod]
fn fromhex(cls: PyTypeRef, string: PyStrRef, vm: &VirtualMachine) -> PyResult {
let bytes = PyBytesInner::fromhex(string.as_str(), vm)?;
let bytes = PyBytesInner::fromhex(string.as_bytes(), vm)?;
let bytes = vm.ctx.new_bytes(bytes);
let args = vec![bytes.into()].into();
PyType::call(&cls, args, vm)
Expand Down
2 changes: 1 addition & 1 deletion vm/src/builtins/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ impl PyBytes {

#[pyclassmethod]
fn fromhex(cls: PyTypeRef, string: PyStrRef, vm: &VirtualMachine) -> PyResult {
let bytes = PyBytesInner::fromhex(string.as_str(), vm)?;
let bytes = PyBytesInner::fromhex(string.as_bytes(), vm)?;
let bytes = vm.ctx.new_bytes(bytes).into();
PyType::call(&cls, vec![bytes].into(), vm)
}
Expand Down
8 changes: 4 additions & 4 deletions vm/src/bytes_inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,11 +459,11 @@ impl PyBytesInner {
bytes_to_hex(self.elements.as_slice(), sep, bytes_per_sep, vm)
}

pub fn fromhex(string: &str, vm: &VirtualMachine) -> PyResult<Vec<u8>> {
let mut iter = string.bytes().enumerate();
let mut bytes: Vec<u8> = Vec::with_capacity(string.len() / 2);
pub fn fromhex(bytes: &[u8], vm: &VirtualMachine) -> PyResult<Vec<u8>> {
let mut iter = bytes.iter().enumerate();
let mut bytes: Vec<u8> = Vec::with_capacity(bytes.len() / 2);
let i = loop {
let (i, b) = match iter.next() {
let (i, &b) = match iter.next() {
Some(val) => val,
None => {
return Ok(bytes);
Expand Down
Loading