MCPcopy
hub / github.com/urllib3/urllib3 / _raw_read

Method _raw_read

src/urllib3/response.py:1007–1057  ·  view source on GitHub ↗

Reads `amt` of bytes from the socket.

(
        self,
        amt: int | None = None,
        *,
        read1: bool = False,
    )

Source from the content-addressed store, hash-verified

1005 return self._fp.read(amt) if amt is not None else self._fp.read()
1006
1007 def _raw_read(
1008 self,
1009 amt: int | None = None,
1010 *,
1011 read1: bool = False,
1012 ) -> bytes:
1013 """
1014 Reads `amt` of bytes from the socket.
1015 """
1016 if self._fp is None:
1017 return None # type: ignore[return-value]
1018
1019 fp_closed = getattr(self._fp, "closed", False)
1020
1021 with self._error_catcher():
1022 data = self._fp_read(amt, read1=read1) if not fp_closed else b""
1023 if amt is not None and amt != 0 and not data:
1024 # Platform-specific: Buggy versions of Python.
1025 # Close the connection when no data is returned
1026 #
1027 # This is redundant to what httplib/http.client _should_
1028 # already do. However, versions of python released before
1029 # December 15, 2012 (http://bugs.python.org/issue16298) do
1030 # not properly close the connection in all cases. There is
1031 # no harm in redundantly calling close.
1032 self._fp.close()
1033 if (
1034 self.enforce_content_length
1035 and self.length_remaining is not None
1036 and self.length_remaining != 0
1037 ):
1038 # This is an edge case that httplib failed to cover due
1039 # to concerns of backward compatibility. We're
1040 # addressing it here to make sure IncompleteRead is
1041 # raised during streaming, so all calls with incorrect
1042 # Content-Length are caught.
1043 raise IncompleteRead(self._fp_bytes_read, self.length_remaining)
1044 elif read1 and (
1045 (amt != 0 and not data) or self.length_remaining == len(data)
1046 ):
1047 # All data has been read, but `self._fp.read1` in
1048 # CPython 3.12 and older doesn't always close
1049 # `http.client.HTTPResponse`, so we close it here.
1050 # See https://github.com/python/cpython/issues/113199
1051 self._fp.close()
1052
1053 if data:
1054 self._fp_bytes_read += len(data)
1055 if self.length_remaining is not None:
1056 self.length_remaining -= len(data)
1057 return data
1058
1059 def read(
1060 self,

Callers 4

drain_connMethod · 0.95
readMethod · 0.95
read1Method · 0.95

Calls 4

_error_catcherMethod · 0.95
_fp_readMethod · 0.95
IncompleteReadClass · 0.85
closeMethod · 0.45