我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用mmap.ACCESS_COPY。
def __init__(self, filename): self._filename = filename self._f = open(filename, "rb") # memory-map the file, size 0 means whole file self._mapped = mmap.mmap(self._f.fileno(), 0, access=mmap.ACCESS_COPY) super(MappedFileDataModel, self).__init__(self._mapped)
def _verify_bmap_checksum(self): """ This is a helper function which verifies the bmap file checksum. """ import mmap correct_chksum = self._xml.find(self._bmap_cs_attrib_name).text.strip() # Before verifying the shecksum, we have to substitute the checksum # value stored in the file with all zeroes. For these purposes we # create private memory mapping of the bmap file. mapped_bmap = mmap.mmap(self._f_bmap.fileno(), 0, access=mmap.ACCESS_COPY) chksum_pos = mapped_bmap.find(correct_chksum.encode()) assert chksum_pos != -1 mapped_bmap[chksum_pos:chksum_pos + self._cs_len] = b'0' * self._cs_len hash_obj = hashlib.new(self._cs_type) hash_obj.update(mapped_bmap) calculated_chksum = hash_obj.hexdigest() mapped_bmap.close() if calculated_chksum != correct_chksum: raise Error("checksum mismatch for bmap file '%s': calculated " "'%s', should be '%s'" % (self._bmap_path, calculated_chksum, correct_chksum))
def _verify_bmap_checksum(self): """ This is a helper function which verifies SHA1 checksum of the bmap file. """ import mmap correct_sha1 = self._xml.find("BmapFileSHA1").text.strip() # Before verifying the shecksum, we have to substitute the SHA1 value # stored in the file with all zeroes. For these purposes we create # private memory mapping of the bmap file. mapped_bmap = mmap.mmap(self._f_bmap.fileno(), 0, access = mmap.ACCESS_COPY) sha1_pos = mapped_bmap.find(correct_sha1) assert sha1_pos != -1 mapped_bmap[sha1_pos:sha1_pos + 40] = '0' * 40 calculated_sha1 = hashlib.sha1(mapped_bmap).hexdigest() mapped_bmap.close() if calculated_sha1 != correct_sha1: raise Error("checksum mismatch for bmap file '%s': calculated " \ "'%s', should be '%s'" % \ (self._bmap_path, calculated_sha1, correct_sha1))
def _verify_bmap_checksum(self): """ This is a helper function which verifies the bmap file checksum. """ import mmap if self.bmap_version_minor == 3: correct_chksum = self._xml.find("BmapFileSHA1").text.strip() else: correct_chksum = self._xml.find("BmapFileChecksum").text.strip() # Before verifying the shecksum, we have to substitute the checksum # value stored in the file with all zeroes. For these purposes we # create private memory mapping of the bmap file. mapped_bmap = mmap.mmap(self._f_bmap.fileno(), 0, access = mmap.ACCESS_COPY) chksum_pos = mapped_bmap.find(correct_chksum) assert chksum_pos != -1 mapped_bmap[chksum_pos:chksum_pos + self._cs_len] = '0' * self._cs_len hash_obj = hashlib.new(self._cs_type) hash_obj.update(mapped_bmap) calculated_chksum = hash_obj.hexdigest() mapped_bmap.close() if calculated_chksum != correct_chksum: raise Error("checksum mismatch for bmap file '%s': calculated " "'%s', should be '%s'" % (self._bmap_path, calculated_chksum, correct_chksum))
def _verify_bmap_checksum(self): """ This is a helper function which verifies SHA1 checksum of the bmap file. """ import mmap correct_sha1 = self._xml.find("BmapFileSHA1").text.strip() # Before verifying the shecksum, we have to substitute the SHA1 value # stored in the file with all zeroes. For these purposes we create # private memory mapping of the bmap file. mapped_bmap = mmap.mmap(self._f_bmap.fileno(), 0, access = mmap.ACCESS_COPY) sha1_pos = mapped_bmap.find(correct_sha1) assert sha1_pos != -1 mapped_bmap[sha1_pos:sha1_pos + 40] = '0' * 40 calculated_sha1 = hashlib.sha1(mapped_bmap).hexdigest() mapped_bmap.close() if calculated_sha1 != correct_sha1: raise Error("checksum mismatch for bmap file '%s': calculated " "'%s', should be '%s'" % (self._bmap_path, calculated_sha1, correct_sha1))