當前位置: 首頁>>代碼示例>>Python>>正文


Python mmap.error方法代碼示例

本文整理匯總了Python中mmap.error方法的典型用法代碼示例。如果您正苦於以下問題:Python mmap.error方法的具體用法?Python mmap.error怎麽用?Python mmap.error使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在mmap的用法示例。


在下文中一共展示了mmap.error方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_constructor_bad_file

# 需要導入模塊: import mmap [as 別名]
# 或者: from mmap import error [as 別名]
def test_constructor_bad_file(self, mmap_file):
        non_file = StringIO('I am not a file')
        non_file.fileno = lambda: -1

        # the error raised is different on Windows
        if is_platform_windows():
            msg = "The parameter is incorrect"
            err = OSError
        else:
            msg = "[Errno 22]"
            err = mmap.error

        with pytest.raises(err, match=msg):
            icom.MMapWrapper(non_file)

        target = open(mmap_file, 'r')
        target.close()

        msg = "I/O operation on closed file"
        with pytest.raises(ValueError, match=msg):
            icom.MMapWrapper(target) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:23,代碼來源:test_common.py

示例2: get_size

# 需要導入模塊: import mmap [as 別名]
# 或者: from mmap import error [as 別名]
def get_size(fileobj):
    """Returns the size of the file.
    The position when passed in will be preserved if no error occurs.

    Args:
        fileobj (fileobj)
    Returns:
        int: The size of the file
    Raises:
        IOError
    """

    old_pos = fileobj.tell()
    try:
        fileobj.seek(0, 2)
        return fileobj.tell()
    finally:
        fileobj.seek(old_pos, 0) 
開發者ID:bugatsinho,項目名稱:bugatsinho.github.io,代碼行數:20,代碼來源:_util.py

示例3: read_full

# 需要導入模塊: import mmap [as 別名]
# 或者: from mmap import error [as 別名]
def read_full(fileobj, size):
    """Like fileobj.read but raises IOError if not all requested data is
    returned.

    If you want to distinguish IOError and the EOS case, better handle
    the error yourself instead of using this.

    Args:
        fileobj (fileobj)
        size (int): amount of bytes to read
    Raises:
        IOError: In case read fails or not enough data is read
    """

    if size < 0:
        raise ValueError("size must not be negative")

    data = fileobj.read(size)
    if len(data) != size:
        raise IOError
    return data 
開發者ID:bugatsinho,項目名稱:bugatsinho.github.io,代碼行數:23,代碼來源:_util.py

示例4: test_constructor_bad_file

# 需要導入模塊: import mmap [as 別名]
# 或者: from mmap import error [as 別名]
def test_constructor_bad_file(self, mmap_file):
        non_file = StringIO('I am not a file')
        non_file.fileno = lambda: -1

        # the error raised is different on Windows
        if is_platform_windows():
            msg = "The parameter is incorrect"
            err = OSError
        else:
            msg = "[Errno 22]"
            err = mmap.error

        tm.assert_raises_regex(err, msg, common.MMapWrapper, non_file)

        target = open(mmap_file, 'r')
        target.close()

        msg = "I/O operation on closed file"
        tm.assert_raises_regex(
            ValueError, msg, common.MMapWrapper, target) 
開發者ID:birforce,項目名稱:vnpy_crypto,代碼行數:22,代碼來源:test_common.py

示例5: test_constructor_bad_file

# 需要導入模塊: import mmap [as 別名]
# 或者: from mmap import error [as 別名]
def test_constructor_bad_file(self):
        non_file = StringIO('I am not a file')
        non_file.fileno = lambda: -1

        # the error raised is different on Windows
        if is_platform_windows():
            msg = "The parameter is incorrect"
            err = OSError
        else:
            msg = "[Errno 22]"
            err = mmap.error

        tm.assert_raises_regex(err, msg, common.MMapWrapper, non_file)

        target = open(self.mmap_file, 'r')
        target.close()

        msg = "I/O operation on closed file"
        tm.assert_raises_regex(
            ValueError, msg, common.MMapWrapper, target) 
開發者ID:securityclippy,項目名稱:elasticintel,代碼行數:22,代碼來源:test_common.py

示例6: insert_bytes

# 需要導入模塊: import mmap [as 別名]
# 或者: from mmap import error [as 別名]
def insert_bytes(fobj, size, offset, BUFFER_SIZE=2 ** 16):
    """Insert size bytes of empty space starting at offset.

    fobj must be an open file object, open rb+ or
    equivalent. Mutagen tries to use mmap to resize the file, but
    falls back to a significantly slower method if mmap fails.

    Args:
        fobj (fileobj)
        size (int): The amount of space to insert
        offset (int): The offset at which to insert the space
    Raises:
        IOError
    """

    if size < 0 or offset < 0:
        raise ValueError

    fobj.seek(0, 2)
    filesize = fobj.tell()
    movesize = filesize - offset

    if movesize < 0:
        raise ValueError

    resize_file(fobj, size, BUFFER_SIZE)

    if mmap is not None:
        try:
            mmap_move(fobj, offset + size, offset, movesize)
        except mmap.error:
            fallback_move(fobj, offset + size, offset, movesize, BUFFER_SIZE)
    else:
        fallback_move(fobj, offset + size, offset, movesize, BUFFER_SIZE) 
開發者ID:bugatsinho,項目名稱:bugatsinho.github.io,代碼行數:36,代碼來源:_util.py

示例7: delete_bytes

# 需要導入模塊: import mmap [as 別名]
# 或者: from mmap import error [as 別名]
def delete_bytes(fobj, size, offset, BUFFER_SIZE=2 ** 16):
    """Delete size bytes of empty space starting at offset.

    fobj must be an open file object, open rb+ or
    equivalent. Mutagen tries to use mmap to resize the file, but
    falls back to a significantly slower method if mmap fails.

    Args:
        fobj (fileobj)
        size (int): The amount of space to delete
        offset (int): The start of the space to delete
    Raises:
        IOError
    """

    if size < 0 or offset < 0:
        raise ValueError

    fobj.seek(0, 2)
    filesize = fobj.tell()
    movesize = filesize - offset - size

    if movesize < 0:
        raise ValueError

    if mmap is not None:
        try:
            mmap_move(fobj, offset, offset + size, movesize)
        except mmap.error:
            fallback_move(fobj, offset, offset + size, movesize, BUFFER_SIZE)
    else:
        fallback_move(fobj, offset, offset + size, movesize, BUFFER_SIZE)

    resize_file(fobj, -size, BUFFER_SIZE) 
開發者ID:bugatsinho,項目名稱:bugatsinho.github.io,代碼行數:36,代碼來源:_util.py

示例8: mmapwrapper

# 需要導入模塊: import mmap [as 別名]
# 或者: from mmap import error [as 別名]
def mmapwrapper(*args, **kwargs):
    """
    Python's mmap call sucks and ommitted the "offset" argument for no
    discernable reason. Replace this with a mmap module that has offset.
    """

    offset = kwargs.get('offset', None)
    if offset in [None, 0]:
        if 'offset' in kwargs:
            del kwargs['offset']
    else:
        raise mmap.error("mmap: Python sucks and does not support offset.")
    return mmap.mmap(*args, **kwargs) 
開發者ID:apple,項目名稱:ccs-calendarserver,代碼行數:15,代碼來源:stream.py

示例9: connectionMade

# 需要導入模塊: import mmap [as 別名]
# 或者: from mmap import error [as 別名]
def connectionMade(self):
        p = StreamProducer(self.inputStream)
        # if the process stopped reading from the input stream,
        # this is not an error condition, so it oughtn't result
        # in a ConnectionLost() from the input stream:
        p.stopProducing = lambda err = None: StreamProducer.stopProducing(p, err)

        d = p.beginProducing(self.transport)
        d.addCallbacks(lambda _: self.transport.closeStdin(),
                       self._inputError) 
開發者ID:apple,項目名稱:ccs-calendarserver,代碼行數:12,代碼來源:stream.py

示例10: mmap_move

# 需要導入模塊: import mmap [as 別名]
# 或者: from mmap import error [as 別名]
def mmap_move(fileobj, dest, src, count):
    """Mmaps the file object if possible and moves 'count' data
    from 'src' to 'dest'. All data has to be inside the file size
    (enlarging the file through this function isn't possible)

    Will adjust the file offset.

    Args:
        fileobj (fileobj)
        dest (int): The destination offset
        src (int): The source offset
        count (int) The amount of data to move
    Raises:
        mmap.error: In case move failed
        IOError: In case an operation on the fileobj fails
        ValueError: In case invalid parameters were given
    """

    assert mmap is not None, "no mmap support"

    if dest < 0 or src < 0 or count < 0:
        raise ValueError("Invalid parameters")

    try:
        fileno = fileobj.fileno()
    except (AttributeError, IOError):
        raise mmap.error(
            "File object does not expose/support a file descriptor")

    fileobj.seek(0, 2)
    filesize = fileobj.tell()
    length = max(dest, src) + count

    if length > filesize:
        raise ValueError("Not in file size boundary")

    offset = ((min(dest, src) // mmap.ALLOCATIONGRANULARITY) *
              mmap.ALLOCATIONGRANULARITY)
    assert dest >= offset
    assert src >= offset
    assert offset % mmap.ALLOCATIONGRANULARITY == 0

    # Windows doesn't handle empty mappings, add a fast path here instead
    if count == 0:
        return

    # fast path
    if src == dest:
        return

    fileobj.flush()
    file_map = mmap.mmap(fileno, length - offset, offset=offset)
    try:
        file_map.move(dest - offset, src - offset, count)
    finally:
        file_map.close() 
開發者ID:bugatsinho,項目名稱:bugatsinho.github.io,代碼行數:58,代碼來源:_util.py

示例11: read

# 需要導入模塊: import mmap [as 別名]
# 或者: from mmap import error [as 別名]
def read(self, sendfile=False):
        if self.f is None:
            return None

        length = self.length
        if length == 0:
            self.f = None
            return None

        # if sendfile and length > SENDFILE_THRESHOLD:
        #    # XXX: Yay using non-existent sendfile support!
        #    # FIXME: if we return a SendfileBuffer, and then sendfile
        #    #        fails, then what? Or, what if file is too short?
        #    readSize = min(length, SENDFILE_LIMIT)
        #    res = SendfileBuffer(self.f, self.start, readSize)
        #    self.length -= readSize
        #    self.start += readSize
        #    return res

        if self.useMMap and length > MMAP_THRESHOLD:
            readSize = min(length, MMAP_LIMIT)
            try:
                res = mmapwrapper(self.f.fileno(), readSize,
                                  access=mmap.ACCESS_READ, offset=self.start)
                # madvise(res, MADV_SEQUENTIAL)
                self.length -= readSize
                self.start += readSize
                return res
            except mmap.error:
                pass

        # Fall back to standard read.
        readSize = min(length, self.CHUNK_SIZE)

        self.f.seek(self.start)
        b = self.f.read(readSize)
        bytesRead = len(b)
        if not bytesRead:
            raise RuntimeError("Ran out of data reading file %r, expected %d more bytes" % (self.f, length))
        else:
            self.length -= bytesRead
            self.start += bytesRead
            return b 
開發者ID:apple,項目名稱:ccs-calendarserver,代碼行數:45,代碼來源:stream.py


注:本文中的mmap.error方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。