def test_open_fail4(tmpdir): path = os.path.join(str(tmpdir), 'test.asdf') with open(path, 'w') as fd: fd.write("\n\n\n") with io.open(path, 'r') as fd: with pytest.raises(ValueError): generic_io.get_file(fd, mode='r')
def test_invalid_obj(tmpdir): with pytest.raises(ValueError): generic_io.get_file(42) path = os.path.join(str(tmpdir), 'test.asdf') with generic_io.get_file(path, 'w') as fd: with pytest.raises(ValueError): fd2 = generic_io.get_file(fd, 'r') with pytest.raises(ValueError): fd2 = generic_io.get_file("http://www.google.com", "w") with pytest.raises(TypeError): fd2 = generic_io.get_file(io.StringIO()) with open(path, 'rb') as fd: with pytest.raises(ValueError): fd2 = generic_io.get_file(fd, 'w') with io.open(path, 'rb') as fd: with pytest.raises(ValueError): fd2 = generic_io.get_file(fd, 'w') with generic_io.get_file(sys.__stdout__, 'w'): pass
def test_close_underlying(tmpdir): path = os.path.join(str(tmpdir), 'test.asdf') with generic_io.get_file(open(path, 'wb'), mode='w', close=True) as ff: pass assert ff.is_closed() == True assert ff._fd.closed == True with generic_io.get_file(open(path, 'rb'), close=True) as ff2: pass assert ff2.is_closed() == True assert ff2._fd.closed == True
def get_read_fd(): fd = generic_io.get_file(httpserver.url + "test.asdf") assert isinstance(fd, generic_io.InputStream) # This is to check for a "feature" in Python 3.x that reading zero # bytes from a socket causes it to stop. We have code in generic_io.py # to workaround it. fd.read(0) return fd
def get_read_fd(): f = generic_io.get_file(path, mode='r') assert isinstance(f, generic_io.RealFile) assert f._uri == util.filepath_to_url(path) # This is to check for a "feature" in Python 3.x that reading zero # bytes from a socket causes it to stop. We have code in generic_io.py # to workaround it. f.read(0) return f
def test_nonseekable_file(tmpdir): base = io.IOBase class FileWrapper(base): def tell(self): raise IOError() def seekable(self): return False def readable(self): return True def writable(self): return True with FileWrapper(os.path.join(str(tmpdir), 'test.asdf'), 'wb') as fd: assert isinstance(generic_io.get_file(fd, 'w'), generic_io.OutputStream) with pytest.raises(ValueError): generic_io.get_file(fd, 'rw') with FileWrapper(os.path.join(str(tmpdir), 'test.asdf'), 'rb') as fd: assert isinstance(generic_io.get_file(fd, 'r'), generic_io.InputStream)
def get_write_fd(): f = generic_io.get_file(buff, mode='w') assert isinstance(f, generic_io.MemoryIO) return f
def get_read_fd(): buff.seek(0) f = generic_io.get_file(buff, mode='rw') assert isinstance(f, generic_io.MemoryIO) return f
def get_write_fd(): f = generic_io.get_file(open(path, 'wb'), mode='w', close=True) assert isinstance(f, generic_io.RealFile) assert f._uri == util.filepath_to_url(path) return f
def get_write_fd(): return generic_io.get_file(open(path, 'wb'), mode='w')
def get_read_fd(): return generic_io.get_file( urllib_request.urlopen(httpserver.url + "test.asdf"))
def get_read_fd(): fd = generic_io.get_file(rhttpserver.url + "test.asdf") assert isinstance(fd, generic_io.HTTPConnection) connection[0] = fd return fd
def get_read_fd(): return generic_io.get_file(path, mode='r')
def get_write_fd(): return generic_io.get_file(path, mode='w')
def get_write_fd(): f = generic_io.get_file(path, mode='w') assert isinstance(f, generic_io.RealFile) assert f._uri == util.filepath_to_url(path) return f
def test_mode_fail(tmpdir): path = os.path.join(str(tmpdir), 'test.asdf') with pytest.raises(ValueError): generic_io.get_file(path, mode="r+")
def test_arbitrary_file_object(): class Wrapper(object): def __init__(self, init): self._fd = init class Random(object): def seek(self, *args): return self._fd.seek(*args) def tell(self, *args): return self._fd.tell(*args) class Reader(Wrapper): def read(self, *args): return self._fd.read(*args) class RandomReader(Reader, Random): pass class Writer(Wrapper): def write(self, *args): return self._fd.write(*args) class RandomWriter(Writer, Random): pass class All(Reader, Writer, Random): pass buff = io.BytesIO() assert isinstance(generic_io.get_file(Reader(buff), 'r'), generic_io.InputStream) assert isinstance(generic_io.get_file(Writer(buff), 'w'), generic_io.OutputStream) assert isinstance(generic_io.get_file(RandomReader(buff), 'r'), generic_io.MemoryIO) assert isinstance(generic_io.get_file(RandomWriter(buff), 'w'), generic_io.MemoryIO) assert isinstance(generic_io.get_file(All(buff), 'rw'), generic_io.MemoryIO) assert isinstance(generic_io.get_file(All(buff), 'r'), generic_io.MemoryIO) assert isinstance(generic_io.get_file(All(buff), 'w'), generic_io.MemoryIO) with pytest.raises(ValueError): generic_io.get_file(Reader(buff), 'w') with pytest.raises(ValueError): generic_io.get_file(Writer(buff), 'r')
def get_read_fd(): return generic_io.get_file(httpserver.url + "test.asdf")
def get_read_fd(): return generic_io.get_file( urllib_request.urlopen( httpserver.url + "test.asdf"))
def get_read_fd(): # Must open with mode=rw in order to get memmapped data f = generic_io.get_file(open(path, 'r+b'), mode='rw', close=True) assert isinstance(f, generic_io.RealFile) assert f._uri == util.filepath_to_url(path) return f
def test_open_fail2(tmpdir): path = os.path.join(str(tmpdir), 'test.asdf') with io.open(path, 'w') as fd: with pytest.raises(ValueError): generic_io.get_file(fd, mode='w')
def get_read_fd(): f = generic_io.get_file(io.open(path, 'r+b'), mode='rw', close=True) assert isinstance(f, generic_io.RealFile) assert f._uri == util.filepath_to_url(path) return f
def test_arbitrary_file_object(): class Wrapper(object): def __init__(self, init): self._fd = init class Random(object): def seek(self, *args): return self._fd.seek(*args) def tell(self, *args): return self._fd.tell(*args) class Reader(Wrapper): def read(self, *args): return self._fd.read(*args) class RandomReader(Reader, Random): pass class Writer(Wrapper): def write(self, *args): return self._fd.write(*args) class RandomWriter(Writer, Random): pass class All(Reader, Writer, Random): pass buff = io.BytesIO() assert isinstance( generic_io.get_file(Reader(buff), 'r'), generic_io.InputStream) assert isinstance( generic_io.get_file(Writer(buff), 'w'), generic_io.OutputStream) assert isinstance( generic_io.get_file(RandomReader(buff), 'r'), generic_io.MemoryIO) assert isinstance( generic_io.get_file(RandomWriter(buff), 'w'), generic_io.MemoryIO) assert isinstance( generic_io.get_file(All(buff), 'rw'), generic_io.MemoryIO) assert isinstance( generic_io.get_file(All(buff), 'r'), generic_io.MemoryIO) assert isinstance( generic_io.get_file(All(buff), 'w'), generic_io.MemoryIO) with pytest.raises(ValueError): generic_io.get_file(Reader(buff), 'w') with pytest.raises(ValueError): generic_io.get_file(Writer(buff), 'r')