Skip to content

Commit 8693adc

Browse files
committed
handle None case for bytes.translate
1 parent 0b28310 commit 8693adc

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

tests/snippets/bytes.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,5 @@
246246
assert b'hjhtuyjyujuyj'.translate(bytes.maketrans(b"hj", b"ab"), b"h") == b'btuybyubuyb'
247247
assert b'hjhtuyjyujuyj'.translate(bytes.maketrans(b"hj", b"ab"), b"a") == b'abatuybyubuyb'
248248
assert b'hjhtuyjyujuyj'.translate(bytes.maketrans(b"hj", b"ab")) == b'abatuybyubuyb'
249+
assert b'hjhtuyfjtyhuhjuyj'.translate(None, b"ht") == b'juyfjyujuyj'
250+

vm/src/obj/objbyteinner.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,13 @@ impl PyByteInner {
617617
pub fn translate(&self, table: PyObjectRef, delete: OptionalArg<PyObjectRef>, vm :&VirtualMachine) -> PyResult{
618618
let table = match try_as_bytes_like(&table) {
619619
Some(value) => value,
620-
None => {return Err(vm.new_type_error(format!("a bytes-like object is required, not {}", table)));},
620+
None => {
621+
match_class!(table,
622+
623+
_n @ PyNone => (0..=255).collect::<Vec<u8>>(),
624+
obj => {return Err(vm.new_type_error(format!("a bytes-like object is required, not {}", obj)));},
625+
)
626+
}
621627
};
622628

623629
if table.len() != 256 {

0 commit comments

Comments
 (0)