Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Mar 31, 2026. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 173
Feat: Add "auto" checksum option and make default#1383
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ffe8135
intermediate commit
andrewsg 25e1109
Feat: Add "auto" checksum option and make default
andrewsg 088b3a9
Update README
andrewsg 28c7e48
add requests/download.py docstring updates
andrewsg c4201c6
update parameterized system tests
andrewsg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -130,16 +130,28 @@ class Download(DownloadBase): | ||
| appropriate checksum (for instance in the case of transcoded or | ||
| ranged downloads where the remote service does not know the | ||
| correct checksum) an INFO-level log will be emitted. Supported | ||
| values are "md5", "crc32c" and None. | ||
| values are "md5", "crc32c", "auto" and None. The default is "auto", | ||
| which will try to detect if the C extension for crc32c is installed | ||
| and fall back to md5 otherwise. | ||
| """ | ||
| def __init__( | ||
| self, media_url, stream=None, start=None, end=None, headers=None, checksum="md5" | ||
| self, | ||
| media_url, | ||
| stream=None, | ||
| start=None, | ||
| end=None, | ||
| headers=None, | ||
| checksum="auto", | ||
| ): | ||
| super(Download, self).__init__( | ||
| media_url, stream=stream, start=start, end=end, headers=headers | ||
| ) | ||
| self.checksum = checksum | ||
| if self.checksum == "auto": | ||
| self.checksum = ( | ||
| "crc32c" if _helpers._is_crc32c_available_and_fast() else "md5" | ||
| ) | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like how this is handled in the constructor 🎉 | ||
| self._bytes_downloaded = 0 | ||
| self._expected_checksum = None | ||
| self._checksum_object = None | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -20,7 +20,6 @@ | ||
| import hashlib | ||
| import logging | ||
| import random | ||
| import warnings | ||
| from urllib.parse import parse_qs | ||
| from urllib.parse import urlencode | ||
| @@ -142,43 +141,6 @@ def calculate_retry_wait(base_wait, max_sleep, multiplier=2.0): | ||
| return new_base_wait, new_base_wait + 0.001 * jitter_ms | ||
| def _get_crc32c_object(): | ||
| """Get crc32c object | ||
| Attempt to use the Google-CRC32c package. If it isn't available, try | ||
| to use CRCMod. CRCMod might be using a 'slow' varietal. If so, warn... | ||
| """ | ||
| try: | ||
| import google_crc32c # type: ignore | ||
| crc_obj = google_crc32c.Checksum() | ||
| except ImportError: | ||
| try: | ||
| import crcmod # type: ignore | ||
| crc_obj = crcmod.predefined.Crc("crc-32c") | ||
| _is_fast_crcmod() | ||
| except ImportError: | ||
| raise ImportError("Failed to import either `google-crc32c` or `crcmod`") | ||
| return crc_obj | ||
| def _is_fast_crcmod(): | ||
| # Determine if this is using the slow form of crcmod. | ||
| nested_crcmod = __import__( | ||
| "crcmod.crcmod", | ||
| globals(), | ||
| locals(), | ||
| ["_usingExtension"], | ||
| 0, | ||
| ) | ||
| fast_crc = getattr(nested_crcmod, "_usingExtension", False) | ||
| if not fast_crc: | ||
| warnings.warn(_SLOW_CRC32C_WARNING, RuntimeWarning, stacklevel=2) | ||
| return fast_crc | ||
| def _get_metadata_key(checksum_type): | ||
| if checksum_type == "md5": | ||
| return "md5Hash" | ||
| @@ -231,10 +193,7 @@ def _get_expected_checksum(response, get_headers, media_url, checksum_type): | ||
| _LOGGER.info(msg) | ||
| checksum_object = _DoNothingHash() | ||
| else: | ||
| if checksum_type == "md5": | ||
| checksum_object = hashlib.md5() | ||
| else: | ||
| checksum_object = _get_crc32c_object() | ||
| checksum_object = _get_checksum_object(checksum_type) | ||
| else: | ||
| expected_checksum = None | ||
| checksum_object = _DoNothingHash() | ||
| @@ -331,13 +290,33 @@ def _get_checksum_object(checksum_type): | ||
| if checksum_type == "md5": | ||
| return hashlib.md5() | ||
| elif checksum_type == "crc32c": | ||
| return _get_crc32c_object() | ||
| # In order to support platforms that don't have google_crc32c | ||
| # support, only perform the import on demand. | ||
| import google_crc32c | ||
| return google_crc32c.Checksum() | ||
| elif checksum_type is None: | ||
| return None | ||
| else: | ||
| raise ValueError("checksum must be ``'md5'``, ``'crc32c'`` or ``None``") | ||
| def _is_crc32c_available_and_fast(): | ||
andrewsg marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| """Return True if the google_crc32c C extension is installed. | ||
| Return False if either the package is not installed, or if only the | ||
| pure-Python version is installed. | ||
| """ | ||
| try: | ||
| import google_crc32c | ||
| if google_crc32c.implementation == "c": | ||
| return True | ||
| except Exception: | ||
| pass | ||
| return False | ||
| def _parse_generation_header(response, get_headers): | ||
| """Parses the generation header from an ``X-Goog-Generation`` value. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding this! This will make 3.0 release notes really clear