python / cpython

The Python programming language
https://www.python.org
Other
63.36k stars 30.34k forks source link

`read1` and `readline` of `http.client.HTTPResponse` do not raise `IncompleteRead` #115997

Open illia-v opened 8 months ago

illia-v commented 8 months ago

Bug report

Bug description:

Unlike http.client.HTTPResponse.read, read1 and readline do not raise IncompleteRead when the content length is know and a connection is closed before everything has been read.

FYI, these two methods have had a common issue in the past https://github.com/python/cpython/issues/113199.

CPython versions tested on:

3.8, 3.9, 3.10, 3.11, 3.12, 3.13, CPython main branch

Operating systems tested on:

Linux, macOS, Windows

Linked PRs

serhiy-storchaka commented 5 months ago

I am not sure that they should raise IncompleteRead.

AFAIK, only read() without argument raises IncompleteRead, because it needs to read the whole content. read1() and readline() without argument have different semantic, they do not read to the end of the stream. readline() reads to the first end of line, and read1() returns the amount of data depending on buffering. Raising IncompleteRead with second argument equal to self.length would be incorrect, because we cannot guarantee that read1() and readline() would return the specified number of bytes if the connection was not closed.