Python fcntl.F_GETFL Examples

The following are 30 code examples of fcntl.F_GETFL(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module fcntl , or try the search function .
Example #1
Source File: virtio_console_guest.py    From avocado-vt with GNU General Public License v2.0 6 votes vote down vote up
def blocking(self, port, mode=False):
        """
        Set port function mode blocking/nonblocking

        :param port: port to set mode
        :param mode: False to set nonblock mode, True for block mode
        """
        fd = self._open([port])[0]

        try:
            fl = fcntl.fcntl(fd, fcntl.F_GETFL)
            if not mode:
                fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
            else:
                fcntl.fcntl(fd, fcntl.F_SETFL, fl & ~os.O_NONBLOCK)

        except Exception as inst:
            print("FAIL: Setting (non)blocking mode: " + str(inst))
            return

        if mode:
            print("PASS: set to blocking mode")
        else:
            print("PASS: set to nonblocking mode") 
Example #2
Source File: io.py    From deepWordBug with Apache License 2.0 6 votes vote down vote up
def set_blocking(fd, blocking=True):
    """
    Set the given file-descriptor blocking or non-blocking.

    Returns the original blocking status.
    """

    old_flag = fcntl.fcntl(fd, fcntl.F_GETFL)

    if blocking:
        new_flag = old_flag & ~ os.O_NONBLOCK
    else:
        new_flag = old_flag | os.O_NONBLOCK

    fcntl.fcntl(fd, fcntl.F_SETFL, new_flag)

    return not bool(old_flag & os.O_NONBLOCK) 
Example #3
Source File: general.py    From scrounger with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, command):
        """
        Creates an interactive process to interact with out of a command

        :param str command: the command to be executed
        """

        from fcntl import fcntl, F_GETFL, F_SETFL
        from subprocess import Popen, PIPE
        import os

        self._command = command
        self._executable = command.split(" ", 1)[0]

        _Log.debug("Starting the interactive process: {}".format(command))

        self._process = Popen(command, shell=True, stdout=PIPE, stdin=PIPE,
            stderr=PIPE)
        fcntl(self._process.stdin, F_SETFL,
            fcntl(self._process.stdin, F_GETFL) | os.O_NONBLOCK)
        fcntl(self._process.stdout, F_SETFL,
            fcntl(self._process.stdout, F_GETFL) | os.O_NONBLOCK)
        fcntl(self._process.stderr, F_SETFL,
            fcntl(self._process.stderr, F_GETFL) | os.O_NONBLOCK) 
Example #4
Source File: TouchManager.py    From EM-uNetPi with MIT License 6 votes vote down vote up
def __init__(self, pScene):
        self.pScene = pScene
        #self.infile_path = "/dev/input/event" + (sys.argv[1] if len(sys.argv) > 1 else "0")
        self.infile_path = "/dev/input/event0"
        self.FORMAT = 'llHHI'
        self.EVENT_SIZE = struct.calcsize(self.FORMAT)
        self.lastPtX = 0
        self.lastPtY = 0
        self.rateX = float(480) / 3900
        self.rateY = float(320) / 3900
        self.sep = 0
        #print str(rateX)
        #print str(rateY)
        self.in_file = open(self.infile_path, "rb")
        flag = fcntl.fcntl(self.in_file, fcntl.F_GETFL)
        fcntl.fcntl(self.in_file, fcntl.F_SETFL, os.O_NONBLOCK) 
Example #5
Source File: supersocket.py    From scapy with GNU General Public License v2.0 6 votes vote down vote up
def set_nonblock(self, set_flag=True):
        """Set the non blocking flag on the socket"""

        # Get the current flags
        if self.fd_flags is None:
            try:
                self.fd_flags = fcntl.fcntl(self.ins, fcntl.F_GETFL)
            except IOError:
                warning("Cannot get flags on this file descriptor !")
                return

        # Set the non blocking flag
        if set_flag:
            new_fd_flags = self.fd_flags | os.O_NONBLOCK
        else:
            new_fd_flags = self.fd_flags & ~os.O_NONBLOCK

        try:
            fcntl.fcntl(self.ins, fcntl.F_SETFL, new_fd_flags)
            self.fd_flags = new_fd_flags
        except Exception:
            warning("Can't set flags on this file descriptor !") 
Example #6
Source File: readchar.py    From marker with MIT License 6 votes vote down vote up
def read_char_no_blocking():
    ''' Read a character in nonblocking mode, if no characters are present in the buffer, return an empty string '''
    fd = sys.stdin.fileno()
    old_settings = termios.tcgetattr(fd)
    old_flags = fcntl.fcntl(fd, fcntl.F_GETFL)
    try:
        tty.setraw(fd, termios.TCSADRAIN)
        fcntl.fcntl(fd, fcntl.F_SETFL, old_flags | os.O_NONBLOCK)
        return sys.stdin.read(1)
    except IOError as e:
        ErrorNumber = e[0]
        # IOError with ErrorNumber 11(35 in Mac)  is thrown when there is nothing to read(Resource temporarily unavailable)
        if (sys.platform.startswith("linux") and ErrorNumber != 11) or (sys.platform == "darwin" and ErrorNumber != 35):
            raise
        return ""
    finally:
        fcntl.fcntl(fd, fcntl.F_SETFL, old_flags)
        termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) 
Example #7
Source File: cli.py    From HyperGAN with MIT License 6 votes vote down vote up
def train(self):
        i=0
        if(self.args.ipython):
            import fcntl
            fd = sys.stdin.fileno()
            fl = fcntl.fcntl(fd, fcntl.F_GETFL)
            fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)

        while((i < self.total_steps or self.total_steps == -1) and not self.gan.destroy):
            i+=1
            start_time = time.time()
            self.step()
            GlobalViewer.tick()

            if (self.args.save_every != None and
                self.args.save_every != -1 and
                self.args.save_every > 0 and
                i % self.args.save_every == 0):
                print(" |= Saving network")
                self.gan.save(self.save_file)
            if self.args.ipython:
                self.check_stdin()
            end_time = time.time() 
Example #8
Source File: wurlitzer.py    From wurlitzer with MIT License 5 votes vote down vote up
def _setup_pipe(self, name):
        real_fd = getattr(sys, '__%s__' % name).fileno()
        save_fd = os.dup(real_fd)
        self._save_fds[name] = save_fd
        
        pipe_out, pipe_in = os.pipe()
        dup2(pipe_in, real_fd)
        os.close(pipe_in)
        self._real_fds[name] = real_fd
        
        # make pipe_out non-blocking
        flags = fcntl(pipe_out, F_GETFL)
        fcntl(pipe_out, F_SETFL, flags|os.O_NONBLOCK)
        return pipe_out 
Example #9
Source File: _compat.py    From pipenv with MIT License 5 votes vote down vote up
def set_binary_mode(f):
            try:
                fileno = f.fileno()
            except Exception:
                pass
            else:
                flags = fcntl.fcntl(fileno, fcntl.F_GETFL)
                fcntl.fcntl(fileno, fcntl.F_SETFL, flags & ~os.O_NONBLOCK)
            return f 
Example #10
Source File: loggingTest.py    From ufora with Apache License 2.0 5 votes vote down vote up
def setUpClass(cls):
        cls.readFd,  w = os.pipe()
        flags = fcntl.fcntl(cls.readFd, fcntl.F_GETFL, 0)
        fcntl.fcntl(cls.readFd, fcntl.F_SETFL, flags | os.O_NONBLOCK)
        os.dup2(w, 2) 
Example #11
Source File: Wanem.py    From EM-uNetPi with MIT License 5 votes vote down vote up
def pause(secs):
    fd = sys.stdin.fileno()

    oldterm = termios.tcgetattr(fd)
    newattr = termios.tcgetattr(fd)
    newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
    termios.tcsetattr(fd, termios.TCSANOW, newattr)

    oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
    fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)

    try:
        ctrlc = False
        paused = False
        t = secs / 0.1
        i = 0
        while i < t:
            if keypressed():
                paused = True
                break
            sleep(0.1)
            i += 1

        if paused:
            while True:
                if keypressed():
                    break
                sleep(0.1)
    except KeyboardInterrupt:
        ctrlc = True

    termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
    fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)
    if ctrlc:
        sys.exit(1) 
Example #12
Source File: sshmonitor.py    From SSHMonitor2.7 with GNU General Public License v3.0 5 votes vote down vote up
def process(self,filename):

        process = subprocess.Popen(
            self.tail_command + [filename], stdin=subprocess.PIPE,
            stdout=subprocess.PIPE, stderr=subprocess.PIPE
        )
    
        # set non-blocking mode for file
        function_control = fcntl.fcntl(process.stdout, fcntl.F_GETFL)
        fcntl.fcntl(process.stdout, fcntl.F_SETFL, function_control | os.O_NONBLOCK)
    
        function_control = fcntl.fcntl(process.stderr, fcntl.F_GETFL)
        fcntl.fcntl(process.stderr, fcntl.F_SETFL, function_control | os.O_NONBLOCK)
        
        return process 
Example #13
Source File: test_sendfdport.py    From ccs-twistedextensions with Apache License 2.0 5 votes vote down vote up
def isNonBlocking(skt):
    """
    Determine if the given socket is blocking or not.

    @param skt: a socket.
    @type skt: L{socket.socket}

    @return: L{True} if the socket is non-blocking, L{False} if the socket is
        blocking.
    @rtype: L{bool}
    """
    return bool(fcntl.fcntl(skt.fileno(), fcntl.F_GETFL) & os.O_NONBLOCK) 
Example #14
Source File: wurlitzer.py    From nbodykit with GNU General Public License v3.0 5 votes vote down vote up
def _setup_pipe(self, name):
        real_fd = getattr(sys, '__%s__' % name).fileno()
        save_fd = os.dup(real_fd)
        self._save_fds[name] = save_fd
        
        pipe_out, pipe_in = os.pipe()
        dup2(pipe_in, real_fd)
        os.close(pipe_in)
        self._real_fds[name] = real_fd
        
        # make pipe_out non-blocking
        flags = fcntl(pipe_out, F_GETFL)
        fcntl(pipe_out, F_SETFL, flags|os.O_NONBLOCK)
        return pipe_out 
Example #15
Source File: unix_events.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def _set_nonblocking(fd):
        flags = fcntl.fcntl(fd, fcntl.F_GETFL)
        flags = flags | os.O_NONBLOCK
        fcntl.fcntl(fd, fcntl.F_SETFL, flags) 
Example #16
Source File: paasta_deployd_steps.py    From paasta with Apache License 2.0 5 votes vote down vote up
def start_second_deployd(context):
    context.daemon1 = Popen("paasta-deployd", stderr=PIPE)
    output = context.daemon1.stderr.readline().decode("utf-8")
    fd = context.daemon1.stderr
    fl = fcntl.fcntl(fd, fcntl.F_GETFL)
    fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
    for i in range(0, 5):
        try:
            output = context.daemon1.stderr.readline().decode("utf-8")
            print(output.rstrip("\n"))
            assert "This node is elected as leader" not in output
        except IOError:
            pass
        time.sleep(1) 
Example #17
Source File: posix.py    From pySINDy with MIT License 5 votes vote down vote up
def _set_nonblocking(fd):
    flags = fcntl.fcntl(fd, fcntl.F_GETFL)
    fcntl.fcntl(fd, fcntl.F_SETFL, flags | os.O_NONBLOCK) 
Example #18
Source File: twisted_test.py    From pySINDy with MIT License 5 votes vote down vote up
def _set_nonblocking(self, fd):
        flags = fcntl.fcntl(fd, fcntl.F_GETFL)
        fcntl.fcntl(fd, fcntl.F_SETFL, flags | os.O_NONBLOCK) 
Example #19
Source File: hontel.py    From hontel with MIT License 5 votes vote down vote up
def session_start(self):
        self._log("SESSION_START")
        self.process = subprocess.Popen(SHELL, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, preexec_fn=os.setsid)

        flags = fcntl.fcntl(self.process.stdout, fcntl.F_GETFL)
        fcntl.fcntl(self.process.stdout, fcntl.F_SETFL, flags | os.O_NONBLOCK) 
Example #20
Source File: job_worker.py    From starthinker with Apache License 2.0 5 votes vote down vote up
def make_non_blocking(file_io):
  fd = file_io.fileno()
  fl = fcntl.fcntl(fd, fcntl.F_GETFL)
  fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK) 
Example #21
Source File: UserFile.py    From pycopia with Apache License 2.0 5 votes vote down vote up
def flag_string(fd):
    """flag_string(fd)
    where fd is an integer file descriptor of an open file. Returns the files open
    flags as a vertical bar (|) delimited string.
    """
    flags = fcntl.fcntl(fd, fcntl.F_GETFL)
    strlist = filter(None, map(lambda n: (flags & getattr(os, n)) and n, os.OLIST))
    # hack to accomodate the fact that O_RDONLY is not really a flag...
    if not (flags & os.ACCMODE):
        strlist.insert(0, "O_RDONLY")
    return "|".join(strlist)


# XXX still need to verify this or add more. 
Example #22
Source File: posix.py    From teleport with Apache License 2.0 5 votes vote down vote up
def _set_nonblocking(fd: int) -> None:
    flags = fcntl.fcntl(fd, fcntl.F_GETFL)
    fcntl.fcntl(fd, fcntl.F_SETFL, flags | os.O_NONBLOCK) 
Example #23
Source File: posix.py    From teleport with Apache License 2.0 5 votes vote down vote up
def _set_nonblocking(fd: int) -> None:
    flags = fcntl.fcntl(fd, fcntl.F_GETFL)
    fcntl.fcntl(fd, fcntl.F_SETFL, flags | os.O_NONBLOCK) 
Example #24
Source File: twisted_test.py    From teleport with Apache License 2.0 5 votes vote down vote up
def _set_nonblocking(self, fd):
        flags = fcntl.fcntl(fd, fcntl.F_GETFL)
        fcntl.fcntl(fd, fcntl.F_SETFL, flags | os.O_NONBLOCK) 
Example #25
Source File: posix.py    From teleport with Apache License 2.0 5 votes vote down vote up
def _set_nonblocking(fd):
    flags = fcntl.fcntl(fd, fcntl.F_GETFL)
    fcntl.fcntl(fd, fcntl.F_SETFL, flags | os.O_NONBLOCK) 
Example #26
Source File: subprocess.py    From satori with Apache License 2.0 5 votes vote down vote up
def _remove_nonblock_flag(self, fd):
            flags = fcntl.fcntl(fd, fcntl.F_GETFL) & (~os.O_NONBLOCK)
            fcntl.fcntl(fd, fcntl.F_SETFL, flags) 
Example #27
Source File: os.py    From satori with Apache License 2.0 5 votes vote down vote up
def make_nonblocking(fd):
        """Put the file descriptor *fd* into non-blocking mode if possible.

        :return: A boolean value that evaluates to True if successful."""
        flags = fcntl.fcntl(fd, fcntl.F_GETFL, 0)
        if not bool(flags & os.O_NONBLOCK):
            fcntl.fcntl(fd, fcntl.F_SETFL, flags | os.O_NONBLOCK)
            return True 
Example #28
Source File: test_fdesc.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def test_setBlocking(self):
        """
        L{fdesc.setBlocking} sets a file description to blocking.
        """
        r, w = os.pipe()
        self.addCleanup(os.close, r)
        self.addCleanup(os.close, w)
        fdesc.setNonBlocking(r)
        fdesc.setBlocking(r)
        self.assertFalse(fcntl.fcntl(r, fcntl.F_GETFL) & os.O_NONBLOCK) 
Example #29
Source File: test_fdesc.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def test_setNonBlocking(self):
        """
        L{fdesc.setNonBlocking} sets a file description to non-blocking.
        """
        r, w = os.pipe()
        self.addCleanup(os.close, r)
        self.addCleanup(os.close, w)
        self.assertFalse(fcntl.fcntl(r, fcntl.F_GETFL) & os.O_NONBLOCK)
        fdesc.setNonBlocking(r)
        self.assertTrue(fcntl.fcntl(r, fcntl.F_GETFL) & os.O_NONBLOCK) 
Example #30
Source File: pipe.py    From multibootusb with GNU General Public License v2.0 5 votes vote down vote up
def set_fd_status_flag(fd, flag):
    """Set a status flag on a file descriptor.

    ``fd`` is the file descriptor or file object, ``flag`` the flag as integer.

    """
    flags = fcntl.fcntl(fd, fcntl.F_GETFL, 0)
    fcntl.fcntl(fd, fcntl.F_SETFL, flags | flag)