Skip to content

Commit cce34ca

Browse files
committed
Add TextIOWrapper.truncate, fix a few bugs
1 parent 26b3445 commit cce34ca

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

Lib/test/test_io.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4209,7 +4209,6 @@ def write(self, data):
42094209
self.assertEqual([b"abcdef", b"middle", b"g"*chunk_size],
42104210
buf._write_stack)
42114211

4212-
@unittest.expectedFailure # TODO: RUSTPYTHON
42134212
def test_basic_io(self):
42144213
return super().test_basic_io()
42154214

crates/vm/src/stdlib/io.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ mod _io {
144144
};
145145
use bstr::ByteSlice;
146146
use crossbeam_utils::atomic::AtomicCell;
147-
use malachite_bigint::{BigInt, BigUint};
147+
use malachite_bigint::BigInt;
148148
use num_traits::ToPrimitive;
149149
use std::{
150150
borrow::Cow,
@@ -2244,7 +2244,7 @@ mod _io {
22442244
set_field!(self.chars_to_skip, CHARS_TO_SKIP_OFF);
22452245
set_field!(self.need_eof as u8, NEED_EOF_OFF);
22462246
set_field!(self.bytes_to_skip, BYTES_TO_SKIP_OFF);
2247-
BigUint::from_bytes_le(&buf).into()
2247+
BigInt::from_signed_bytes_le(&buf)
22482248
}
22492249

22502250
fn set_decoder_state(&self, decoder: &PyObject, vm: &VirtualMachine) -> PyResult<()> {
@@ -2728,7 +2728,7 @@ mod _io {
27282728
n_decoded += n;
27292729
cookie.bytes_to_feed += 1;
27302730
let (dec_buffer, dec_flags) = decoder_getstate()?;
2731-
if dec_buffer.is_empty() && n_decoded.chars < num_to_skip.chars {
2731+
if dec_buffer.is_empty() && n_decoded.chars <= num_to_skip.chars {
27322732
cookie.start_pos += cookie.bytes_to_feed as Offset;
27332733
num_to_skip -= n_decoded;
27342734
cookie.dec_flags = dec_flags;
@@ -2900,6 +2900,25 @@ mod _io {
29002900
flush_inner(&mut textio, vm)
29012901
}
29022902

2903+
#[pymethod]
2904+
fn truncate(
2905+
zelf: PyRef<Self>,
2906+
pos: OptionalArg<PyObjectRef>,
2907+
vm: &VirtualMachine,
2908+
) -> PyResult {
2909+
// Implementation follows _pyio.py TextIOWrapper.truncate
2910+
let mut textio = zelf.lock(vm)?;
2911+
flush_inner(&mut textio, vm)?;
2912+
let buffer = textio.buffer.clone();
2913+
drop(textio);
2914+
2915+
let pos = match pos.into_option() {
2916+
Some(p) => p,
2917+
None => vm.call_method(zelf.as_object(), "tell", ())?,
2918+
};
2919+
vm.call_method(&buffer, "truncate", (pos,))
2920+
}
2921+
29032922
#[pymethod]
29042923
fn isatty(&self, vm: &VirtualMachine) -> PyResult {
29052924
let textio = self.lock(vm)?;

0 commit comments

Comments
 (0)