Skip to content

Allow passing negative integers as amt to read methods #3356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 28, 2024

Conversation

illia-v
Copy link
Member

@illia-v illia-v commented Feb 27, 2024

Fixes #3122.

According to https://docs.python.org/3/library/io.html#io.BufferedIOBase.read, if the [size] argument is omitted, None, or negative, data is read and returned until EOF is reached.

urllib3 (as well as httplib) breaks when a negative number is passed to its read methods. This PR fixes the issue by making urllib3 treat negative numbers the same as None.

@illia-v
Copy link
Member Author

illia-v commented Feb 27, 2024

FYI, Windows 3.13 tests started to fail because of python/cpython#115582

Copy link
Member

@sethmlarson sethmlarson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great Illia, thank you! I had one comment then we're good to go:

@@ -1272,7 +1285,7 @@ def test_mock_transfer_encoding_chunked_unlmtd_read(self) -> None:
resp = HTTPResponse(
r, preload_content=False, headers={"transfer-encoding": "chunked"}
)
assert stream == list(resp.read_chunked())
assert stream == list(resp.read_chunked(amt))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're losing a testcase here, can we test not passing a parameter using *read_chunked_args with [(), (None,), and (-1,)] as our parametrized cases?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea, done

@illia-v illia-v requested a review from sethmlarson February 28, 2024 15:34
Copy link
Member

@sethmlarson sethmlarson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

request(..., preload_content=False).read(-1) raises an unexpected Exception
2 participants