Repository navigation
Closed
Description
Subject
rewind_body should raise UnrewindableBodyError not ValueError when seek missing from body.
Current message ValueError: body_pos must be of type integer, instead it was <class 'int'> is confusing.
Environment
$ uv run --with urllib3 env.py
OS macOS-15.5-arm64-arm-64bit-Mach-O
Python 3.13.1
OpenSSL 3.0.15 3 Sep 2024
urllib3 2.6.4.dev10Steps to Reproduce
$ cat repro.py
"""Reproduction: urllib3 raises confusing ValueError on retry when body has tell() but no seek().
Run with: uv run --with urllib3 repro.py
"""
import urllib3
class TellableStream:
"""A stream with tell() and read() but no seek()."""
def __init__(self, data: bytes) -> None:
self._data = data
self._pos = 0
def read(self, n: int = -1) -> bytes:
if n == -1:
chunk = self._data[self._pos :]
self._pos = len(self._data)
else:
chunk = self._data[self._pos : self._pos + n]
self._pos += len(chunk)
return chunk
def tell(self) -> int:
return self._pos
body = TellableStream(b"hello world")
pool = urllib3.HTTPConnectionPool("localhost", port=1)
pool.request(
"POST",
"/",
body=body,
retries=urllib3.Retry(total=2, connect=2),
preload_content=True,
decode_content=True,
)
$ uv run --with urllib3 repro.py
Traceback (most recent call last):
File "/Users/chromy/Desktop/repro.py", line 31, in <module>
pool.request(
~~~~~~~~~~~~^
"POST",
^^^^^^^
...<4 lines>...
decode_content=True,
^^^^^^^^^^^^^^^^^^^^
)
^
File "/Users/chromy/.cache/uv/archive-v0/vRvoZnLVVd6vwhcGJStRk/lib/python3.13/site-packages/urllib3/_request_methods.py", line 143, in request
return self.request_encode_body(
~~~~~~~~~~~~~~~~~~~~~~~~^
method, url, fields=fields, headers=headers, **urlopen_kw
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File "/Users/chromy/.cache/uv/archive-v0/vRvoZnLVVd6vwhcGJStRk/lib/python3.13/site-packages/urllib3/_request_methods.py", line 278, in request_encode_body
return self.urlopen(method, url, **extra_kw)
~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/chromy/.cache/uv/archive-v0/vRvoZnLVVd6vwhcGJStRk/lib/python3.13/site-packages/urllib3/connectionpool.py", line 871, in urlopen
return self.urlopen(
~~~~~~~~~~~~^
method,
^^^^^^^
...<13 lines>...
**response_kw,
^^^^^^^^^^^^^^
)
^
File "/Users/chromy/.cache/uv/archive-v0/vRvoZnLVVd6vwhcGJStRk/lib/python3.13/site-packages/urllib3/connectionpool.py", line 761, in urlopen
body_pos = set_file_position(body, body_pos)
File "/Users/chromy/.cache/uv/archive-v0/vRvoZnLVVd6vwhcGJStRk/lib/python3.13/site-packages/urllib3/util/request.py", line 150, in set_file_position
rewind_body(body, pos)
~~~~~~~~~~~^^^^^^^^^^^
File "/Users/chromy/.cache/uv/archive-v0/vRvoZnLVVd6vwhcGJStRk/lib/python3.13/site-packages/urllib3/util/request.py", line 187, in rewind_body
raise ValueError(
f"body_pos must be of type integer, instead it was {type(body_pos)}."
)
ValueError: body_pos must be of type integer, instead it was <class 'int'>.Expected Behavior
Should raise UnrewindableBodyError
Actual Behavior
Raises:
ValueError: body_pos must be of type integer, instead it was <class 'int'>.
Which is confusing.
Metadata
Metadata
Assignees
Labels
No labels
Activity