Exemplo n.º 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')
Exemplo n.º 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
Exemplo n.º 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
Exemplo n.º 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
Exemplo n.º 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
Exemplo n.º 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)
Exemplo n.º 7
0
 def get_write_fd():
     f = generic_io.get_file(buff, mode='w')
     assert isinstance(f, generic_io.MemoryIO)
     return f
Exemplo n.º 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
Exemplo n.º 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
Exemplo n.º 10
0
 def get_write_fd():
     return generic_io.get_file(open(path, 'wb'), mode='w')
Exemplo n.º 11
0
 def get_read_fd():
     return generic_io.get_file(
         urllib_request.urlopen(httpserver.url + "test.asdf"))
Exemplo n.º 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
Exemplo n.º 13
0
 def get_read_fd():
     return generic_io.get_file(path, mode='r')
Exemplo n.º 14
0
 def get_write_fd():
     return generic_io.get_file(path, mode='w')
Exemplo n.º 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
Exemplo n.º 16
0
 def get_write_fd():
     return generic_io.get_file(open(path, 'wb'), mode='w')
Exemplo n.º 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+")
Exemplo n.º 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')
Exemplo n.º 19
0
 def get_read_fd():
     return generic_io.get_file(httpserver.url + "test.asdf")
Exemplo n.º 20
0
 def get_read_fd():
     return generic_io.get_file(
         urllib_request.urlopen(
             httpserver.url + "test.asdf"))
Exemplo n.º 21
0
 def get_read_fd():
     return generic_io.get_file(httpserver.url + "test.asdf")
Exemplo n.º 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
Exemplo n.º 23
0
 def get_read_fd():
     return generic_io.get_file(path, mode='r')
Exemplo n.º 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
Exemplo n.º 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')
Exemplo n.º 26
0
 def get_write_fd():
     return generic_io.get_file(path, mode='w')
Exemplo n.º 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+")
Exemplo n.º 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
Exemplo n.º 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
Exemplo n.º 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')
Exemplo n.º 31
0
 def get_write_fd():
     f = generic_io.get_file(buff, mode='w')
     assert isinstance(f, generic_io.MemoryIO)
     return f
Exemplo n.º 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
Exemplo n.º 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
Exemplo n.º 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
Exemplo n.º 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