Skip to content

Instantly share code, notes, and snippets.

@chriskuehl
Created January 24, 2015 04:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chriskuehl/4da02e5fe1a500eb0bc5 to your computer and use it in GitHub Desktop.
Save chriskuehl/4da02e5fe1a500eb0bc5 to your computer and use it in GitHub Desktop.
From: Chris Kuehl <ckuehl@ocf.berkeley.edu>
Date: Fri, 23 Jan 2015 14:54:24 -0800
Subject: [PATCH] Don't raise IncompleteRead if response reads fails
This is a backport of the following commits onto the python-pip package.
https://github.com/pypa/pip/commit/53eab2357e4425436b51e109bfaedcb9f3772072
https://github.com/pypa/pip/commit/e77c0c573c059bb4a86b2c5c06835f57e4af6c47
This fixes Debian bug#744145 by allowing pip to continue to work with
upgraded versions of the requests library.
---
pip/download.py | 35 +++++++++++++++++++++++++++--------
1 file changed, 27 insertions(+), 8 deletions(-)
diff --git a/pip/download.py b/pip/download.py
index adff2ec..768e7ba 100644
--- a/pip/download.py
+++ b/pip/download.py
@@ -22,8 +22,7 @@ from pip.log import logger
import requests, six
from requests.adapters import BaseAdapter
from requests.auth import AuthBase, HTTPBasicAuth
-from requests.compat import IncompleteRead
-from requests.exceptions import InvalidURL, ChunkedEncodingError
+from requests.exceptions import InvalidURL
from requests.models import Response
from requests.structures import CaseInsensitiveDict
@@ -416,12 +415,32 @@ def _download_url(resp, link, temp_location):
def resp_read(chunk_size):
try:
# Special case for urllib3.
- try:
- for chunk in resp.raw.stream(
- chunk_size, decode_content=False):
- yield chunk
- except IncompleteRead as e:
- raise ChunkedEncodingError(e)
+ for chunk in resp.raw.stream(
+ chunk_size,
+ # We use decode_content=False here because we do
+ # want urllib3 to mess with the raw bytes we get
+ # from the server. If we decompress inside of
+ # urllib3 then we cannot verify the checksum
+ # because the checksum will be of the compressed
+ # file. This breakage will only occur if the
+ # server adds a Content-Encoding header, which
+ # depends on how the server was configured:
+ # - Some servers will notice that the file isn't a
+ # compressible file and will leave the file alone
+ # and with an empty Content-Encoding
+ # - Some servers will notice that the file is
+ # already compressed and will leave the file
+ # alone and will add a Content-Encoding: gzip
+ # header
+ # - Some servers won't notice anything at all and
+ # will take a file that's already been compressed
+ # and compress it again and set the
+ # Content-Encoding: gzip header
+ #
+ # By setting this not to decode automatically we
+ # hope to eliminate problems with the second case.
+ decode_content=False):
+ yield chunk
except AttributeError:
# Standard file-like object.
while True:
--
2.1.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment