Skip to content

Commit 0b9beeb

Browse files
authored
Merge pull request #2318 from qingshi163/bytes-format
Reimpl cformat to suit for bytes
2 parents ac4e5b8 + b18814a commit 0b9beeb

5 files changed

Lines changed: 687 additions & 495 deletions

File tree

Lib/test/test_bytes.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -639,8 +639,6 @@ def test_rindex(self):
639639
self.assertEqual(b.rindex(i, 3, 9), 7)
640640
self.assertRaises(ValueError, b.rindex, w, 1, 3)
641641

642-
# TODO: RUSTPYTHON
643-
@unittest.expectedFailure
644642
def test_mod(self):
645643
b = self.type2test(b'hello, %b!')
646644
orig = b
@@ -658,8 +656,6 @@ def test_mod(self):
658656
self.assertEqual(b, b'hello,\x00world!')
659657
self.assertIs(type(b), self.type2test)
660658

661-
# TODO: RUSTPYTHON
662-
@unittest.expectedFailure
663659
def test_imod(self):
664660
b = self.type2test(b'hello, %b!')
665661
orig = b

vm/src/builtins/bytearray.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ impl PyByteArray {
608608
#[pymethod(name = "__mod__")]
609609
fn modulo(&self, values: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyByteArray> {
610610
let formatted = self.borrow_value().cformat(values, vm)?;
611-
Ok(formatted.into_bytes().into())
611+
Ok(formatted.into())
612612
}
613613

614614
#[pymethod(name = "__rmod__")]

vm/src/builtins/bytes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,9 +441,9 @@ impl PyBytes {
441441
}
442442

443443
#[pymethod(name = "__mod__")]
444-
fn modulo(&self, values: PyObjectRef, vm: &VirtualMachine) -> PyResult {
444+
fn modulo(&self, values: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyBytes> {
445445
let formatted = self.inner.cformat(values, vm)?;
446-
Ok(vm.ctx.new_bytes(formatted.into_bytes()))
446+
Ok(formatted.into())
447447
}
448448

449449
#[pymethod(name = "__rmod__")]

vm/src/bytesinner.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::builtins::pystr::{self, PyStr, PyStrRef};
1111
use crate::builtins::singletons::PyNoneRef;
1212
use crate::builtins::PyTypeRef;
1313
use crate::byteslike::try_bytes_like;
14+
use crate::cformat::CFormatBytes;
1415
use crate::function::{OptionalArg, OptionalOption};
1516
use crate::pyobject::{
1617
BorrowValue, Either, IdProtocol, PyComparisonValue, PyIterable, PyObjectRef, PyRef, PyResult,
@@ -894,8 +895,10 @@ impl PyBytesInner {
894895
res
895896
}
896897

897-
pub fn cformat(&self, values: PyObjectRef, vm: &VirtualMachine) -> PyResult<String> {
898-
self.elements.py_cformat(values, vm)
898+
pub fn cformat(&self, values: PyObjectRef, vm: &VirtualMachine) -> PyResult<Vec<u8>> {
899+
CFormatBytes::parse_from_bytes(self.elements.as_slice())
900+
.map_err(|err| vm.new_value_error(err.to_string()))?
901+
.format(vm, values)
899902
}
900903

901904
pub fn repeat(&self, n: isize) -> Vec<u8> {

0 commit comments

Comments
 (0)