Skip to content

Commit 3ff61ce

Browse files
[PR #11301/815901f6 backport][3.12] Clean up redundant code and stale comments from PR #11290 (#11318)
**This is a backport of PR #11301 as merged into master (815901f).** Co-authored-by: J. Nick Koston <[email protected]>
1 parent 36de40a commit 3ff61ce

File tree

3 files changed

+8
-13
lines changed

3 files changed

+8
-13
lines changed

aiohttp/payload.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -559,11 +559,7 @@ def size(self) -> Optional[int]:
559559
# By storing the start position, we ensure the size calculation always
560560
# returns the correct total size for any subsequent use.
561561
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()
567563

568564
# Return the total size from the start position
569565
# This ensures Content-Length is correct even after reading

tests/test_client_functional.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5298,8 +5298,8 @@ async def test_file_upload_307_308_redirect(
52985298
) -> None:
52995299
"""Test that file uploads work correctly with 307/308 redirects.
53005300
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.
53035303
"""
53045304
received_bodies: list[bytes] = []
53055305

tests/test_payload.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,8 +1283,9 @@ def open_file() -> TextIO:
12831283
async def test_iobase_payload_size_after_reading(tmp_path: Path) -> None:
12841284
"""Test that IOBasePayload.size returns correct size after file has been read.
12851285
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.
12881289
"""
12891290
# Create a test file with known content
12901291
test_file = tmp_path / "test.txt"
@@ -1306,14 +1307,12 @@ async def test_iobase_payload_size_after_reading(tmp_path: Path) -> None:
13061307
assert len(writer.buffer) == expected_size
13071308

13081309
# 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
13111311

13121312
# Attempting to write again should write the full content
1313-
# but currently writes nothing because file is at EOF
13141313
writer2 = BufferWriter()
13151314
await p.write(writer2)
1316-
assert len(writer2.buffer) == expected_size # This also fails!
1315+
assert len(writer2.buffer) == expected_size
13171316
finally:
13181317
await asyncio.to_thread(f.close)
13191318

0 commit comments

Comments
 (0)