# Buggy code
def _shortcut_open(uri, mode, **kw):
    """Try to open the URI using the standard library io.open function.

    This can be much faster than the alternative of opening in binary mode and
    then decoding.

    This is only possible under the following conditions:

        1. Opening a local file
        2. Ignore extension is set to True

    If it is not possible to use the built-in open for the specified URI, returns None.

    :param str uri: A string indicating what to open.
    :param str mode: The mode to pass to the open function.
    :param dict kw:
    :returns: The opened file
    :rtype: file
    """
    if not isinstance(uri, six.string_types):
        return None

    parsed_uri = _parse_uri(uri)
    if parsed_uri.scheme != "file":
        return None

    _, extension = P.splitext(parsed_uri.uri_path)
    ignore_extension = kw.get("ignore_extension", False)
    if extension in (".gz", ".bz2") and not ignore_extension:
        return None

    open_kwargs = {}
    errors = kw.get("errors")
    if errors is not None:
        open_kwargs["errors"] = errors

    encoding = kw.get("encoding")
    if encoding is not None:
        open_kwargs["encoding"] = encoding
        mode = mode.replace("b", "")

    return io.open(parsed_uri.uri_path, mode, **open_kwargs)


# Traceback (IOError)
IOError                                   Traceback (most recent call last)
<ipython-input-1-afdc56d6c4b5> in <module>()
2 from gensim.test.utils import datapath
3
----> 4 m = FastText.load_fasttext_format(datapath("lee_fasttext"))

/home/ivan/release/gensim/gensim/models/fasttext.py in load_fasttext_format(cls, model_file, encoding)
697             model_file += '.bin'
698         model.file_name = model_file
--> 699         model.load_binary_data(encoding=encoding)
700         return model
701

/home/ivan/release/gensim/gensim/models/fasttext.py in load_binary_data(self, encoding)
712             self._load_model_params(f)
713             self._load_dict(f, encoding=encoding)
--> 714             self._load_vectors(f)
715
716     def _load_model_params(self, file_handle):

/home/ivan/release/gensim/gensim/models/fasttext.py in _load_vectors(self, file_handle)
819
820         self.num_original_vectors = num_vectors
--> 821         self.wv.vectors_ngrams = np.fromfile(file_handle, dtype=dtype, count=num_vectors * dim)
822         self.wv.vectors_ngrams = self.wv.vectors_ngrams.reshape((num_vectors, dim))
823         assert self.wv.vectors_ngrams.shape == (

IOError: first argument must be an open file

# Fixed code
def _shortcut_open(uri, mode, **kw):
    """Try to open the URI using the standard library io.open function.

    This can be much faster than the alternative of opening in binary mode and
    then decoding.

    This is only possible under the following conditions:

        1. Opening a local file
        2. Ignore extension is set to True

    If it is not possible to use the built-in open for the specified URI, returns None.

    :param str uri: A string indicating what to open.
    :param str mode: The mode to pass to the open function.
    :param dict kw:
    :returns: The opened file
    :rtype: file
    """
    if not isinstance(uri, six.string_types):
        return None

    parsed_uri = _parse_uri(uri)
    if parsed_uri.scheme != "file":
        return None

    _, extension = P.splitext(parsed_uri.uri_path)
    ignore_extension = kw.get("ignore_extension", False)
    if extension in (".gz", ".bz2") and not ignore_extension:
        return None

    #
    # https://docs.python.org/2/library/functions.html#open
    #
    # buffering: 0: off; 1: on; negative number: use system default
    #
    buffering = kw.get("buffering", -1)

    open_kwargs = {}
    errors = kw.get("errors")
    if errors is not None:
        open_kwargs["errors"] = errors

    encoding = kw.get("encoding")
    if encoding is not None:
        open_kwargs["encoding"] = encoding
        mode = mode.replace("b", "")

    #
    # Under Py3, the built-in open accepts kwargs, and it's OK to use that.
    # Under Py2, the built-in open _doesn't_ accept kwargs, but we still use it
    # whenever possible (see issue #207).  If we're under Py2 and have to use
    # kwargs, then we have no option other to use io.open.
    #
    if six.PY3:
        return open(parsed_uri.uri_path, mode, buffering=buffering, **open_kwargs)
    elif not open_kwargs:
        return open(parsed_uri.uri_path, mode, buffering=buffering)
    return io.open(parsed_uri.uri_path, mode, buffering=buffering, **open_kwargs)