8000 Allow try_update_quantity_from_tuple to set left alignment when neede… · RustPython/RustPython@1795740 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1795740

Browse files
Allow try_update_quantity_from_tuple to set left alignment when needed (#4766)
1 parent 4783df5 commit 1795740

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

Lib/test/test_bytes.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -704,8 +704,6 @@ def test_rindex(self):
704704
self.assertEqual(b.rindex(i, 3, 9), 7)
705705
self.assertRaises(ValueError, b.rindex, w, 1, 3)
706706

707-
# TODO: RUSTPYTHON
708-
@unittest.expectedFailure
709707
def test_mod(self):
710708
b = self.type2test(b'hello, %b!')
711709
orig = b

vm/src/cformat.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,15 +218,40 @@ fn try_update_quantity_from_element(
218218
}
219219
}
220220

221+
fn try_conversion_flag_from_tuple(
222+
vm: &VirtualMachine,
223+
element: Option<&PyObjectRef>,
224+
) -> PyResult<CConversionFlags> {
225+
match element {
226+
Some(width_obj) => {
227+
if let Some(i) = width_obj.payload::<PyInt>() {
228+
let i = i.try_to_primitive::<i32>(vm)?;
229+
let flags = if i < 0 {
230+
CConversionFlags::LEFT_ADJUST
231+
} else {
232+
CConversionFlags::from_bits(0).unwrap()
233+
};
234+
Ok(flags)
235+
} else {
236+
Err(vm.new_type_error("* wants int".to_owned()))
237+
}
238+
}
239+
None => Err(vm.new_type_error("not enough arguments for format string".to_owned())),
240+
}
241+
}
242+
221243
fn try_update_quantity_from_tuple<'a, I: Iterator<Item = &'a PyObjectRef>>(
222244
vm: &VirtualMachine,
223245
elements: &mut I,
224246
q: &mut Option<CFormatQuantity>,
247+
f: &mut CConversionFlags,
225248
) -> PyResult<()> {
226249
let Some(CFormatQuantity::FromValuesTuple) = q else {
227250
return Ok(());
228251
};
229-
let quantity = try_update_quantity_from_element(vm, elements.next())?;
252+
let element = elements.next();
253+
f.insert(try_conversion_flag_from_tuple(vm, element)?);
254+
let quantity = try_update_quantity_from_element(vm, element)?;
230255
*q = Some(quantity);
231256
Ok(())
232257
}
@@ -322,7 +347,12 @@ pub(crate) fn cformat_bytes(
322347
match part {
323348
CFormatPart::Literal(literal) => result.append(literal),
324349
CFormatPart::Spec(spec) => {
325-
try_update_quantity_from_tuple(vm, &mut value_iter, &mut spec.min_field_width)?;
350+
try_update_quantity_from_tuple(
351+
vm,
352+
&mut value_iter,
353+
&mut spec.min_field_width,
354+
&mut spec.flags,
355+
)?;
326356
try_update_precision_from_tuple(vm, &mut value_iter, &mut spec.precision)?;
327357

328358
let value = match value_iter.next() {
@@ -416,7 +446,12 @@ pub(crate) fn cformat_string(
416446
match part {
417447
CFormatPart::Literal(literal) => result.push_str(literal),
418448
CFormatPart::Spec(spec) => {
419-
try_update_quantity_from_tuple(vm, &mut value_iter, &mut spec.min_field_width)?;
449+
try_update_quantity_from_tuple(
450+
vm,
451+
&mut value_iter,
452+
&mut spec.min_field_width,
453+
&mut spec.flags,
454+
)?;
420455
try_update_precision_from_tuple(vm, &mut value_iter, &mut spec.precision)?;
421456

422457
let value = match value_iter.next() {

0 commit comments

Comments
 (0)
0