示例#1
0
    def test_write_raises_blocking_error(self):
        file_object = mock.Mock()
        file_object.write = mock.Mock(
            side_effect=io.BlockingIOError(None, None, 5))
        adapter = file_object_adapter.FileObjectAdapter(file_object)

        self.assertEqual(5, adapter.write(b'something'))
示例#2
0
 def write(self, b):
     self._write_stack.append(b[:])
     n = self._blocking_script.pop(0)
     if (n < 0):
         raise io.BlockingIOError(0, "test blocking", -n)
     else:
         return n
示例#3
0
  def _read(self, size: int, read1: bool) -> bytes:
    self._check_not_closed()

    if size is None:
      size = -1

    try:
      if read1:
        data = self._read1_with_tink_error(size)
      else:
        data = self._read_with_tink_error(size)

      if not data:
        raise io.BlockingIOError(errno.EAGAIN,
                                 'No data available at the moment.')
      else:
        self._bytes_read += len(data)
        return data
    except core.TinkError as e:
      # We are checking if the exception was raised because of C++
      # OUT_OF_RANGE status, which signals EOF.
      wrapped_e = e.args[0]
      if (isinstance(wrapped_e, tink_bindings.StatusNotOk) and
          wrapped_e.args[0] == tink_bindings.ErrorCode.OUT_OF_RANGE):
        return b''
      else:
        raise e
示例#4
0
    def test_read_raises_blocking_error(self):
        file_object = mock.Mock()
        file_object.read = mock.Mock(
            side_effect=io.BlockingIOError(None, None))
        adapter = file_object_adapter.FileObjectAdapter(file_object)

        self.assertEqual(adapter.read(10), b'')
示例#5
0
 def distinguish_empty(self, io_method, args):
     """Some io.SocketIO methods return the empty string both for
     disconnects and EAGAIN.  Attempt to distinguish between the two.
     """
     result = io_method(*args)
     if not result:
         try:
             if self.socket.recv(1, socket.MSG_PEEK):
                 # the socket has become readable in the time it took
                 # to get here.  raise a BlockingIOError so we can get
                 # selected as readable again
                 raise core.eagain()
             else:
                 # the socket was actually disconnected
                 raise core.EndOfStream
         except socket.error as e:
             if e.errno not in (errno.EAGAIN, errno.EWOULDBLOCK):
                 # if this isn't a blocking io errno, raise the legitimate
                 # exception
                 raise
             # a socket can't be reopened for reading if it's
             # closed/shutdown, so it's still unreadable.  raise a
             # BlockingIOError to communicate this upward
             raise io.BlockingIOError(*e.args)
     return result
    def _read(self, size: int, read1: bool) -> bytes:
        self._check_not_closed()

        if size is None:
            size = -1

        try:
            if read1:
                data = self._read1_with_tink_error(size)
            else:
                data = self._read_with_tink_error(size)

            if not data:
                raise io.BlockingIOError(errno.EAGAIN,
                                         'No data available at the moment.')
            else:
                self._bytes_read += len(data)
                return data
        except tink_error.TinkError as e:
            # We are checking if the exception was raised because of C++
            # OUT_OF_RANGE status, which signals EOF.
            if e.args[0].code == _OUT_OF_RANGE_ERROR_CODE:
                return b''
            else:
                raise e
 def read_pixel(self):
     # one pixel is four 16 bit values
     pixel = self.ser.read(8)
     if len(pixel) < 8:
         raise io.BlockingIOError()
     if self.output is not None:
         self.output.write(pixel)
     pixel = unpack("<4H", pixel)
     self.image_max = max(pixel[0], self.image_max)
     return pixel
示例#8
0
 def read(self, size: int = -1) -> bytes:
     if size > 0:
         self._state += 1
         if self._state > 10000000:
             raise AssertionError(
                 'too many read. Is there an infinite loop?')
         if self._state % 3 == 0:  # block on every third call.
             raise io.BlockingIOError(
                 errno.EAGAIN, 'write could not complete without blocking',
                 0)
         # read at most 5 bytes.
         return super(SlowBytesIO, self).read(min(size, 5))
     return super(SlowBytesIO, self).read(size)
示例#9
0
  def read_full_line(self):
    """Returns a full line if available.

    Here "full" line means a line which is terminated by os.linesep.
    If the available data does not contain os.linesep and the underlying stream
    has not reached to EOF yet, raises io.BlockingIOError.
    Note that even if the available data does not contain os.linesep, when EOF
    is found, the data will be returned.
    Empty string is a marker of EOF, as same as other file-like objects.

    Here is an example. Let 'abcde\nvwxyz' be available data:
    1) The first read_full_line() returns 'abcde\n'.
    2) The second read_full_line() raises io.BlockingIOError, because 'vwxyz'
       may be followed by more data.
    3) Then, let '12345\n67890' come to the stream.
    4) The next read_full_line() returns 'vwxyz12345\n'. Note that the leading
       data was pending 'vwxyz' at 2).
    5) Once the stream gets EOF, pending string '67890' is returned regardless
       whether it is terminated by os.linesep or not.
    6) Once the stream gets EOF, following read_full_line() invocation returns
       ''.
    """
    if self.closed:
      raise ValueError('I/O operation on closed file')

    if self._lines:
      return self._lines.pop(0)

    # Read available data from the file descriptor as much as possible.
    read_data, eof = _read_available_data(self._stream.fileno())
    if self._pending:
      read_data = self._pending + read_data
    split_lines = read_data.splitlines(True)  # Keep trailing EOL character.

    if not eof and read_data and not read_data.endswith(os.linesep):
      # More data will be followed for the last line. Keep it as pending.
      self._pending = split_lines.pop()
    else:
      self._pending = ''

    if not split_lines:
      if not eof:
        raise io.BlockingIOError(errno.EAGAIN, LineReader._EAGAIN_MESSAGE)
      return ''

    self._lines = split_lines[1:]
    return split_lines[0]
示例#10
0
文件: streams.py 项目: jayvdb/pyslet
    def flush(self):
        """flushes the Pipe

        The intention of flush to push any written data out to the
        destination, in this case the thread that is reading the data.

        In write-blocking mode this call will wait until the buffer is
        empty, though if the reader is idle for more than
        :attr:`timeout` seconds then it will raise IOError.

        In non-blocking mode it simple raises IOError with EWOULDBLOCK
        if the buffer is not empty.

        Given that flush is called automatically by :meth:`close` for
        classes that inherit from the base io classes our implementation
        of close discards the buffer rather than risk an exception."""
        if self.timeout is not None:
            tstart = time.time()
        with self.lock:
            blen = self.bsize - self.rpos
            while self.buffer:
                if self.wblocking:
                    if self.timeout is None:
                        twait = None
                    else:
                        new_blen = self.bsize - self.rpos
                        if new_blen < blen:
                            # making progress, restart the clock
                            blen = new_blen
                            tstart = time.time()
                        twait = (tstart + self.timeout) - time.time()
                        if twait < 0:
                            logging.warning("Pipe.flush timed out for %s",
                                            repr(self))
                            logging.debug("Pipe.flush found stuck data: %s",
                                          repr(self.buffer))
                            raise IOError(errno.ETIMEDOUT,
                                          os.strerror(errno.ETIMEDOUT),
                                          "pyslet.http.server.Pipe.flush")
                    logging.debug("Pipe.flush waiting for %s", repr(self))
                    self.lock.wait(twait)
                else:
                    raise io.BlockingIOError(
                        errno.EWOULDBLOCK,
                        "Pipe.flush write blocked on %s" % repr(self))
示例#11
0
    def write(self, b: bytes) -> int:
        """Write the given buffer to the stream.

    May use multiple calls to the underlying file object's write() method.

    Returns:
      The number of bytes written, which will always be the length of b in
      bytes.

    Raises:
      BlockingIOError: if the write could not be fully completed, with
        characters_written set to the number of bytes successfully written.
      TinkError: if there was a permanent error.

    Args:
      b: The buffer to write.
    """
        self._check_not_closed()

        if not isinstance(b, (bytes, memoryview, bytearray)):
            raise TypeError('a bytes-like object is required, not {}'.format(
                type(b).__name__))

        # One call to OutputStreamAdapter.write() may call next() multiple times
        # on the C++ EncryptingStream, but will perform a partial write if there is
        # a temporary write error. Permanent write errors will bubble up as
        # exceptions.
        written = self._output_stream_adapter.write(b)
        if written < 0:
            raise tink_error.TinkError('Number of written bytes was negative')

        self._bytes_written += written

        if written < len(b):
            raise io.BlockingIOError(
                errno.EAGAIN, 'Write could not complete without blocking.',
                written)
        elif written > len(b):
            raise tink_error.TinkError(
                'Number of written bytes was greater than length of bytes given'
            )

        return written
示例#12
0
def test_obtain_file_lock_when_locked(tmpdir):
    """
    Test obtain file lock when the file is locked
    """
    f_content = 'locked'
    f_name = 'lockfile'

    f_dir = tmpdir.mkdir('obtain_file_lock')
    f_dir_log = tmpdir.mkdir('obtain_file_lock_log')

    f_path = '{}/{}'.format(f_dir, f_name)

    with open(f_path, 'w+') as f_file:
        f_file.write(f_content)

    result = '{"wo_status": "FAIL", '
    result += '"wo_comment": '
    result += '"Lock could not be obtained on the file {}",'.format(f_name)
    result += ' "wo_newparams": '
    result += '{"SERVICEINSTANCEID": "12346", "process": "abc", "PROCESSINSTANCEID": "2345"}'
    result += '}'

    with patch('msa_sdk.util.constants.UBI_JENTREPRISE_DIRECTORY', f_dir):
        with patch('msa_sdk.util.fcntl.flock') as mock_flock:
            mock_flock.side_effect = io.BlockingIOError()
            with patch('msa_sdk.util.constants.PROCESS_LOGS_DIRECTORY',
                       f_dir_log):
                assert obtain_file_lock(
                    f_name, 'w+', {"SERVICEINSTANCEID": "12346",
                                   "process": "abc", "PROCESSINSTANCEID": "2345"}, 0.5, 2) == result

    log_file = '{}/process-12346.log'.format(f_dir_log)

    log_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")

    log_msg = '\n=== {} ===|{}|\n{}\n=== {} ===|{}--|'.format(
        log_time, 2345,
        ('{\n    "SERVICEINSTANCEID": "12346",'
         '\n    "process": "abc",'
         '\n    "PROCESSINSTANCEID": "2345"\n}'), log_time, 2345
    )

    assert open(log_file).read() == log_msg
示例#13
0
            def read(self, size=-1):
                result = None
                start = self.current_posn
                if not self.raise_exception:
                    if size < 0:
                        end = size
                        self.current_posn = len(self.string)
                    else:
                        end = (start + size)
                        self.current_posn = end
                    result = self.string[start:end]
                else:
                    if start > len(self.string):
                        result = b''
                    else:
                        self.raise_exception = False
                        raise io.BlockingIOError(1, 'Dummy error',
                                                 self.current_posn)

                self.raise_exception = not self.raise_exception
                return result
示例#14
0
 def _read(self, size):
     try:
         return self.channel.recv_stderr(size)
     except socket.timeout:
         raise io.BlockingIOError(errno.EAGAIN, os.strerror(errno.EAGAIN))