Test handling with file-like objects.
(kind, preload, split, tmp_path)
| 2028 | [False, pytest.param(True, marks=pytest.mark.slowtest, id="splitTrue")], |
| 2029 | ) |
| 2030 | def test_file_like(kind, preload, split, tmp_path): |
| 2031 | """Test handling with file-like objects.""" |
| 2032 | fname = tmp_path / "test_file_like_raw.fif" |
| 2033 | fnames = (fname,) |
| 2034 | this_raw = read_raw_fif(test_fif_fname).crop(0, 4).pick("mag") |
| 2035 | if split: |
| 2036 | this_raw.save(fname, split_size="5MB") |
| 2037 | fnames += (Path(str(fname)[:-4] + "-1.fif"),) |
| 2038 | bad_fname = Path(str(fname)[:-4] + "-2.fif") |
| 2039 | assert not bad_fname.is_file() |
| 2040 | else: |
| 2041 | this_raw.save(fname) |
| 2042 | for f in fnames: |
| 2043 | assert f.is_file() |
| 2044 | if preload is str: |
| 2045 | if platform.system() == "Windows": |
| 2046 | pytest.skip("Cannot test preload=str on Windows") |
| 2047 | preload = str(tmp_path / "memmap") |
| 2048 | with open(fname, "rb") as file_fid: |
| 2049 | if kind == "bytes": |
| 2050 | fid = BytesIO(file_fid.read()) |
| 2051 | elif kind == "path": |
| 2052 | fid = fname |
| 2053 | else: |
| 2054 | assert kind == "file" |
| 2055 | fid = file_fid |
| 2056 | if kind != "path": |
| 2057 | assert not fid.closed |
| 2058 | with pytest.raises(ValueError, match="preload must be used with file"): |
| 2059 | read_raw_fif(fid) |
| 2060 | assert not file_fid.closed |
| 2061 | if kind != "path": |
| 2062 | assert not fid.closed |
| 2063 | assert not file_fid.closed |
| 2064 | # Use test_preloading=False but explicitly pass the preload type |
| 2065 | # so that we don't bother testing preload=False |
| 2066 | kwargs = dict( |
| 2067 | fname=fid, |
| 2068 | preload=preload, |
| 2069 | on_split_missing="warn", |
| 2070 | test_preloading=False, |
| 2071 | test_kwargs=False, |
| 2072 | ) |
| 2073 | want_filenames = list(fnames) |
| 2074 | if kind == "bytes": |
| 2075 | # the split file will not be correctly resolved for BytesIO |
| 2076 | want_filenames = [None] |
| 2077 | if split and kind == "bytes": |
| 2078 | ctx = pytest.warns(RuntimeWarning, match="Split raw file detected") |
| 2079 | else: |
| 2080 | ctx = nullcontext() |
| 2081 | with ctx: |
| 2082 | raw = _test_raw_reader(read_raw_fif, **kwargs) |
| 2083 | if kind != "path": |
| 2084 | assert not fid.closed |
| 2085 | assert not file_fid.closed |
| 2086 | want_filenames = tuple(want_filenames) |
| 2087 | assert raw.filenames == want_filenames |
nothing calls this directly
no test coverage detected