示例#1
0
    def test_pipe2(self):
        self.assertRaises(TypeError, os.pipe2, "DEADBEEF")
        self.assertRaises(TypeError, os.pipe2, 0, 0)

        # try calling with flags = 0, like os.pipe()
        r, w = os.pipe2(0)
        os.close(r)
        os.close(w)

        # test flags
        r, w = os.pipe2(os.O_CLOEXEC | os.O_NONBLOCK)
        self.addCleanup(os.close, r)
        self.addCleanup(os.close, w)
        self.assertFalse(os.get_inheritable(r))
        self.assertFalse(os.get_inheritable(w))
        self.assertFalse(os.get_blocking(r))
        self.assertFalse(os.get_blocking(w))
        # try reading from an empty pipe: this should fail, not block
        self.assertRaises(OSError, os.read, r, 1)
        # try a write big enough to fill-up the pipe: this should either
        # fail or perform a partial write, not block
        try:
            os.write(w, b"x" * support.PIPE_MAX_SIZE)
        except OSError:
            pass
示例#2
0
文件: terminal.py 项目: qsantos/spyce
    def __enter__(self):
        # prepare standard file descriptors for raw manipulation
        self.was_blocking = os.get_blocking(0)
        os.set_blocking(0, False)
        try:
            self.terminal_attr_stdin = termios.tcgetattr(0)
            self.terminal_attr_stdout = termios.tcgetattr(1)
            self.terminal_attr_stderr = termios.tcgetattr(2)
            tty.setraw(0)
            tty.setraw(1)
            tty.setraw(2)
        except termios.error:  # probably redirected
            self.terminal_attr_stdin = None

        # redirect standard file descriptors to new PTY
        master, slave = pty.openpty()
        os.set_blocking(master, False)
        self.real_stdin = os.dup(0)
        self.real_stdout = os.dup(1)
        self.real_stderr = os.dup(2)
        os.close(0)
        os.close(1)
        os.close(2)
        os.dup2(slave, 0)
        os.dup2(slave, 1)
        os.dup2(slave, 2)
        os.close(slave)
        self.terminal_pipe = master

        # start REPL in separate thread
        threading.Thread(target=repl, args=(self,), daemon=True).start()

        return self
示例#3
0
def check_reopen(r1, w):
    try:
        print("Reopening read end")
        r2 = os.open("/proc/self/fd/{}".format(r1), os.O_RDONLY)

        print("r1 is {}, r2 is {}".format(r1, r2))

        print("checking they both can receive from w...")

        os.write(w, b"a")
        assert os.read(r1, 1) == b"a"

        os.write(w, b"b")
        assert os.read(r2, 1) == b"b"

        print("...ok")

        print("setting r2 to non-blocking")
        os.set_blocking(r2, False)

        print("os.get_blocking(r1) ==", os.get_blocking(r1))
        print("os.get_blocking(r2) ==", os.get_blocking(r2))

        # Check r2 is really truly non-blocking
        try:
            os.read(r2, 1)
        except BlockingIOError:
            print("r2 definitely seems to be in non-blocking mode")

        # Check that r1 is really truly still in blocking mode
        def sleep_then_write():
            time.sleep(1)
            os.write(w, b"c")
        threading.Thread(target=sleep_then_write, daemon=True).start()
        assert os.read(r1, 1) == b"c"
        print("r1 definitely seems to be in blocking mode")
    except Exception as exc:
        print("ERROR: {!r}".format(exc))
示例#4
0
文件: test_pty.py 项目: 1st1/cpython
    def test_basic(self):
        try:
            debug("Calling master_open()")
            master_fd, slave_name = pty.master_open()
            debug("Got master_fd '%d', slave_name '%s'" %
                  (master_fd, slave_name))
            debug("Calling slave_open(%r)" % (slave_name,))
            slave_fd = pty.slave_open(slave_name)
            debug("Got slave_fd '%d'" % slave_fd)
        except OSError:
            # " An optional feature could not be imported " ... ?
            raise unittest.SkipTest("Pseudo-terminals (seemingly) not functional.")

        self.assertTrue(os.isatty(slave_fd), 'slave_fd is not a tty')

        # Solaris requires reading the fd before anything is returned.
        # My guess is that since we open and close the slave fd
        # in master_open(), we need to read the EOF.

        # Ensure the fd is non-blocking in case there's nothing to read.
        blocking = os.get_blocking(master_fd)
        try:
            os.set_blocking(master_fd, False)
            try:
                s1 = os.read(master_fd, 1024)
                self.assertEqual(b'', s1)
            except OSError as e:
                if e.errno != errno.EAGAIN:
                    raise
        finally:
            # Restore the original flags.
            os.set_blocking(master_fd, blocking)

        debug("Writing to slave_fd")
        os.write(slave_fd, TEST_STRING_1)
        s1 = _readline(master_fd)
        self.assertEqual(b'I wish to buy a fish license.\n',
                         normalize_output(s1))

        debug("Writing chunked output")
        os.write(slave_fd, TEST_STRING_2[:5])
        os.write(slave_fd, TEST_STRING_2[5:])
        s2 = _readline(master_fd)
        self.assertEqual(b'For my pet fish, Eric.\n', normalize_output(s2))

        os.close(slave_fd)
        os.close(master_fd)
示例#5
0
文件: __init__.py 项目: casdr/ansible
def check_blocking_io():
    """Check stdin/stdout/stderr to make sure they are using blocking IO."""
    handles = []

    for handle in (sys.stdin, sys.stdout, sys.stderr):
        # noinspection PyBroadException
        try:
            fd = handle.fileno()
        except Exception:
            continue  # not a real file handle, such as during the import sanity test

        if not os.get_blocking(fd):
            handles.append(getattr(handle, 'name', None) or '#%s' % fd)

    if handles:
        raise SystemExit(
            'ERROR: Ansible requires blocking IO on stdin/stdout/stderr. '
            'Non-blocking file handles detected: %s' %
            ', '.join(_io for _io in handles))
示例#6
0
 def tx(self, bytes_in: bytes):
     frame = kissfix.FEND + b'\00' + kissfix.escape_special_codes(
         bytes_in) + kissfix.FEND
     try:
         os.write(self.control, frame)
     except BlockingIOError:
         logging.error(
             "PTY interface buffer is full. The connected application may have crashed or isn't reading fast enough. Data loss is likely. Alternatively you aren't using the PTY interface and should have used --no-pty. Clearing the buffer now so we can keep going"
         )
         blocking = os.get_blocking(
             self.user_port)  # remember what the state was before
         os.set_blocking(self.user_port, False)
         try:
             while 1:
                 os.read(self.user_port,
                         32)  # read off the buffer until we've cleared it
         except BlockingIOError:
             pass
         os.set_blocking(self.user_port,
                         blocking)  # restore the state after
示例#7
0
    def events(self):
        """
        Returns an iterable with currently pending events.

        Event processing should look like this::

            fd = open("/dev/input/event0", "rb")
            ctx = libevdev.Device(fd)

            while True:
                for e in ctx.events():
                    print(e):

        This function detects if the file descriptor is in blocking or
        non-blocking mode and adjusts its behavior accordingly. If the file
        descriptor is in nonblocking mode and no events are available, this
        function returns immediately. If the file descriptor is blocking,
        this function blocks if there are no events available.

        :returns: an iterable with the currently pending events
        """
        if self._libevdev.fd is None:
            return []

        if os.get_blocking(self._libevdev.fd.fileno()):
            flags = READ_FLAG_BLOCKING
        else:
            flags = READ_FLAG_NORMAL

        ev = self._libevdev.next_event(flags)
        while ev is not None:
            code = libevdev.evbit(ev.type, ev.code)
            yield InputEvent(code, ev.value, ev.sec, ev.usec)
            if code == libevdev.EV_SYN.SYN_DROPPED:
                raise EventsDroppedException()
            ev = self._libevdev.next_event(flags)
示例#8
0
 def test_basic(self):
     try:
         debug('Calling master_open()')
         master_fd, slave_name = pty.master_open()
         debug("Got master_fd '%d', slave_name '%s'" %
               (master_fd, slave_name))
         debug('Calling slave_open(%r)' % (slave_name, ))
         slave_fd = pty.slave_open(slave_name)
         debug("Got slave_fd '%d'" % slave_fd)
     except OSError:
         raise unittest.SkipTest(
             'Pseudo-terminals (seemingly) not functional.')
     self.assertTrue(os.isatty(slave_fd), 'slave_fd is not a tty')
     blocking = os.get_blocking(master_fd)
     try:
         os.set_blocking(master_fd, False)
         try:
             s1 = os.read(master_fd, 1024)
             self.assertEqual(b'', s1)
         except OSError as e:
             if e.errno != errno.EAGAIN:
                 raise
     finally:
         os.set_blocking(master_fd, blocking)
     debug('Writing to slave_fd')
     os.write(slave_fd, TEST_STRING_1)
     s1 = os.read(master_fd, 1024)
     self.assertEqual(b'I wish to buy a fish license.\n',
                      normalize_output(s1))
     debug('Writing chunked output')
     os.write(slave_fd, TEST_STRING_2[:5])
     os.write(slave_fd, TEST_STRING_2[5:])
     s2 = os.read(master_fd, 1024)
     self.assertEqual(b'For my pet fish, Eric.\n', normalize_output(s2))
     os.close(slave_fd)
     os.close(master_fd)
示例#9
0
        os.write(wfd, CONTENT2)
        assert os.read(rfd, len(CONTENT2)) == CONTENT2
        assert not os.get_inheritable(rfd)
        assert not os.get_inheritable(wfd)
        os.set_inheritable(rfd, True)
        os.set_inheritable(wfd, True)
        assert os.get_inheritable(rfd)
        assert os.get_inheritable(wfd)
        os.set_inheritable(rfd, True)
        os.set_inheritable(wfd, True)
        os.set_inheritable(rfd, True)
        os.set_inheritable(wfd, True)
        assert os.get_inheritable(rfd)
        assert os.get_inheritable(wfd)

        assert os.get_blocking(rfd)
        assert os.get_blocking(wfd)
        os.set_blocking(rfd, False)
        os.set_blocking(wfd, False)
        assert not os.get_blocking(rfd)
        assert not os.get_blocking(wfd)
        os.set_blocking(rfd, True)
        os.set_blocking(wfd, True)
        os.set_blocking(rfd, True)
        os.set_blocking(wfd, True)
        assert os.get_blocking(rfd)
        assert os.get_blocking(wfd)
    finally:
        os.close(rfd)
        os.close(wfd)
import os
import platform
import sys

print(sys.gettrace())
print(os.getcwd(), os.get_blocking(1), os.get_exec_path(),
      os.get_inheritable(1))

print(os.get_terminal_size())
print("The code is running from : " + os.getcwd())

print("The credention " + str(os.geteuid()))
print("The os use groups are " + str(os.getgroups()))
print("The average system load information  " + str(os.getloadavg()))
print("Get os login " + os.getlogin() + " \n The p_id: " + str(os.getpgid(1)) +
      "\n the p_group: " + str(os.getpgrp()))
print("\n os p_id :" + str(os.getpid()) + "\n os_pp_id :" + str(os.getppid()))
print("\nvgroup id" + str(os.getresgid()) + "\nuser_id " + str(os.getresuid()))
print("\n " + str(os.getsid(1)) + "\n" + str(os.getuid()))
print("cpu count :" + str(os.cpu_count()))

print("\n\n\n \t\t<--- SYSTEM INFORMATION ---> \n\n\n")
print("" + str(platform.uname()))
print("With processor " + platform.processor() + "The machine " +
      platform.machine() + " run in " + platform.node() +
      "node is connected in " + str(platform.mac_ver()))
print("" + str(platform.java_ver()))
print("python version " + str(platform.python_version_tuple()))
示例#11
0
        threading.Thread(target=sleep_then_write, daemon=True).start()
        assert os.read(r1, 1) == b"c"
        print("r1 definitely seems to be in blocking mode")
    except Exception as exc:
        print("ERROR: {!r}".format(exc))


print("-- testing anonymous pipe --")
check_reopen(*os.pipe())

print("-- testing FIFO --")
with tempfile.TemporaryDirectory() as tmpdir:
    fifo = tmpdir + "/" + "myfifo"
    os.mkfifo(fifo)
    # "A process can open a FIFO in nonblocking mode.  In this case, opening
    # for read-only will succeed even if no-one has opened on the write side
    # yet and opening for write-only will fail with ENXIO (no such device or
    # address) unless the other end has already been opened." -- Linux fifo(7)
    r = os.open(fifo, os.O_RDONLY | os.O_NONBLOCK)
    assert not os.get_blocking(r)
    os.set_blocking(r, True)
    assert os.get_blocking(r)
    w = os.open(fifo, os.O_WRONLY)
    check_reopen(r, w)

print("-- testing socketpair --")
import socket
rs, ws = socket.socketpair()
check_reopen(rs.fileno(), ws.fileno())

示例#12
0
    def test_openpty(self):
        try:
            mode = tty.tcgetattr(pty.STDIN_FILENO)
        except tty.error:
            # not a tty or bad/closed fd
            debug("tty.tcgetattr(pty.STDIN_FILENO) failed")
            mode = None

        new_stdin_winsz = None
        if self.stdin_rows != None and self.stdin_cols != None:
            try:
                debug("Setting pty.STDIN_FILENO window size")
                # Set number of columns and rows to be the
                # floors of 1/5 of respective original values
                target_stdin_winsz = struct.pack("HHHH", self.stdin_rows // 5,
                                                 self.stdin_cols // 5, 0, 0)
                _set_term_winsz(pty.STDIN_FILENO, target_stdin_winsz)

                # Were we able to set the window size
                # of pty.STDIN_FILENO successfully?
                new_stdin_winsz = _get_term_winsz(pty.STDIN_FILENO)
                self.assertEqual(new_stdin_winsz, target_stdin_winsz,
                                 "pty.STDIN_FILENO window size unchanged")
            except OSError:
                warnings.warn("Failed to set pty.STDIN_FILENO window size")
                pass

        try:
            debug("Calling pty.openpty()")
            try:
                master_fd, slave_fd = pty.openpty(mode, new_stdin_winsz)
            except TypeError:
                master_fd, slave_fd = pty.openpty()
            debug(f"Got master_fd '{master_fd}', slave_fd '{slave_fd}'")
        except OSError:
            # " An optional feature could not be imported " ... ?
            raise unittest.SkipTest(
                "Pseudo-terminals (seemingly) not functional.")

        self.assertTrue(os.isatty(slave_fd), "slave_fd is not a tty")

        if mode:
            self.assertEqual(tty.tcgetattr(slave_fd), mode,
                             "openpty() failed to set slave termios")
        if new_stdin_winsz:
            self.assertEqual(_get_term_winsz(slave_fd), new_stdin_winsz,
                             "openpty() failed to set slave window size")

        # Solaris requires reading the fd before anything is returned.
        # My guess is that since we open and close the slave fd
        # in master_open(), we need to read the EOF.
        #
        # NOTE: the above comment is from an older version of the test;
        # master_open() is not being used anymore.

        # Ensure the fd is non-blocking in case there's nothing to read.
        blocking = os.get_blocking(master_fd)
        try:
            os.set_blocking(master_fd, False)
            try:
                s1 = os.read(master_fd, 1024)
                self.assertEqual(b'', s1)
            except OSError as e:
                if e.errno != errno.EAGAIN:
                    raise
        finally:
            # Restore the original flags.
            os.set_blocking(master_fd, blocking)

        debug("Writing to slave_fd")
        os.write(slave_fd, TEST_STRING_1)
        s1 = _readline(master_fd)
        self.assertEqual(b'I wish to buy a fish license.\n',
                         normalize_output(s1))

        debug("Writing chunked output")
        os.write(slave_fd, TEST_STRING_2[:5])
        os.write(slave_fd, TEST_STRING_2[5:])
        s2 = _readline(master_fd)
        self.assertEqual(b'For my pet fish, Eric.\n', normalize_output(s2))

        os.close(slave_fd)
        # closing master_fd can raise a SIGHUP if the process is
        # the session leader: we installed a SIGHUP signal handler
        # to ignore this signal.
        os.close(master_fd)
示例#13
0
文件: test_pty.py 项目: 8vasu/pypty2
    def test_openpty(self):
        try:
            mode = tty.tcgetattr(pty.STDIN_FILENO)
        except tty.error:
            # not a tty or bad/closed fd
            debug("tty.tcgetattr(pty.STDIN_FILENO) failed")
            mode = None

        new_stdin_winsz = None
        if self.old_stdin_winsz != None:
            try:
                # Modify pty.STDIN_FILENO window size; we need to
                # check if pty.openpty() is able to set pty slave
                # window size accordingly.
                debug("setting pty.STDIN_FILENO window size")
                debug(f"original winsize: {self.old_stdin_winsz}")
                target_stdin_winsz = tty.winsize()
                target_stdin_winsz.ws_row = self.old_stdin_winsz.ws_row + 1
                target_stdin_winsz.ws_col = self.old_stdin_winsz.ws_col + 1
                target_stdin_winsz.ws_xpixel = self.old_stdin_winsz.ws_xpixel + 1
                target_stdin_winsz.ws_ypixel = self.old_stdin_winsz.ws_ypixel + 1
                debug(f"target winsize: {target_stdin_winsz}")
                target_stdin_winsz.tcsetwinsize(pty.STDIN_FILENO)

                # Were we able to set the window size of
                # pty.STDIN_FILENO successfully?
                new_stdin_winsz = tty.winsize(fd=pty.STDIN_FILENO)
                self.assertEqual(new_stdin_winsz, target_stdin_winsz,
                                 "pty.STDIN_FILENO window size unchanged")
            except tty.error:
                warnings.warn("failed to set pty.STDIN_FILENO window size")

        try:
            debug("calling pty.openpty()")
            try:
                master_fd, slave_fd = pty.openpty(mode, new_stdin_winsz)
            except TypeError:
                master_fd, slave_fd = pty.openpty()
            debug(f"got master_fd '{master_fd}', slave_fd '{slave_fd}'")
        except OSError:
            # " An optional feature could not be imported " ... ?
            raise unittest.SkipTest(
                "pseudo-terminals (seemingly) not functional")

        self.assertTrue(os.isatty(slave_fd), "slave_fd is not a tty")

        if mode:
            self.assertEqual(tty.tcgetattr(slave_fd), mode,
                             "openpty() failed to set slave termios")
        if new_stdin_winsz:
            self.assertEqual(tty.winsize(fd=slave_fd), new_stdin_winsz,
                             "openpty() failed to set slave window size")

        # Solaris requires reading the fd before anything is returned.
        # My guess is that since we open and close the slave fd
        # in master_open(), we need to read the EOF.
        #
        # NOTE: the above comment is from an older version of the test;
        # master_open() is not being used anymore.

        # Ensure the fd is non-blocking in case there's nothing to read.
        blocking = os.get_blocking(master_fd)
        try:
            os.set_blocking(master_fd, False)
            try:
                s1 = os.read(master_fd, 1024)
                self.assertEqual(b'', s1)
            except OSError as e:
                if e.errno != errno.EAGAIN:
                    raise
        finally:
            # Restore the original flags.
            os.set_blocking(master_fd, blocking)

        debug("writing to slave_fd")
        os.write(slave_fd, TEST_STRING_1)
        s1 = _readline(master_fd)
        self.assertEqual(b'I wish to buy a fish license.\n',
                         normalize_output(s1))

        debug("writing chunked output")
        os.write(slave_fd, TEST_STRING_2[:5])
        os.write(slave_fd, TEST_STRING_2[5:])
        s2 = _readline(master_fd)
        self.assertEqual(b'For my pet fish, Eric.\n', normalize_output(s2))

        os.close(slave_fd)
        # closing master_fd can raise a SIGHUP if the process is
        # the session leader: we installed a SIGHUP signal handler
        # to ignore this signal.
        os.close(master_fd)
示例#14
0
        threading.Thread(target=sleep_then_write, daemon=True).start()
        assert os.read(r1, 1) == b"c"
        print("r1 definitely seems to be in blocking mode")
    except Exception as exc:
        print("ERROR: {!r}".format(exc))


print("-- testing anonymous pipe --")
check_reopen(*os.pipe())

print("-- testing FIFO --")
with tempfile.TemporaryDirectory() as tmpdir:
    fifo = tmpdir + "/" + "myfifo"
    os.mkfifo(fifo)
    # "A process can open a FIFO in nonblocking mode.  In this case, opening
    # for read-only will succeed even if no-one has opened on the write side
    # yet and opening for write-only will fail with ENXIO (no such device or
    # address) unless the other end has already been opened." -- Linux fifo(7)
    r = os.open(fifo, os.O_RDONLY | os.O_NONBLOCK)
    assert not os.get_blocking(r)
    os.set_blocking(r, True)
    assert os.get_blocking(r)
    w = os.open(fifo, os.O_WRONLY)
    check_reopen(r, w)

print("-- testing socketpair --")
import socket
rs, ws = socket.socketpair()
check_reopen(rs.fileno(), ws.fileno())
示例#15
0
 def test_change_blocking(self):
     self.assertTrue(os.get_blocking(self.fd))
     os.set_blocking(self.fd, False)
     self.assertFalse(os.get_blocking(self.fd))
示例#16
0
 def test_get_blocking(self):
     self.assertTrue(os.get_blocking(self.fd))
示例#17
0
    def test_openpty(self):
        try:
            mode = tty.tcgetattr(pty.STDIN_FILENO)
        except tty.error:
            # not a tty or bad/closed fd
            debug("tty.tcgetattr(pty.STDIN_FILENO) failed")
            mode = None

        new_stdin_winsz = None
        if self.stdin_rows != None and self.stdin_cols != None:
            try:
                # Modify pty.STDIN_FILENO window size; we need to
                # check if pty.openpty() is able to set pty slave
                # window size accordingly.
                debug("Setting pty.STDIN_FILENO window size")
                debug(
                    f"original size: (rows={self.stdin_rows}, cols={self.stdin_cols})"
                )
                target_stdin_rows = self.stdin_rows + 1
                target_stdin_cols = self.stdin_cols + 1
                debug(
                    f"target size: (rows={target_stdin_rows}, cols={target_stdin_cols})"
                )
                target_stdin_winsz = struct.pack("HHHH", target_stdin_rows,
                                                 target_stdin_cols, 0, 0)
                _set_term_winsz(pty.STDIN_FILENO, target_stdin_winsz)

                # Were we able to set the window size
                # of pty.STDIN_FILENO successfully?
                new_stdin_winsz = _get_term_winsz(pty.STDIN_FILENO)
                self.assertEqual(new_stdin_winsz, target_stdin_winsz,
                                 "pty.STDIN_FILENO window size unchanged")
            except OSError:
                warnings.warn("Failed to set pty.STDIN_FILENO window size")
                pass

        try:
            debug("Calling pty.openpty()")
            try:
                master_fd, slave_fd = pty.openpty(mode, new_stdin_winsz)
            except TypeError:
                master_fd, slave_fd = pty.openpty()
            debug(f"Got master_fd '{master_fd}', slave_fd '{slave_fd}'")
        except OSError:
            # " An optional feature could not be imported " ... ?
            raise unittest.SkipTest(
                "Pseudo-terminals (seemingly) not functional.")

        # closing master_fd can raise a SIGHUP if the process is
        # the session leader: we installed a SIGHUP signal handler
        # to ignore this signal.
        self.addCleanup(os.close, master_fd)
        self.addCleanup(os.close, slave_fd)

        self.assertTrue(os.isatty(slave_fd), "slave_fd is not a tty")

        if mode:
            self.assertEqual(tty.tcgetattr(slave_fd), mode,
                             "openpty() failed to set slave termios")
        if new_stdin_winsz:
            self.assertEqual(_get_term_winsz(slave_fd), new_stdin_winsz,
                             "openpty() failed to set slave window size")

        # Ensure the fd is non-blocking in case there's nothing to read.
        blocking = os.get_blocking(master_fd)
        try:
            os.set_blocking(master_fd, False)
            try:
                s1 = os.read(master_fd, 1024)
                self.assertEqual(b'', s1)
            except OSError as e:
                if e.errno != errno.EAGAIN:
                    raise
        finally:
            # Restore the original flags.
            os.set_blocking(master_fd, blocking)

        debug("Writing to slave_fd")
        os.write(slave_fd, TEST_STRING_1)
        s1 = _readline(master_fd)
        self.assertEqual(b'I wish to buy a fish license.\n',
                         normalize_output(s1))

        debug("Writing chunked output")
        os.write(slave_fd, TEST_STRING_2[:5])
        os.write(slave_fd, TEST_STRING_2[5:])
        s2 = _readline(master_fd)
        self.assertEqual(b'For my pet fish, Eric.\n', normalize_output(s2))
示例#18
0
 def _ensure_blocking(self, a: ClassUnderTest) -> None:
     try:
         if not os.get_blocking(a.fileno()):
             self.skipTest("Test only applies to blocking streams")
     except OSError:
         pass