Python io.UnsupportedOperation() Examples

The following are 30 code examples for showing how to use io.UnsupportedOperation(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module io , or try the search function .

Example 1
Project: clikit   Author: sdispater   File: stream_input_stream.py    License: MIT License 6 votes vote down vote up
def read(self, length):  # type: (int) -> str
        """
        Reads the given amount of characters from the stream.
        """
        if self.is_closed():
            raise io.UnsupportedOperation("Cannot read from a closed input.")

        try:
            data = self._stream.read(length)
        except EOFError:
            return ""

        if data:
            return data

        return "" 
Example 2
Project: script.module.inputstreamhelper   Author: emilsvennesson   File: arm_chromeos.py    License: MIT License 6 votes vote down vote up
def seek_stream(self, seek_pos):
        """Move position of bstream to seek_pos"""
        try:
            self.bstream[0].seek(seek_pos)
            self.bstream[1] = seek_pos
            return

        except UnsupportedOperation:
            chunksize = 4 * 1024**2

            if seek_pos >= self.bstream[1]:
                while seek_pos - self.bstream[1] > chunksize:
                    self.read_stream(chunksize)
                self.read_stream(seek_pos - self.bstream[1])
                return

            self.bstream[0].close()
            self.bstream[1] = 0
            self.bstream = self.get_bstream(self.imgpath)

            while seek_pos - self.bstream[1] > chunksize:
                self.read_stream(chunksize)
            self.read_stream(seek_pos - self.bstream[1])

            return 
Example 3
Project: jawfish   Author: war-and-code   File: utils.py    License: MIT License 6 votes vote down vote up
def super_len(o):
    if hasattr(o, '__len__'):
        return len(o)

    if hasattr(o, 'len'):
        return o.len

    if hasattr(o, 'fileno'):
        try:
            fileno = o.fileno()
        except io.UnsupportedOperation:
            pass
        else:
            return os.fstat(fileno).st_size

    if hasattr(o, 'getvalue'):
        # e.g. BytesIO, cStringIO.StringIO
        return len(o.getvalue()) 
Example 4
Project: qutebrowser   Author: qutebrowser   File: qtutils.py    License: GNU General Public License v3.0 6 votes vote down vote up
def seek(self, offset: int, whence: int = io.SEEK_SET) -> int:
        self._check_open()
        self._check_random()
        if whence == io.SEEK_SET:
            ok = self.dev.seek(offset)
        elif whence == io.SEEK_CUR:
            ok = self.dev.seek(self.tell() + offset)
        elif whence == io.SEEK_END:
            ok = self.dev.seek(len(self) + offset)
        else:
            raise io.UnsupportedOperation("whence = {} is not "
                                          "supported!".format(whence))
        if not ok:
            raise QtOSError(self.dev, msg="seek failed!")

        return self.dev.pos() 
Example 5
Project: linter-pylama   Author: AtomLinter   File: inirama.py    License: MIT License 6 votes vote down vote up
def read(self, *files, **params):
        """ Read and parse INI files.

        :param *files: Files for reading
        :param **params: Params for parsing

        Set `update=False` for prevent values redefinition.

        """
        for f in files:
            try:
                with io.open(f, encoding='utf-8') as ff:
                    NS_LOGGER.info('Read from `{0}`'.format(ff.name))
                    self.parse(ff.read(), **params)
            except (IOError, TypeError, SyntaxError, io.UnsupportedOperation):
                if not self.silent_read:
                    NS_LOGGER.error('Reading error `{0}`'.format(ff.name))
                    raise 
Example 6
Project: vulscan   Author: vulscanteam   File: utils.py    License: MIT License 6 votes vote down vote up
def super_len(o):
    if hasattr(o, '__len__'):
        return len(o)

    if hasattr(o, 'len'):
        return o.len

    if hasattr(o, 'fileno'):
        try:
            fileno = o.fileno()
        except io.UnsupportedOperation:
            pass
        else:
            return os.fstat(fileno).st_size

    if hasattr(o, 'getvalue'):
        # e.g. BytesIO, cStringIO.StringIO
        return len(o.getvalue()) 
Example 7
Project: GTDWeb   Author: lanbing510   File: base.py    License: GNU General Public License v2.0 6 votes vote down vote up
def chunks(self, chunk_size=None):
        """
        Read the file and yield chunks of ``chunk_size`` bytes (defaults to
        ``UploadedFile.DEFAULT_CHUNK_SIZE``).
        """
        if not chunk_size:
            chunk_size = self.DEFAULT_CHUNK_SIZE

        try:
            self.seek(0)
        except (AttributeError, UnsupportedOperation):
            pass

        while True:
            data = self.read(chunk_size)
            if not data:
                break
            yield data 
Example 8
Project: pledgeservice   Author: MayOneUS   File: utils.py    License: Apache License 2.0 6 votes vote down vote up
def super_len(o):
    if hasattr(o, '__len__'):
        return len(o)

    if hasattr(o, 'len'):
        return o.len

    if hasattr(o, 'fileno'):
        try:
            fileno = o.fileno()
        except io.UnsupportedOperation:
            pass
        else:
            return os.fstat(fileno).st_size

    if hasattr(o, 'getvalue'):
        # e.g. BytesIO, cStringIO.StringIO
        return len(o.getvalue()) 
Example 9
Project: pledgeservice   Author: MayOneUS   File: utils.py    License: Apache License 2.0 6 votes vote down vote up
def super_len(o):
    if hasattr(o, '__len__'):
        return len(o)

    if hasattr(o, 'len'):
        return o.len

    if hasattr(o, 'fileno'):
        try:
            fileno = o.fileno()
        except io.UnsupportedOperation:
            pass
        else:
            return os.fstat(fileno).st_size

    if hasattr(o, 'getvalue'):
        # e.g. BytesIO, cStringIO.StringIO
        return len(o.getvalue()) 
Example 10
Project: faces   Author: skarlekar   File: utils.py    License: GNU General Public License v2.0 6 votes vote down vote up
def super_len(o):
    if hasattr(o, '__len__'):
        return len(o)

    if hasattr(o, 'len'):
        return o.len

    if hasattr(o, 'fileno'):
        try:
            fileno = o.fileno()
        except io.UnsupportedOperation:
            pass
        else:
            return os.fstat(fileno).st_size

    if hasattr(o, 'getvalue'):
        # e.g. BytesIO, cStringIO.StringIO
        return len(o.getvalue()) 
Example 11
Project: faces   Author: skarlekar   File: utils.py    License: GNU General Public License v2.0 6 votes vote down vote up
def super_len(o):
    if hasattr(o, '__len__'):
        return len(o)

    if hasattr(o, 'len'):
        return o.len

    if hasattr(o, 'fileno'):
        try:
            fileno = o.fileno()
        except io.UnsupportedOperation:
            pass
        else:
            return os.fstat(fileno).st_size

    if hasattr(o, 'getvalue'):
        # e.g. BytesIO, cStringIO.StringIO
        return len(o.getvalue()) 
Example 12
Project: clikit   Author: sdispater   File: stream_output_stream.py    License: MIT License 5 votes vote down vote up
def write(self, string):  # type: (str) -> None
        """
        Writes a string to the stream.
        """
        if self.is_closed():
            raise io.UnsupportedOperation("Cannot write to a closed input.")

        self._stream.write(string)
        self._stream.flush() 
Example 13
Project: clikit   Author: sdispater   File: stream_output_stream.py    License: MIT License 5 votes vote down vote up
def flush(self):  # type: () -> None
        """
        Flushes the stream and forces all pending text to be written out.
        """
        if self.is_closed():
            raise io.UnsupportedOperation("Cannot write to a closed input.")

        self._stream.flush() 
Example 14
Project: clikit   Author: sdispater   File: stream_input_stream.py    License: MIT License 5 votes vote down vote up
def read_line(self, length=None):  # type: (Optional[int]) -> str
        """
        Reads a line from the stream.
        """
        if self.is_closed():
            raise io.UnsupportedOperation("Cannot read from a closed input.")

        try:
            return self._stream.readline(length) or ""
        except EOFError:
            return "" 
Example 15
Project: jawfish   Author: war-and-code   File: _io.py    License: MIT License 5 votes vote down vote up
def __new__(cls, *args, **kwargs):
        return open(*args, **kwargs)


# In normal operation, both `UnsupportedOperation`s should be bound to the
# same object. 
Example 16
Project: jawfish   Author: war-and-code   File: _io.py    License: MIT License 5 votes vote down vote up
def _unsupported(self, name):
        """Internal: raise an IOError exception for unsupported operations."""
        raise UnsupportedOperation("%s.%s() not supported" %
                                   (self.__class__.__name__, name))

    ### Positioning ### 
Example 17
Project: jawfish   Author: war-and-code   File: _io.py    License: MIT License 5 votes vote down vote up
def seekable(self):
        """Return a bool indicating whether object supports random access.

        If False, seek(), tell() and truncate() will raise UnsupportedOperation.
        This method may need to do a test seek().
        """
        return False 
Example 18
Project: jawfish   Author: war-and-code   File: _io.py    License: MIT License 5 votes vote down vote up
def readable(self):
        """Return a bool indicating whether object was opened for reading.

        If False, read() will raise UnsupportedOperation.
        """
        return False 
Example 19
Project: jawfish   Author: war-and-code   File: _io.py    License: MIT License 5 votes vote down vote up
def _checkReadable(self, msg=None):
        """Internal: raise UnsupportedOperation if file is not readable
        """
        if not self.readable():
            raise UnsupportedOperation("File or stream is not readable."
                                       if msg is None else msg) 
Example 20
Project: jawfish   Author: war-and-code   File: _io.py    License: MIT License 5 votes vote down vote up
def writable(self):
        """Return a bool indicating whether object was opened for writing.

        If False, write() and truncate() will raise UnsupportedOperation.
        """
        return False 
Example 21
Project: jawfish   Author: war-and-code   File: _io.py    License: MIT License 5 votes vote down vote up
def _checkWritable(self, msg=None):
        """Internal: raise UnsupportedOperation if file is not writable
        """
        if not self.writable():
            raise UnsupportedOperation("File or stream is not writable."
                                       if msg is None else msg) 
Example 22
Project: qutebrowser   Author: qutebrowser   File: test_qtutils.py    License: GNU General Public License v3.0 5 votes vote down vote up
def test_fileno(self, pyqiodev):
        with pytest.raises(io.UnsupportedOperation):
            pyqiodev.fileno() 
Example 23
Project: qutebrowser   Author: qutebrowser   File: test_qtutils.py    License: GNU General Public License v3.0 5 votes vote down vote up
def test_seek_unsupported(self, pyqiodev):
        """Test seeking with unsupported whence arguments."""
        # pylint: disable=no-member,useless-suppression
        if hasattr(os, 'SEEK_HOLE'):
            whence = os.SEEK_HOLE
        elif hasattr(os, 'SEEK_DATA'):
            whence = os.SEEK_DATA
        # pylint: enable=no-member,useless-suppression
        else:
            pytest.skip("Needs os.SEEK_HOLE or os.SEEK_DATA available.")
        pyqiodev.open(QIODevice.ReadOnly)
        with pytest.raises(io.UnsupportedOperation):
            pyqiodev.seek(0, whence) 
Example 24
Project: qutebrowser   Author: qutebrowser   File: test_qtutils.py    License: GNU General Public License v3.0 5 votes vote down vote up
def test_truncate(self, pyqiodev):
        with pytest.raises(io.UnsupportedOperation):
            pyqiodev.truncate() 
Example 25
Project: plugin.video.emby   Author: MediaBrowser   File: utils.py    License: GNU General Public License v3.0 5 votes vote down vote up
def super_len(o):
    total_length = 0
    current_position = 0

    if hasattr(o, '__len__'):
        total_length = len(o)

    elif hasattr(o, 'len'):
        total_length = o.len

    elif hasattr(o, 'getvalue'):
        # e.g. BytesIO, cStringIO.StringIO
        total_length = len(o.getvalue())

    elif hasattr(o, 'fileno'):
        try:
            fileno = o.fileno()
        except io.UnsupportedOperation:
            pass
        else:
            total_length = os.fstat(fileno).st_size

            # Having used fstat to determine the file length, we need to
            # confirm that this file was opened up in binary mode.
            if 'b' not in o.mode:
                warnings.warn((
                    "Requests has determined the content-length for this "
                    "request using the binary size of the file: however, the "
                    "file has been opened in text mode (i.e. without the 'b' "
                    "flag in the mode). This may lead to an incorrect "
                    "content-length. In Requests 3.0, support will be removed "
                    "for files in text mode."),
                    FileModeWarning
                )

    if hasattr(o, 'tell'):
        current_position = o.tell()

    return max(0, total_length - current_position) 
Example 26
Project: jbox   Author: jpush   File: utils.py    License: MIT License 5 votes vote down vote up
def super_len(o):
    total_length = 0
    current_position = 0

    if hasattr(o, '__len__'):
        total_length = len(o)

    elif hasattr(o, 'len'):
        total_length = o.len

    elif hasattr(o, 'getvalue'):
        # e.g. BytesIO, cStringIO.StringIO
        total_length = len(o.getvalue())

    elif hasattr(o, 'fileno'):
        try:
            fileno = o.fileno()
        except io.UnsupportedOperation:
            pass
        else:
            total_length = os.fstat(fileno).st_size

            # Having used fstat to determine the file length, we need to
            # confirm that this file was opened up in binary mode.
            if 'b' not in o.mode:
                warnings.warn((
                    "Requests has determined the content-length for this "
                    "request using the binary size of the file: however, the "
                    "file has been opened in text mode (i.e. without the 'b' "
                    "flag in the mode). This may lead to an incorrect "
                    "content-length. In Requests 3.0, support will be removed "
                    "for files in text mode."),
                    FileModeWarning
                )

    if hasattr(o, 'tell'):
        current_position = o.tell()

    return max(0, total_length - current_position) 
Example 27
Project: jbox   Author: jpush   File: util.py    License: MIT License 5 votes vote down vote up
def has_fileno(obj):
    if not hasattr(obj, "fileno"):
        return False

    # check BytesIO case and maybe others
    try:
        obj.fileno()
    except (AttributeError, IOError, io.UnsupportedOperation):
        return False

    return True 
Example 28
Project: omniduct   Author: airbnb   File: base.py    License: MIT License 5 votes vote down vote up
def read(self, size=-1):
        if not self.readable:
            raise io.UnsupportedOperation("File not open for reading.")
        return self.__io_buffer.read(size) 
Example 29
Project: omniduct   Author: airbnb   File: base.py    License: MIT License 5 votes vote down vote up
def readline(self, size=-1):
        if not self.readable:
            raise io.UnsupportedOperation("File not open for reading.")
        return self.__io_buffer.readline(size) 
Example 30
Project: omniduct   Author: airbnb   File: base.py    License: MIT License 5 votes vote down vote up
def readlines(self, hint=-1):
        if not self.readable:
            raise io.UnsupportedOperation("File not open for reading.")
        return self.__io_buffer.readlines(hint)