Beispiel #1
0
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')
Beispiel #2
0
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
Beispiel #3
0
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
Beispiel #4
0
 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
Beispiel #5
0
 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
Beispiel #6
0
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)
Beispiel #7
0
 def get_write_fd():
     f = generic_io.get_file(buff, mode='w')
     assert isinstance(f, generic_io.MemoryIO)
     return f
Beispiel #8
0
 def get_read_fd():
     buff.seek(0)
     f = generic_io.get_file(buff, mode='rw')
     assert isinstance(f, generic_io.MemoryIO)
     return f
Beispiel #9
0
 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
Beispiel #10
0
 def get_write_fd():
     return generic_io.get_file(open(path, 'wb'), mode='w')
Beispiel #11
0
 def get_read_fd():
     return generic_io.get_file(
         urllib_request.urlopen(httpserver.url + "test.asdf"))
Beispiel #12
0
 def get_read_fd():
     fd = generic_io.get_file(rhttpserver.url + "test.asdf")
     assert isinstance(fd, generic_io.HTTPConnection)
     connection[0] = fd
     return fd
Beispiel #13
0
 def get_read_fd():
     return generic_io.get_file(path, mode='r')
Beispiel #14
0
 def get_write_fd():
     return generic_io.get_file(path, mode='w')
Beispiel #15
0
 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
Beispiel #16
0
 def get_write_fd():
     return generic_io.get_file(open(path, 'wb'), mode='w')
Beispiel #17
0
def test_mode_fail(tmpdir):
    path = os.path.join(str(tmpdir), 'test.asdf')

    with pytest.raises(ValueError):
        generic_io.get_file(path, mode="r+")
Beispiel #18
0
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')
Beispiel #19
0
 def get_read_fd():
     return generic_io.get_file(httpserver.url + "test.asdf")
Beispiel #20
0
 def get_read_fd():
     return generic_io.get_file(
         urllib_request.urlopen(
             httpserver.url + "test.asdf"))
Beispiel #21
0
 def get_read_fd():
     return generic_io.get_file(httpserver.url + "test.asdf")
Beispiel #22
0
 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
Beispiel #23
0
 def get_read_fd():
     return generic_io.get_file(path, mode='r')
Beispiel #24
0
 def get_read_fd():
     fd = generic_io.get_file(rhttpserver.url + "test.asdf")
     assert isinstance(fd, generic_io.HTTPConnection)
     connection[0] = fd
     return fd
Beispiel #25
0
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')
Beispiel #26
0
 def get_write_fd():
     return generic_io.get_file(path, mode='w')
Beispiel #27
0
def test_mode_fail(tmpdir):
    path = os.path.join(str(tmpdir), 'test.asdf')

    with pytest.raises(ValueError):
        generic_io.get_file(path, mode="r+")
Beispiel #28
0
 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
Beispiel #29
0
 def get_read_fd():
     buff.seek(0)
     f = generic_io.get_file(buff, mode='rw')
     assert isinstance(f, generic_io.MemoryIO)
     return f
Beispiel #30
0
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')
Beispiel #31
0
 def get_write_fd():
     f = generic_io.get_file(buff, mode='w')
     assert isinstance(f, generic_io.MemoryIO)
     return f
Beispiel #32
0
 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
Beispiel #33
0
 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
Beispiel #34
0
 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
Beispiel #35
0
 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