Skip to content
Myr

Fix `prepare_body` stream detection for `__getattr__`-based file wrappers (#7433)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

---------

authored by Kaeun Kim · committed by GitHub · parent 0b401c7 · Browse files · 2 files+18 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Nate Prewitt <nate.prewitt@gmail.com>

Diff stats

2 files+18 added−3 removed

  • tests:+15 added−0 removed
  • other:+3 added−3 removed

2 files+18 added−3 removed

modified:src/requests/models.py+3 added-3 removed
594 unmodified lines
595596 if not isinstance(body, bytes):597 body = body.encode("utf-8")598599 if isinstance(data, Iterable) and not isinstance(600 data, (str, bytes, list, tuple, Mapping)601 ):599 # data that proxies attributes to underlying objects needs hasattr600 is_iterable = isinstance(data, Iterable) or hasattr(data, "__iter__")601 if is_iterable and not isinstance(data, (str, bytes, list, tuple, Mapping)):602 try:603 length = super_len(data)604 except (TypeError, AttributeError, UnsupportedOperation):605 length = None
575 unmodified lines
modified:tests/test_requests.py+15 added
2071 unmodified lines
2072 requests.utils.rewind_body(prep)20732074 assert "Unable to rewind request body" in str(e)20752076 def test_getattr_proxy_stream_follows_redirect(self, httpbin):2077 """Ensure stream wrappers that don't implement __iter__ directly are still detected."""20782079 class AttrProxy:2080 def __init__(self):2081 self._file = io.BytesIO(b"data")20822083 def __getattr__(self, name):2084 return getattr(self._file, name)20852086 r = requests.post(2087 httpbin("redirect-to?url=/post&status_code=307"), data=AttrProxy()2088 )2089 assert r.json()["data"] == "data"20902091 def _patch_adapter_gzipped_redirect(self, session, url):2092 adapter = session.get_adapter(url=url)2093 org_build_response = adapter.build_response2094 self._patched_response = False
974 unmodified lines