File tree Expand file tree Collapse file tree 3 files changed +8
-13
lines changed Expand file tree Collapse file tree 3 files changed +8
-13
lines changed Original file line number Diff line number Diff line change @@ -559,11 +559,7 @@ def size(self) -> Optional[int]:
559
559
# By storing the start position, we ensure the size calculation always
560
560
# returns the correct total size for any subsequent use.
561
561
if self ._start_position is None :
562
- try :
563
- self ._start_position = self ._value .tell ()
564
- except (OSError , AttributeError ):
565
- # Can't get position, can't determine size
566
- return None
562
+ self ._start_position = self ._value .tell ()
567
563
568
564
# Return the total size from the start position
569
565
# This ensures Content-Length is correct even after reading
Original file line number Diff line number Diff line change @@ -5298,8 +5298,8 @@ async def test_file_upload_307_308_redirect(
5298
5298
) -> None :
5299
5299
"""Test that file uploads work correctly with 307/308 redirects.
5300
5300
5301
- This demonstrates the bug where file payloads get incorrect Content-Length
5302
- on redirect because the file position isn't reset .
5301
+ This verifies that file payloads maintain correct Content-Length
5302
+ on redirect by properly handling the file position.
5303
5303
"""
5304
5304
received_bodies : list [bytes ] = []
5305
5305
Original file line number Diff line number Diff line change @@ -1283,8 +1283,9 @@ def open_file() -> TextIO:
1283
1283
async def test_iobase_payload_size_after_reading (tmp_path : Path ) -> None :
1284
1284
"""Test that IOBasePayload.size returns correct size after file has been read.
1285
1285
1286
- This demonstrates the bug where size calculation doesn't account for
1287
- the current file position, causing issues with 307/308 redirects.
1286
+ This verifies that size calculation properly accounts for the initial
1287
+ file position, which is critical for 307/308 redirects where the same
1288
+ payload instance is reused.
1288
1289
"""
1289
1290
# Create a test file with known content
1290
1291
test_file = tmp_path / "test.txt"
@@ -1306,14 +1307,12 @@ async def test_iobase_payload_size_after_reading(tmp_path: Path) -> None:
1306
1307
assert len (writer .buffer ) == expected_size
1307
1308
1308
1309
# Second size check - should still return full file size
1309
- # but currently returns 0 because file position is at EOF
1310
- assert p .size == expected_size # This assertion fails!
1310
+ assert p .size == expected_size
1311
1311
1312
1312
# Attempting to write again should write the full content
1313
- # but currently writes nothing because file is at EOF
1314
1313
writer2 = BufferWriter ()
1315
1314
await p .write (writer2 )
1316
- assert len (writer2 .buffer ) == expected_size # This also fails!
1315
+ assert len (writer2 .buffer ) == expected_size
1317
1316
finally :
1318
1317
await asyncio .to_thread (f .close )
1319
1318
You can’t perform that action at this time.
0 commit comments