Skip to content

Commit

Permalink
new general sol for finding _seekable in IOBasePayload
Browse files Browse the repository at this point in the history
  • Loading branch information
GLGDLY committed Sep 21, 2024
1 parent f706927 commit c4da788
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 19 deletions.
5 changes: 1 addition & 4 deletions aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,11 @@ def process_data_to_payload(body: Any) -> Any:
if body is None:
return None

# FormData
if isinstance(body, FormData):
body = body()

try:
with contextlib.suppress(payload.LookupError):
body = payload.PAYLOAD_REGISTRY.get(body, disposition=None)
except payload.LookupError:
pass # keep for ClientRequest to handle

return body

Expand Down
2 changes: 1 addition & 1 deletion aiohttp/payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ def __init__(

try:
self._seekable = self._value.seekable()
except (AttributeError, OSError):
except AttributeError: # https://github.com/python/cpython/issues/124293
self._seekable = False

if self._seekable:
Expand Down
6 changes: 3 additions & 3 deletions tests/test_formdata.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import io
import pathlib
import tarfile
import tempfile
from pathlib import Path
from typing import NoReturn
from unittest import mock

Expand Down Expand Up @@ -107,7 +107,7 @@ async def test_formdata_boundary_param() -> None:


async def test_formdata_on_redirect(aiohttp_client: AiohttpClient) -> None:
with pathlib.Path(pathlib.Path(__file__).parent / "sample.txt").open("rb") as fobj:
with Path(__file__).with_name("sample.txt").open("rb") as fobj:
content = fobj.read()
fobj.seek(0)

Expand Down Expand Up @@ -139,7 +139,7 @@ async def handler_1(request: web.Request) -> web.Response:


async def test_formdata_on_redirect_after_recv(aiohttp_client: AiohttpClient) -> None:
with pathlib.Path(pathlib.Path(__file__).parent / "sample.txt").open("rb") as fobj:
with Path(__file__).with_name("sample.txt").open("rb") as fobj:
content = fobj.read()
fobj.seek(0)

Expand Down
4 changes: 1 addition & 3 deletions tests/test_multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -1426,9 +1426,7 @@ async def test_multiple_write_on_io_payload(
self, buf: bytearray, stream: Stream
) -> None:
with aiohttp.MultipartWriter("form-data", boundary=":") as writer:
with pathlib.Path(pathlib.Path(__file__).parent / "sample.txt").open(
"rb"
) as fobj:
with pathlib.Path(__file__).with_name("sample.txt").open("rb") as fobj:
content = fobj.read()
fobj.seek(0)

Expand Down
16 changes: 8 additions & 8 deletions tests/test_payload.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import array
import io
import pathlib
from pathlib import Path
from typing import Any, AsyncIterator, Iterator
from unittest import mock

Expand Down Expand Up @@ -102,7 +102,7 @@ def test_string_io_payload() -> None:


def test_text_io_payload() -> None:
filepath = pathlib.Path(__file__).parent / "sample.txt"
filepath = Path(__file__).with_name("sample.txt")
filesize = filepath.stat().st_size
with filepath.open("r") as f:
p = payload.TextIOPayload(f)
Expand All @@ -113,7 +113,7 @@ def test_text_io_payload() -> None:


def test_bytes_io_payload() -> None:
filepath = pathlib.Path(__file__).parent / "sample.txt"
filepath = Path(__file__).with_name("sample.txt")
filesize = filepath.stat().st_size
with filepath.open("rb") as f:
p = payload.BytesIOPayload(f)
Expand All @@ -126,7 +126,7 @@ def test_bytes_io_payload() -> None:


def test_buffered_reader_payload() -> None:
filepath = pathlib.Path(__file__).parent / "sample.txt"
filepath = Path(__file__).with_name("sample.txt")
filesize = filepath.stat().st_size
with filepath.open("rb") as f:
p = payload.BufferedReaderPayload(f)
Expand Down Expand Up @@ -179,7 +179,7 @@ async def test_string_io_payload_write() -> None:


async def test_io_base_payload_write() -> None:
filepath = pathlib.Path(__file__).parent / "sample.txt"
filepath = Path(__file__).with_name("sample.txt")
with filepath.open("rb") as f:
content = f.read()
with io.BytesIO(content) as bf:
Expand All @@ -206,7 +206,7 @@ async def test_io_base_payload_write() -> None:


async def test_text_io_payload_write() -> None:
filepath = pathlib.Path(__file__).parent / "sample.txt"
filepath = Path(__file__).with_name("sample.txt")
with filepath.open("r") as f:
content = f.read()
f.seek(0)
Expand All @@ -233,7 +233,7 @@ async def test_text_io_payload_write() -> None:


async def test_bytes_io_payload_write() -> None:
filepath = pathlib.Path(__file__).parent / "sample.txt"
filepath = Path(__file__).with_name("sample.txt")
with filepath.open("rb") as f:
content = f.read()
with io.BytesIO(content) as bf:
Expand All @@ -260,7 +260,7 @@ async def test_bytes_io_payload_write() -> None:


async def test_buffered_reader_payload_write() -> None:
filepath = pathlib.Path(__file__).parent / "sample.txt"
filepath = Path(__file__).with_name("sample.txt")
with filepath.open("rb") as f:
content = f.read()
f.seek(0)
Expand Down

0 comments on commit c4da788

Please sign in to comment.