Skip to content

Commit 476b75d

Browse files
authored
Upgrade test_io from 3.13.11 and fix more io tests (#6565)
* fix various test_io * Upgrade test_io from 3.13.11 * Fix more test_io
1 parent 3600b66 commit 476b75d

File tree

3 files changed

+249
-86
lines changed

3 files changed

+249
-86
lines changed

Lib/test/test_io.py

Lines changed: 48 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1818,10 +1818,6 @@ def test_garbage_collection(self):
18181818
support.gc_collect()
18191819
self.assertIsNone(wr(), wr)
18201820

1821-
@unittest.expectedFailure # TODO: RUSTPYTHON
1822-
def test_error_through_destructor(self):
1823-
return super().test_error_through_destructor()
1824-
18251821
def test_args_error(self):
18261822
# Issue #17275
18271823
with self.assertRaisesRegex(TypeError, "BufferedReader"):
@@ -1844,12 +1840,8 @@ def test_bad_readinto_type(self):
18441840
self.assertIsInstance(cm.exception.__cause__, TypeError)
18451841

18461842
@unittest.expectedFailure # TODO: RUSTPYTHON
1847-
def test_flush_error_on_close(self):
1848-
return super().test_flush_error_on_close()
1849-
1850-
@unittest.expectedFailure # TODO: RUSTPYTHON
1851-
def test_seek_character_device_file(self):
1852-
return super().test_seek_character_device_file()
1843+
def test_error_through_destructor(self):
1844+
return super().test_error_through_destructor()
18531845

18541846
def test_truncate_on_read_only(self):
18551847
return super().test_truncate_on_read_only()
@@ -2166,10 +2158,6 @@ def test_slow_close_from_thread(self):
21662158
class CBufferedWriterTest(BufferedWriterTest, SizeofTest):
21672159
tp = io.BufferedWriter
21682160

2169-
@unittest.expectedFailure # TODO: RUSTPYTHON
2170-
def test_error_through_destructor(self):
2171-
return super().test_error_through_destructor()
2172-
21732161
def test_initialization(self):
21742162
rawio = self.MockRawIO()
21752163
bufio = self.tp(rawio)
@@ -2205,8 +2193,8 @@ def test_args_error(self):
22052193
self.tp(self.BytesIO(), 1024, 1024, 1024)
22062194

22072195
@unittest.expectedFailure # TODO: RUSTPYTHON
2208-
def test_flush_error_on_close(self):
2209-
return super().test_flush_error_on_close()
2196+
def test_error_through_destructor(self):
2197+
return super().test_error_through_destructor()
22102198

22112199
@unittest.skip('TODO: RUSTPYTHON; fallible allocation')
22122200
def test_constructor(self):
@@ -2692,10 +2680,6 @@ def test_interleaved_readline_write(self):
26922680
class CBufferedRandomTest(BufferedRandomTest, SizeofTest):
26932681
tp = io.BufferedRandom
26942682

2695-
@unittest.expectedFailure # TODO: RUSTPYTHON
2696-
def test_error_through_destructor(self):
2697-
return super().test_error_through_destructor()
2698-
26992683
@unittest.skipIf(sys.platform == 'win32', 'TODO: RUSTPYTHON; cyclic GC not supported, causes file locking')
27002684
@unittest.expectedFailure # TODO: RUSTPYTHON
27012685
def test_garbage_collection(self):
@@ -2708,16 +2692,8 @@ def test_args_error(self):
27082692
self.tp(self.BytesIO(), 1024, 1024, 1024)
27092693

27102694
@unittest.expectedFailure # TODO: RUSTPYTHON
2711-
def test_flush_error_on_close(self):
2712-
return super().test_flush_error_on_close()
2713-
2714-
@unittest.expectedFailure # TODO: RUSTPYTHON
2715-
def test_seek_character_device_file(self):
2716-
return super().test_seek_character_device_file()
2717-
2718-
@unittest.expectedFailure # TODO: RUSTPYTHON; f.read1(1) returns b'a'
2719-
def test_read1_after_write(self):
2720-
return super().test_read1_after_write()
2695+
def test_error_through_destructor(self):
2696+
return super().test_error_through_destructor()
27212697

27222698
@unittest.skip('TODO: RUSTPYTHON; fallible allocation')
27232699
def test_constructor(self):
@@ -3395,7 +3371,24 @@ def test_multibyte_seek_and_tell(self):
33953371
self.assertEqual(f.tell(), p1)
33963372
f.close()
33973373

3398-
@unittest.expectedFailure # TODO: RUSTPYTHON
3374+
def test_tell_after_readline_with_cr(self):
3375+
# Test for gh-141314: TextIOWrapper.tell() assertion failure
3376+
# when dealing with standalone carriage returns
3377+
data = b'line1\r'
3378+
with self.open(os_helper.TESTFN, "wb") as f:
3379+
f.write(data)
3380+
3381+
with self.open(os_helper.TESTFN, "r") as f:
3382+
# Read line that ends with \r
3383+
line = f.readline()
3384+
self.assertEqual(line, "line1\n")
3385+
# This should not cause an assertion failure
3386+
pos = f.tell()
3387+
# Verify we can seek back to this position
3388+
f.seek(pos)
3389+
remaining = f.read()
3390+
self.assertEqual(remaining, "")
3391+
33993392
def test_seek_with_encoder_state(self):
34003393
f = self.open(os_helper.TESTFN, "w", encoding="euc_jis_2004")
34013394
f.write("\u00e6\u0300")
@@ -4130,10 +4123,6 @@ class CTextIOWrapperTest(TextIOWrapperTest):
41304123
io = io
41314124
shutdown_error = "LookupError: unknown encoding: ascii"
41324125

4133-
@unittest.expectedFailure # TODO: RUSTPYTHON
4134-
def test_error_through_destructor(self):
4135-
return super().test_error_through_destructor()
4136-
41374126
@unittest.expectedFailure # TODO: RUSTPYTHON
41384127
def test_initialization(self):
41394128
r = self.BytesIO(b"\xc3\xa9\n\n")
@@ -4291,6 +4280,10 @@ def test_reconfigure_write_fromascii(self):
42914280
def test_reconfigure_write_through(self):
42924281
return super().test_reconfigure_write_through()
42934282

4283+
@unittest.expectedFailure # TODO: RUSTPYTHON
4284+
def test_error_through_destructor(self):
4285+
return super().test_error_through_destructor()
4286+
42944287
@unittest.expectedFailure # TODO: RUSTPYTHON
42954288
def test_repr(self):
42964289
return super().test_repr()
@@ -4306,6 +4299,11 @@ def test_recursive_repr(self):
43064299
def test_pickling_subclass(self):
43074300
return super().test_pickling_subclass()
43084301

4302+
# TODO: RUSTPYTHON; euc_jis_2004 encoding not supported
4303+
@unittest.expectedFailure
4304+
def test_seek_with_encoder_state(self):
4305+
return super().test_seek_with_encoder_state()
4306+
43094307

43104308
class PyTextIOWrapperTest(TextIOWrapperTest):
43114309
io = pyio
@@ -4319,6 +4317,11 @@ def test_constructor(self):
43194317
def test_newlines(self):
43204318
return super().test_newlines()
43214319

4320+
# TODO: RUSTPYTHON; euc_jis_2004 encoding not supported
4321+
@unittest.expectedFailure
4322+
def test_seek_with_encoder_state(self):
4323+
return super().test_seek_with_encoder_state()
4324+
43224325

43234326
class IncrementalNewlineDecoderTest(unittest.TestCase):
43244327

@@ -4836,22 +4839,6 @@ class CMiscIOTest(MiscIOTest):
48364839
name_of_module = "io", "_io"
48374840
extra_exported = "BlockingIOError",
48384841

4839-
@unittest.expectedFailure # TODO: RUSTPYTHON; BufferedWriter seeks on non-seekable pipe
4840-
def test_nonblock_pipe_write_bigbuf(self):
4841-
return super().test_nonblock_pipe_write_bigbuf()
4842-
4843-
@unittest.expectedFailure # TODO: RUSTPYTHON; BufferedWriter seeks on non-seekable pipe
4844-
def test_nonblock_pipe_write_smallbuf(self):
4845-
return super().test_nonblock_pipe_write_smallbuf()
4846-
4847-
@unittest.expectedFailure # TODO: RUSTPYTHON
4848-
def test_warn_on_dealloc(self):
4849-
return super().test_warn_on_dealloc()
4850-
4851-
@unittest.expectedFailure # TODO: RUSTPYTHON
4852-
def test_warn_on_dealloc_fd(self):
4853-
return super().test_warn_on_dealloc_fd()
4854-
48554842
def test_readinto_buffer_overflow(self):
48564843
# Issue #18025
48574844
class BadReader(self.io.BufferedIOBase):
@@ -4916,6 +4903,16 @@ def test_daemon_threads_shutdown_stderr_deadlock(self):
49164903
def test_check_encoding_errors(self):
49174904
return super().test_check_encoding_errors()
49184905

4906+
# TODO: RUSTPYTHON; ResourceWarning not triggered by _io.FileIO
4907+
@unittest.expectedFailure
4908+
def test_warn_on_dealloc(self):
4909+
return super().test_warn_on_dealloc()
4910+
4911+
# TODO: RUSTPYTHON; ResourceWarning not triggered by _io.FileIO
4912+
@unittest.expectedFailure
4913+
def test_warn_on_dealloc_fd(self):
4914+
return super().test_warn_on_dealloc_fd()
4915+
49194916

49204917
class PyMiscIOTest(MiscIOTest):
49214918
io = pyio

crates/derive-impl/src/pystructseq.rs

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -316,20 +316,53 @@ pub(crate) fn impl_pystruct_sequence_data(
316316

317317
// Generate try_from_elements trait override only when try_from_object=true
318318
let try_from_elements_trait_override = if try_from_object {
319-
let visible_field_idents: Vec<_> = visible_fields.iter().map(|f| &f.ident).collect();
320-
let skipped_field_idents: Vec<_> = skipped_fields.iter().map(|f| &f.ident).collect();
319+
let visible_field_inits: Vec<_> = visible_fields
320+
.iter()
321+
.map(|f| {
322+
let ident = &f.ident;
323+
let cfg_attrs = &f.cfg_attrs;
324+
if cfg_attrs.is_empty() {
325+
quote! { #ident: iter.next().unwrap().clone().try_into_value(vm)?, }
326+
} else {
327+
quote! {
328+
#(#cfg_attrs)*
329+
#ident: iter.next().unwrap().clone().try_into_value(vm)?,
330+
}
331+
}
332+
})
333+
.collect();
334+
let skipped_field_inits: Vec<_> = skipped_fields
335+
.iter()
336+
.map(|f| {
337+
let ident = &f.ident;
338+
let cfg_attrs = &f.cfg_attrs;
339+
if cfg_attrs.is_empty() {
340+
quote! {
341+
#ident: match iter.next() {
342+
Some(v) => v.clone().try_into_value(vm)?,
343+
None => vm.ctx.none(),
344+
},
345+
}
346+
} else {
347+
quote! {
348+
#(#cfg_attrs)*
349+
#ident: match iter.next() {
350+
Some(v) => v.clone().try_into_value(vm)?,
351+
None => vm.ctx.none(),
352+
},
353+
}
354+
}
355+
})
356+
.collect();
321357
quote! {
322358
fn try_from_elements(
323359
elements: Vec<::rustpython_vm::PyObjectRef>,
324360
vm: &::rustpython_vm::VirtualMachine,
325361
) -> ::rustpython_vm::PyResult<Self> {
326362
let mut iter = elements.into_iter();
327363
Ok(Self {
328-
#(#visible_field_idents: iter.next().unwrap().clone().try_into_value(vm)?,)*
329-
#(#skipped_field_idents: match iter.next() {
330-
Some(v) => v.clone().try_into_value(vm)?,
331-
None => vm.ctx.none(),
332-
},)*
364+
#(#visible_field_inits)*
365+
#(#skipped_field_inits)*
333366
})
334367
}
335368
}

0 commit comments

Comments
 (0)