Attempt to rewind body to a certain position. Primarily used for request redirects and retries. :param body: File-like object that supports seek. :param int pos: Position to seek to in file.
(body: typing.IO[typing.AnyStr], body_pos: _TYPE_BODY_POSITION)
| 160 | |
| 161 | |
| 162 | def rewind_body(body: typing.IO[typing.AnyStr], body_pos: _TYPE_BODY_POSITION) -> None: |
| 163 | """ |
| 164 | Attempt to rewind body to a certain position. |
| 165 | Primarily used for request redirects and retries. |
| 166 | |
| 167 | :param body: |
| 168 | File-like object that supports seek. |
| 169 | |
| 170 | :param int pos: |
| 171 | Position to seek to in file. |
| 172 | """ |
| 173 | body_seek = getattr(body, "seek", None) |
| 174 | if body_seek is not None and isinstance(body_pos, int): |
| 175 | try: |
| 176 | body_seek(body_pos) |
| 177 | except OSError as e: |
| 178 | raise UnrewindableBodyError( |
| 179 | "An error occurred when rewinding request body for redirect/retry." |
| 180 | ) from e |
| 181 | elif body_pos is _FAILEDTELL: |
| 182 | raise UnrewindableBodyError( |
| 183 | "Unable to record file position for rewinding " |
| 184 | "request body during a redirect/retry." |
| 185 | ) |
| 186 | else: |
| 187 | raise ValueError( |
| 188 | f"body_pos must be of type integer, instead it was {type(body_pos)}." |
| 189 | ) |
| 190 | |
| 191 | |
| 192 | class ChunksAndContentLength(typing.NamedTuple): |