Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit680cce9

Browse files
committed
memoryview hex
1 parent2a67207 commit680cce9

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

‎Lib/test/test_bytes.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,6 @@ def test_fromhex(self):
416416
self.type2test.fromhex(data)
417417
self.assertIn('at position %s'%pos,str(cm.exception))
418418

419-
# TODO: RUSTPYTHON
420-
@unittest.expectedFailure
421419
deftest_hex(self):
422420
self.assertRaises(TypeError,self.type2test.hex)
423421
self.assertRaises(TypeError,self.type2test.hex,1)

‎Lib/test/test_memoryview.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,8 +544,6 @@ def test_ctypes_cast(self):
544544
m[2:]=memoryview(p6).cast(format)[2:]
545545
self.assertEqual(d.value,0.6)
546546

547-
# TODO: RUSTPYTHON
548-
@unittest.expectedFailure
549547
deftest_memoryview_hex(self):
550548
# Issue #9951: memoryview.hex() segfaults with non-contiguous buffers.
551549
x=b'0'*200000

‎vm/src/obj/objbytes.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,10 @@ impl PyBytes {
222222
self.inner.swapcase().into()
223223
}
224224

225+
// TODO: Changed in version 3.8: bytes.hex() now supports optional sep and
226+
// bytes_per_sep parameters to insert separators between bytes in the hex output.
225227
#[pymethod(name ="hex")]
226-
fnhex(&self) ->String{
228+
pub(crate)fnhex(&self) ->String{
227229
self.inner.hex()
228230
}
229231

‎vm/src/obj/objmemory.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,30 @@ impl PyMemoryView {
603603
}
604604
}
605605

606+
// TODO: Changed in version 3.8: memoryview.hex() now supports optional sep and bytes_per_sep
607+
// parameters to insert separators between bytes in the hex output.
608+
#[pymethod]
609+
fnhex(zelf:PyRef<Self>,vm:&VirtualMachine) ->PyResult<String>{
610+
zelf.try_not_released(vm)?;
611+
let guard;
612+
let vec;
613+
let bytes =match zelf.as_contiguous(){
614+
Some(bytes) =>{
615+
guard = bytes;
616+
&*guard
617+
}
618+
None =>{
619+
vec = zelf.to_contiguous();
620+
vec.as_slice()
621+
}
622+
};
623+
let s = bytes
624+
.iter()
625+
.map(|x|format!("{:02x}", x))
626+
.collect::<String>();
627+
Ok(s)
628+
}
629+
606630
fneq(zelf:&PyRef<Self>,other:&PyObjectRef,vm:&VirtualMachine) ->PyResult<bool>{
607631
if zelf.is(other){
608632
returnOk(true);
@@ -686,7 +710,8 @@ impl Buffer for PyMemoryViewRef {
686710
}
687711

688712
fnis_resizable(&self) ->bool{
689-
self.buffer.is_resizable()
713+
// memoryview cannot resize
714+
false
690715
}
691716

692717
fnas_contiguous(&self) ->Option<BorrowedValue<[u8]>>{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp