-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
use requests when it is installed #28874
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2031cfe
use requests when it is installed
ocefpaf 95e3b75
pop session out before calling urllib.request.urlopen
ocefpaf ac39c2e
no session obj for now
ocefpaf 03959aa
use import_optional_dependency
ocefpaf 02a2365
documento min requests version
ocefpaf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
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 |
---|---|---|
|
@@ -32,6 +32,7 @@ | |
import zipfile | ||
|
||
from pandas.compat import _get_lzma_file, _import_lzma | ||
from pandas.compat._optional import import_optional_dependency | ||
from pandas.errors import ( # noqa | ||
AbstractMethodError, | ||
DtypeWarning, | ||
|
@@ -184,13 +185,25 @@ def is_gcs_url(url) -> bool: | |
|
||
|
||
def urlopen(*args, **kwargs): | ||
""" | ||
Lazy-import wrapper for stdlib urlopen, as that imports a big chunk of | ||
the stdlib. | ||
""" | ||
import urllib.request | ||
compression = None | ||
content_encoding = None | ||
try: | ||
requests = import_optional_dependency("requests") | ||
r = requests.get(*args, **kwargs) | ||
r.raise_for_status() | ||
content = r.content | ||
r.close() | ||
except ImportError: | ||
import urllib.request | ||
|
||
return urllib.request.urlopen(*args, **kwargs) | ||
r = urllib.request.urlopen(*args, **kwargs) | ||
content = r.read() | ||
content_encoding = r.headers.get("Content-Encoding", None) | ||
if content_encoding == "gzip": | ||
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 think this also needs to be under the |
||
# Override compression based on Content-Encoding header. | ||
compression = "gzip" | ||
reader = BytesIO(content) | ||
return reader, compression | ||
|
||
|
||
def get_filepath_or_buffer( | ||
|
@@ -221,13 +234,7 @@ def get_filepath_or_buffer( | |
filepath_or_buffer = _stringify_path(filepath_or_buffer) | ||
|
||
if isinstance(filepath_or_buffer, str) and _is_url(filepath_or_buffer): | ||
req = urlopen(filepath_or_buffer) | ||
content_encoding = req.headers.get("Content-Encoding", None) | ||
if content_encoding == "gzip": | ||
# Override compression based on Content-Encoding header | ||
compression = "gzip" | ||
reader = BytesIO(req.read()) | ||
req.close() | ||
reader, compression = urlopen(filepath_or_buffer) | ||
return reader, encoding, compression, True | ||
|
||
if is_s3_url(filepath_or_buffer): | ||
|
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
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.
can you use pandas.compat._optional.import_optional_dependency here
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.
Not sure if I got it right.