Skip to content

Added with context manager to if clauses in _monkey_patch #19367

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

Merged
merged 3 commits into from
Feb 6, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions sklearn/datasets/tests/test_openml.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Test the openml loader.
"""
import gzip
from io import BytesIO
import json
import numpy as np
import os
Expand Down Expand Up @@ -203,21 +204,26 @@ def _mock_urlopen_data_description(url, has_gzip_header):
_file_name(url, '.json'))

if has_gzip_header and gzip_response:
fp = open(path, 'rb')
with open(path, 'rb') as f:
fp = BytesIO(f.read())
return _MockHTTPResponse(fp, True)
else:
fp = read_fn(path, 'rb')
with read_fn(path, 'rb') as f:
fp = BytesIO(f.read())
return _MockHTTPResponse(fp, False)

def _mock_urlopen_data_features(url, has_gzip_header):
assert url.startswith(url_prefix_data_features)
path = os.path.join(currdir, 'data', 'openml', str(data_id),
_file_name(url, '.json'))

if has_gzip_header and gzip_response:
fp = open(path, 'rb')
with open(path, 'rb') as f:
fp = BytesIO(f.read())
return _MockHTTPResponse(fp, True)
else:
fp = read_fn(path, 'rb')
with read_fn(path, 'rb') as f:
fp = BytesIO(f.read())
return _MockHTTPResponse(fp, False)

def _mock_urlopen_download_data(url, has_gzip_header):
Expand All @@ -227,10 +233,12 @@ def _mock_urlopen_download_data(url, has_gzip_header):
_file_name(url, '.arff'))

if has_gzip_header and gzip_response:
fp = open(path, 'rb')
with open(path, 'rb') as f:
fp = BytesIO(f.read())
return _MockHTTPResponse(fp, True)
else:
fp = read_fn(path, 'rb')
with read_fn(path, 'rb') as f:
fp = BytesIO(f.read())
return _MockHTTPResponse(fp, False)

def _mock_urlopen_data_list(url, has_gzip_header):
Expand All @@ -247,10 +255,12 @@ def _mock_urlopen_data_list(url, has_gzip_header):
hdrs=None, fp=None)

if has_gzip_header:
fp = open(json_file_path, 'rb')
with open(json_file_path, 'rb') as f:
fp = BytesIO(f.read())
return _MockHTTPResponse(fp, True)
else:
fp = read_fn(json_file_path, 'rb')
with read_fn(json_file_path, 'rb') as f:
fp = BytesIO(f.read())
return _MockHTTPResponse(fp, False)

def _mock_urlopen(request):
Expand Down