Skip to content

io.open patch failing with numpy's genfromtxt #832

@jamesbraza

Description

@jamesbraza

Describe the bug

It seems numpy.genfromtxt is failing with pyfakefs, it's throwing a FileNotFoundError despite the file actually being present.

How To Reproduce

import csv
import pathlib

import numpy as np
import pytest

THIS_DIRECTORY = pathlib.PurePath(__file__).parent


@pytest.fixture(name="thisdir_fs")
def fixture_thisdir_fs(fs):
    fs.add_real_directory(THIS_DIRECTORY, read_only=False)
    return fs


def test_with_numpy_genfromtxt(thisdir_fs) -> None:
    csv_file = THIS_DIRECTORY / "stub.csv"
    with open(csv_file, mode="w", newline="", encoding="utf-8") as f:
        csv_writer = csv.writer(f)
        csv_writer.writerow(["spam", "ham"])
        csv_writer.writerows([(1, 2), (3, 4)])

    with open(csv_file, newline="", encoding="utf-8") as f:
        assert len(f.readlines()) == 3  # Confirm file was actually made in fake fs

    # FileNotFoundError: [Errno 2] No such file or directory: '/path/to/file/stub.csv'
    file_data = np.genfromtxt(str(csv_file), delimiter=",", skip_header=True)

Running this test will yield the following FileNotFoundError:

file.py::test_with_numpy_genfromtxt FAILED
file.py:15 (test_with_numpy_genfromtxt)
thisdir_fs = <pyfakefs.fake_filesystem.FakeFilesystem object at 0x102fda170>

    def test_with_numpy_genfromtxt(thisdir_fs) -> None:
        csv_file = THIS_DIRECTORY / "stub.csv"
        with open(csv_file, mode="w", newline="", encoding="utf-8") as f:
            csv_writer = csv.writer(f)
            csv_writer.writerow(["spam", "ham"])
            csv_writer.writerows([(1, 2), (3, 4)])
    
        with open(csv_file, newline="", encoding="utf-8") as f:
            assert len(f.readlines()) == 3  # Confirm file was actually made in fake fs
    
        # FileNotFoundError: [Errno 2] No such file or directory: '/path/to/file/stub.csv'
>       file_data = np.genfromtxt(str(csv_file), delimiter=",", skip_header=True)

/path/to/file.py:27: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/path/to/venv/lib/python3.10/site-packages/numpy/lib/npyio.py:1977: in genfromtxt
    fid = np.lib._datasource.open(fname, 'rt', encoding=encoding)
/path/to/venv/lib/python3.10/site-packages/numpy/lib/_datasource.py:193: in open
    return ds.open(path, mode, encoding=encoding, newline=newline)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <numpy.DataSource object at 0x10335a800>
path = '/path/to/file/stub.csv', mode = 'rt', encoding = None
newline = None

    def open(self, path, mode='r', encoding=None, newline=None):
        """
        Open and return file-like object.
    
        If `path` is an URL, it will be downloaded, stored in the
        `DataSource` directory and opened from there.
    
        Parameters
        ----------
        path : str
            Local file path or URL to open.
        mode : {'r', 'w', 'a'}, optional
            Mode to open `path`.  Mode 'r' for reading, 'w' for writing,
            'a' to append. Available modes depend on the type of object
            specified by `path`. Default is 'r'.
        encoding : {None, str}, optional
            Open text file with given encoding. The default encoding will be
            what `io.open` uses.
        newline : {None, str}, optional
            Newline to use when reading text file.
    
        Returns
        -------
        out : file object
            File object.
    
        """
    
        # TODO: There is no support for opening a file for writing which
        #       doesn't exist yet (creating a file).  Should there be?
    
        # TODO: Add a ``subdir`` parameter for specifying the subdirectory
        #       used to store URLs in self._destpath.
    
        if self._isurl(path) and self._iswritemode(mode):
            raise ValueError("URLs are not writeable")
    
        # NOTE: _findfile will fail on a new file opened for writing.
        found = self._findfile(path)
        if found:
            _fname, ext = self._splitzipext(found)
            if ext == 'bz2':
                mode.replace("+", "")
>           return _file_openers[ext](found, mode=mode,
                                      encoding=encoding, newline=newline)
E           FileNotFoundError: [Errno 2] No such file or directory: '/path/to/file/stub.csv'

/path/to/venv/lib/python3.10/site-packages/numpy/lib/_datasource.py:530: FileNotFoundError

Your environment

> python -c "import platform; print(platform.platform())"
macOS-12.6-arm64-arm-64bit
> python -c "import sys; print('Python', sys.version)"
Python 3.10.9 (main, Jan  6 2023, 11:19:20) [Clang 14.0.0 (clang-1400.0.29.102)]
> python -c "from pyfakefs import __version__; print('pyfakefs', __version__)"
pyfakefs 5.2.2
> python -c "import pytest; print('pytest', pytest.__version__)"
pytest 7.3.1
> python -c "import numpy; print('numpy', numpy.__version__)"
numpy 1.24.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions