def __init__(self, fileobj, name=None, onclose=None, mapped=True, gzip=False): if gzip: fileobj = GzipFile(fileobj=fileobj) self.file = fileobj self._name = name self.onclose = onclose self.is_closed = False for attr in ("read", "readline", "write", "tell", "seek", "truncate"): if hasattr(fileobj, attr): setattr(self, attr, getattr(fileobj, attr)) # If mapped is True, set the 'map' attribute to a memory-mapped # representation of the file. Otherwise, the fake 'map' that set up by # the base class will be used. if not gzip and mapped and hasattr(fileobj, "mode") and "r" in fileobj.mode: fd = fileobj.fileno() self.size = os.fstat(fd).st_size if self.size > 0: import mmap try: self.map = mmap.mmap(fd, self.size, access=mmap.ACCESS_READ) except OSError: self._setup_fake_map() else: self._setup_fake_map() self.is_real = not gzip and hasattr(fileobj, "fileno")
def __init__(self, fileobj, name=None, onclose=None, mapped=True, gzip=False): if gzip: fileobj = GzipFile(fileobj=fileobj) self.file = fileobj self._name = name self.onclose = onclose self.is_closed = False for attr in ("read", "readline", "write", "tell", "seek", "truncate"): if hasattr(fileobj, attr): setattr(self, attr, getattr(fileobj, attr)) self.is_real = not gzip and hasattr(fileobj, "fileno") # If mapped is True, set the 'map' attribute to a memory-mapped # representation of the file. Otherwise, the fake 'map' that set up by # the base class will be used. if (mapped and self.is_real and hasattr(fileobj, "mode") and "r" in fileobj.mode): fd = fileobj.fileno() self.size = os.fstat(fd).st_size if self.size > 0: import mmap try: self.map = mmap.mmap(fd, self.size, access=mmap.ACCESS_READ) except OSError: self._setup_fake_map() else: self._setup_fake_map()