Ejemplo n.º 1
0
    def test_parallel_writes_and_reads(self):
        # Warning: a test like the one below deadlocks CPython
        # http://bugs.python.org/issue1164
        # It also deadlocks on py.py because the space GIL is not
        # released.
        import thread, sys, os

        try:
            fdopen = self.file.fdopen
        except AttributeError:
            # when running with -A
            skip("deadlocks on top of CPython")
        read_fd, write_fd = os.pipe()
        fread = fdopen(read_fd, "rb", 200)
        fwrite = fdopen(write_fd, "wb", 200)
        run = True
        readers_done = [0]

        def writer():
            f = 0.1
            while run:
                print >> fwrite, f,
                f = 4 * f - 3 * f * f
            print >> fwrite, "X"
            fwrite.flush()
            sys.stdout.write("writer ends\n")

        def reader(j):
            while True:
                data = fread.read(1)
                # sys.stdout.write('%d%r ' % (j, data))
                if data == "X":
                    break
            sys.stdout.write("reader ends\n")
            readers_done[0] += 1

        for j in range(3):
            thread.start_new_thread(reader, (j,))
            thread.start_new_thread(writer, ())

        import time

        t = time.time() + 5
        print "start of test"
        while time.time() < t:
            time.sleep(1)
        print "end of test"

        assert readers_done[0] == 0
        run = False  # end the writers
        for i in range(600):
            time.sleep(0.4)
            sys.stdout.flush()
            x = readers_done[0]
            if x == 3:
                break
            print "readers_done == %d, still waiting..." % (x,)
        else:
            raise Exception("time out")
        print "Passed."
Ejemplo n.º 2
0
    def test_parallel_writes_and_reads(self):
        # Warning: a test like the one below deadlocks CPython
        # http://bugs.python.org/issue1164
        # It also deadlocks on py.py because the space GIL is not
        # released.
        import thread, sys, os
        try:
            fdopen = self.file.fdopen
        except AttributeError:
            # when running with -A
            skip("deadlocks on top of CPython")
        read_fd, write_fd = os.pipe()
        fread = fdopen(read_fd, 'rb', 200)
        fwrite = fdopen(write_fd, 'wb', 200)
        run = True
        readers_done = [0]

        def writer():
            f = 0.1
            while run:
                print >> fwrite, f,
                f = 4 * f - 3 * f * f
            print >> fwrite, "X"
            fwrite.flush()
            sys.stdout.write('writer ends\n')

        def reader(j):
            while True:
                data = fread.read(1)
                #sys.stdout.write('%d%r ' % (j, data))
                if data == "X":
                    break
            sys.stdout.write('reader ends\n')
            readers_done[0] += 1

        for j in range(3):
            thread.start_new_thread(reader, (j, ))
            thread.start_new_thread(writer, ())

        import time
        t = time.time() + 5
        print "start of test"
        while time.time() < t:
            time.sleep(1)
        print "end of test"

        assert readers_done[0] == 0
        run = False  # end the writers
        for i in range(600):
            time.sleep(0.4)
            sys.stdout.flush()
            x = readers_done[0]
            if x == 3:
                break
            print 'readers_done == %d, still waiting...' % (x, )
        else:
            raise Exception("time out")
        print 'Passed.'
Ejemplo n.º 3
0
def open (name, flags='r', mode=0, labelset=None, endpoint=None, bufsize=4096):
    
    py_fl = ""
    c_fl = 0

    map = { "w" : (posix.O_WRONLY, True),
            "r" : (posix.O_RDONLY, True),
            "a" : (posix.O_APPEND, True),
            "c" : (posix.O_CREAT, False),
            "e" : (posix.O_EXCL, False),
            "t" : (posix.O_TRUNC, False)}

    for c in flags:
        p = map.get (c)
        if p is None:
            raise ValueError, "Unknown flag to open: '%s'" % c
        c_fl |=  p[0]
        if p[1]:
            py_fl += c
        
    fd = fli._open (name, c_fl, mode, labelset, endpoint)
    if fd < 0:
        raise_err ("open failed on file '%s'" % name)
            
    return posix.fdopen (fd, py_fl, bufsize)
Ejemplo n.º 4
0
    def dup(self):
        import posix

        if not hasattr(posix, 'fdopen'):
            raise AttributeError, 'dup() method unavailable'

        return posix.fdopen(posix.dup(self._file_.fileno()), self._file_.mode)
Ejemplo n.º 5
0
    def dup(self):
	import posix

	try: ignore = posix.fdopen
	except: raise AttributeError, 'dup() method unavailable'

	return posix.fdopen(posix.dup(self._file_.fileno()), self._file_.mode)
Ejemplo n.º 6
0
    def Open(self, path, mode='r'):
        """Opens a path for read, but moves it out of the reserved 3-9 fd range.

    Returns:
      A Python file object.  The caller is responsible for Close().

    Raises:
      OSError if the path can't be found.
    """
        if mode == 'r':
            fd_mode = posix.O_RDONLY
        elif mode == 'w':
            fd_mode = posix.O_CREAT | posix.O_RDWR
        else:
            raise AssertionError(mode)

        fd = posix.open(path, fd_mode, 0666)  # may raise OSError
        new_fd = self._GetFreeDescriptor()
        posix.dup2(fd, new_fd)
        posix.close(fd)
        try:
            f = posix.fdopen(new_fd, mode)  # Might raise IOError
        except IOError as e:
            raise OSError(*e.args)  # Consistently raise OSError
        return f
Ejemplo n.º 7
0
 def test_ignore_ioerror_in_readall_if_nonempty_result(self):
     # this is the behavior of regular files in CPython 2.7, as
     # well as of _io.FileIO at least in CPython 3.3.  This is
     # *not* the behavior of _io.FileIO in CPython 3.4 or 3.5;
     # see CPython's issue #21090.
     import sys
     try:
         from posix import openpty, fdopen, write, close
     except ImportError:
         skip('no openpty on this platform')
     if 'gnukfreebsd' in sys.platform:
         skip('close() hangs forever on kFreeBSD')
     read_fd, write_fd = openpty()
     write(write_fd, 'Abc\n')
     close(write_fd)
     f = fdopen(read_fd)
     # behavior on Linux: f.read() returns 'Abc\r\n', then the next time
     # it raises IOError.  Behavior on OS/X (Python 2.7.5): the close()
     # above threw away the buffer, and f.read() always returns ''.
     if sys.platform.startswith('linux'):
         s = f.read()
         assert s == 'Abc\r\n'
         raises(IOError, f.read)
     else:
         s = f.read()
         assert s == ''
         s = f.read()
         assert s == ''
     f.close()
Ejemplo n.º 8
0
 def test_ignore_ioerror_in_readall_if_nonempty_result(self):
     # this is the behavior of regular files in CPython 2.7, as
     # well as of _io.FileIO at least in CPython 3.3.  This is
     # *not* the behavior of _io.FileIO in CPython 3.4 or 3.5;
     # see CPython's issue #21090.
     import sys
     try:
         from posix import openpty, fdopen, write, close
     except ImportError:
         skip('no openpty on this platform')
     read_fd, write_fd = openpty()
     write(write_fd, 'Abc\n')
     close(write_fd)
     f = fdopen(read_fd)
     # behavior on Linux: f.read() returns 'Abc\r\n', then the next time
     # it raises IOError.  Behavior on OS/X (Python 2.7.5): the close()
     # above threw away the buffer, and f.read() always returns ''.
     if sys.platform.startswith('linux'):
         s = f.read()
         assert s == 'Abc\r\n'
         raises(IOError, f.read)
     else:
         s = f.read()
         assert s == ''
         s = f.read()
         assert s == ''
     f.close()
Ejemplo n.º 9
0
    def dup(self):
        import posix

        if not hasattr(posix, 'fdopen'):
            raise AttributeError, 'dup() method unavailable'

        return posix.fdopen(posix.dup(self._file_.fileno()), self._file_.mode)
Ejemplo n.º 10
0
    def dup2(self, fd):
        import posix

        if not hasattr(posix, "fdopen"):
            raise AttributeError, "dup() method unavailable"
        posix.dup2(self._file_.fileno(), fd)
        return posix.fdopen(fd, self._file_.mode)
Ejemplo n.º 11
0
    def dup2(self, fd):
        import posix

        if not hasattr(posix, 'fdopen'):
            raise AttributeError('dup() method unavailable')

        posix.dup2(self._file_.fileno(), fd)
        return posix.fdopen(fd, self._file_.mode)
Ejemplo n.º 12
0
 def test_readinto_error(self):
     import _socket, posix, array
     s = _socket.socket()
     buff = array.array("c", "X" * 65)
     fh = posix.fdopen(posix.dup(s.fileno()), 'rb')
     # "Transport endpoint is not connected"
     raises(IOError, fh.readinto, buff)
     fh.close()
     s.close()
Ejemplo n.º 13
0
    def dup(self):
        import posix

        try:
            ignore = posix.fdopen
        except:
            raise AttributeError, 'dup() method unavailable'

        return posix.fdopen(posix.dup(self._file_.fileno()), self._file_.mode)
Ejemplo n.º 14
0
 def test_readinto_error(self):
     import _socket, posix, array
     s = _socket.socket()
     buff = array.array("c", "X" * 65)
     fh = posix.fdopen(posix.dup(s.fileno()), 'rb')
     # "Transport endpoint is not connected"
     raises(IOError, fh.readinto, buff)
     fh.close()
     s.close()
Ejemplo n.º 15
0
    def test_EAGAIN(self):
        import _socket, posix
        s1, s2 = _socket.socketpair()
        s2.setblocking(False)
        s1.send("hello")

        f2 = posix.fdopen(posix.dup(s2.fileno()), 'rb', 0)
        data = f2.read(12)
        assert data == "hello"

        f2.close()
        s2.close()
        s1.close()
Ejemplo n.º 16
0
    def test_EAGAIN(self):
        import _socket, posix
        s1, s2 = _socket.socketpair()
        s2.setblocking(False)
        s1.send("hello")

        f2 = posix.fdopen(posix.dup(s2.fileno()), 'rb', 0)
        data = f2.read(12)
        assert data == "hello"

        f2.close()
        s2.close()
        s1.close()
Ejemplo n.º 17
0
 def test_ignore_ioerror_in_readall_if_nonempty_result(self):
     # this is the behavior of regular files in CPython 2.7, as
     # well as of _io.FileIO at least in CPython 3.3.  This is
     # *not* the behavior of _io.FileIO in CPython 3.4 or 3.5;
     # see CPython's issue #21090.
     try:
         from posix import openpty, fdopen, write, close
     except ImportError:
         skip('no openpty on this platform')
     read_fd, write_fd = openpty()
     write(write_fd, 'Abc\n')
     close(write_fd)
     f = fdopen(read_fd)
     s = f.read()
     assert s == 'Abc\r\n'
     raises(IOError, f.read)
     f.close()
Ejemplo n.º 18
0
 def test_ignore_ioerror_in_readall_if_nonempty_result(self):
     # this is the behavior of regular files in CPython 2.7, as
     # well as of _io.FileIO at least in CPython 3.3.  This is
     # *not* the behavior of _io.FileIO in CPython 3.4 or 3.5;
     # see CPython's issue #21090.
     try:
         from posix import openpty, fdopen, write, close
     except ImportError:
         skip('no openpty on this platform')
     read_fd, write_fd = openpty()
     write(write_fd, 'Abc\n')
     close(write_fd)
     f = fdopen(read_fd)
     s = f.read()
     assert s == 'Abc\r\n'
     raises(IOError, f.read)
     f.close()
Ejemplo n.º 19
0
    def Open(self, path, mode='r'):
        """Opens a path for read, but moves it out of the reserved 3-9 fd range.

    Returns:
      A Python file object.  The caller is responsible for Close().

    Raises:
      OSError if the path can't be found.
    """
        if mode == 'r':
            fd_mode = posix.O_RDONLY
        elif mode == 'w':
            fd_mode = posix.O_CREAT | posix.O_RDWR
        else:
            raise AssertionError(mode)

        fd = posix.open(path, fd_mode, 0666)
        new_fd = self._NextFreeFileDescriptor()
        posix.dup2(fd, new_fd)
        posix.close(fd)
        return posix.fdopen(new_fd, mode)
Ejemplo n.º 20
0
 def test_fdopen(self):
     import os
     f = self.file(self.temppath, "w")
     try:
         f.write("foo\nbaz\n")
     finally:
         f.close()
     try:
         fdopen = self.file.fdopen
     except AttributeError:
         fdopen = os.fdopen      # when running with -A
     fd = os.open(self.temppath, os.O_WRONLY | os.O_CREAT)
     f2 = fdopen(fd, "a")
     f2.seek(0, 2)
     f2.write("bar\nboo")
     f2.close()
     # don't close fd, will get a whining __del__
     f = self.file(self.temppath, "r")
     try:
         s = f.read()
         assert s == "foo\nbaz\nbar\nboo"
     finally:
         f.close()
Ejemplo n.º 21
0
 def test_fdopen(self):
     import os
     f = self.file(self.temppath, "w")
     try:
         f.write("foo\nbaz\n")
     finally:
         f.close()
     try:
         fdopen = self.file.fdopen
     except AttributeError:
         fdopen = os.fdopen  # when running with -A
     fd = os.open(self.temppath, os.O_WRONLY | os.O_CREAT)
     f2 = fdopen(fd, "a")
     f2.seek(0, 2)
     f2.write("bar\nboo")
     f2.close()
     # don't close fd, will get a whining __del__
     f = self.file(self.temppath, "r")
     try:
         s = f.read()
         assert s == "foo\nbaz\nbar\nboo"
     finally:
         f.close()
Ejemplo n.º 22
0
 def fdopen_helper(self, *args):
     fd = os.open(test_support.TESTFN, os.O_RDONLY)
     fp2 = posix.fdopen(fd, *args)
     fp2.close()
Ejemplo n.º 23
0
"""Extended file operations available in POSIX.