From c4da788151bbe12de58e9daf6777471ca24c5dbd Mon Sep 17 00:00:00 2001 From: GLGDLY Date: Sat, 21 Sep 2024 15:05:30 +0800 Subject: [PATCH] new general sol for finding _seekable in IOBasePayload --- aiohttp/client_reqrep.py | 5 +---- aiohttp/payload.py | 2 +- tests/test_formdata.py | 6 +++--- tests/test_multipart.py | 4 +--- tests/test_payload.py | 16 ++++++++-------- 5 files changed, 14 insertions(+), 19 deletions(-) diff --git a/aiohttp/client_reqrep.py b/aiohttp/client_reqrep.py index f8a5346dee0..68713ab2375 100644 --- a/aiohttp/client_reqrep.py +++ b/aiohttp/client_reqrep.py @@ -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 diff --git a/aiohttp/payload.py b/aiohttp/payload.py index b68b7573398..395c44f6ea4 100644 --- a/aiohttp/payload.py +++ b/aiohttp/payload.py @@ -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: diff --git a/tests/test_formdata.py b/tests/test_formdata.py index e4a10911834..2cf1f3177d1 100644 --- a/tests/test_formdata.py +++ b/tests/test_formdata.py @@ -1,7 +1,7 @@ import io -import pathlib import tarfile import tempfile +from pathlib import Path from typing import NoReturn from unittest import mock @@ -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) @@ -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) diff --git a/tests/test_multipart.py b/tests/test_multipart.py index 756f8cffc91..74a715369e7 100644 --- a/tests/test_multipart.py +++ b/tests/test_multipart.py @@ -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) diff --git a/tests/test_payload.py b/tests/test_payload.py index 4ee2e3b7398..c04533a817b 100644 --- a/tests/test_payload.py +++ b/tests/test_payload.py @@ -1,6 +1,6 @@ import array import io -import pathlib +from pathlib import Path from typing import Any, AsyncIterator, Iterator from unittest import mock @@ -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) @@ -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) @@ -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) @@ -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: @@ -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) @@ -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: @@ -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)