# Buggy code
def _open_openml_url(openml_path, data_home):
    """
    Returns a resource from OpenML.org. Caches it to data_home if required.

    Parameters
    ----------
    openml_path : str
        OpenML URL that will be accessed. This will be prefixes with
        _OPENML_PREFIX

    data_home : str
        Directory to which the files will be cached. If None, no caching will
        be applied.

    Returns
    -------
    result : stream
        A stream to the OpenML resource
    """

    def is_gzip(_fsrc):
        return _fsrc.info().get("Content-Encoding", "") == "gzip"

    req = Request(_OPENML_PREFIX + openml_path)
    req.add_header("Accept-encoding", "gzip")

    if data_home is None:
        fsrc = urlopen(req)
        if is_gzip(fsrc):
            if PY2:
                fsrc = BytesIO(fsrc.read())
            return gzip.GzipFile(fileobj=fsrc, mode="rb")
        return fsrc

    local_path = _get_local_path(openml_path, data_home)
    if not os.path.exists(local_path):
        fsrc = urlopen(req)
        try:
            os.makedirs(os.path.dirname(local_path))
        except OSError:
            # potentially, the directory has been created already
            pass

        try:
            if is_gzip(fsrc):
                with open(local_path, "wb") as fdst:
                    shutil.copyfileobj(fsrc, fdst)
                    fsrc.close()
            else:
                with gzip.GzipFile(local_path, "wb") as fdst:
                    shutil.copyfileobj(fsrc, fdst)
                    fsrc.close()
        except Exception:
            os.unlink(local_path)
            raise

    # XXX: First time, decompression will not be necessary (by using fsrc), but
    # it will happen nonetheless
    return gzip.GzipFile(local_path, "rb")


# Traceback (OSError)
In [1]: from sklearn.datasets import fetch_openml
In [2]: fetch_openml('mnist_784', version=1, return_X_y=True)
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-2-0c2a4453f0a2> in <module>
----> 1 fetch_openml('mnist_784', version=1, return_X_y=True)

~/src/scikit-learn/sklearn/datasets/openml.py in fetch_openml(name, version, data_id, data_home, target_column, cache, return_X_y)
490                 "specify a numeric data_id or a name, not "
491                 "both.".format(data_id, name))
--> 492         data_info = _get_data_info_by_name(name, version, data_home)
493         data_id = data_info['did']
494     elif data_id is not None:

~/src/scikit-learn/sklearn/datasets/openml.py in _get_data_info_by_name(name, version, data_home)
285     url = (_SEARCH_NAME + "/data_version/{}").format(name, version)
286     json_data = _get_json_content_from_openml_api(url, None, False,
--> 287                                                   data_home)
288     if json_data is None:
289         # we can do this in 1 function call if OpenML does not require the

~/src/scikit-learn/sklearn/datasets/openml.py in _get_json_content_from_openml_api(url, error_message, raise_if_error, data_home)
143         else:
144             return None
--> 145     json_data = json.loads(response.read().decode("utf-8"))
146     response.close()
147     return json_data

~/.miniconda3/envs/sklearn-dev/lib/python3.7/gzip.py in read(self, size)
274             import errno
275             raise OSError(errno.EBADF, "read() on write-only GzipFile object")
--> 276         return self._buffer.read(size)
277
278     def read1(self, size=-1):

~/.miniconda3/envs/sklearn-dev/lib/python3.7/gzip.py in read(self, size)
461                 # jump to the next member, if there is one.
462                 self._init_read()
--> 463                 if not self._read_gzip_header():
464                     self._size = self._pos
465                     return b""

~/.miniconda3/envs/sklearn-dev/lib/python3.7/gzip.py in _read_gzip_header(self)
409
410         if magic != b'\037\213':
--> 411             raise OSError('Not a gzipped file (%r)' % magic)
412
413         (method, flag,

OSError: Not a gzipped file (b'{"')

# Fixed code
def _open_openml_url(openml_path, data_home):
    """
    Returns a resource from OpenML.org. Caches it to data_home if required.

    Parameters
    ----------
    openml_path : str
        OpenML URL that will be accessed. This will be prefixes with
        _OPENML_PREFIX

    data_home : str
        Directory to which the files will be cached. If None, no caching will
        be applied.

    Returns
    -------
    result : stream
        A stream to the OpenML resource
    """

    def is_gzip(_fsrc):
        return _fsrc.info().get("Content-Encoding", "") == "gzip"

    req = Request(_OPENML_PREFIX + openml_path)
    req.add_header("Accept-encoding", "gzip")

    if data_home is None:
        fsrc = urlopen(req)
        if is_gzip(fsrc):
            if PY2:
                fsrc = BytesIO(fsrc.read())
            return gzip.GzipFile(fileobj=fsrc, mode="rb")
        return fsrc

    local_path = _get_local_path(openml_path, data_home)
    if not os.path.exists(local_path):
        try:
            os.makedirs(os.path.dirname(local_path))
        except OSError:
            # potentially, the directory has been created already
            pass

        try:
            with closing(urlopen(req)) as fsrc:
                if is_gzip(fsrc):
                    with open(local_path, "wb") as fdst:
                        shutil.copyfileobj(fsrc, fdst)
                else:
                    with gzip.GzipFile(local_path, "wb") as fdst:
                        shutil.copyfileobj(fsrc, fdst)
        except Exception:
            if os.path.exists(local_path):
                os.unlink(local_path)
            raise

    # XXX: First time, decompression will not be necessary (by using fsrc), but
    # it will happen nonetheless
    return gzip.GzipFile(local_path, "rb")