Python mmap.ACCESS_READ Examples

The following are 30 code examples of mmap.ACCESS_READ(). 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 also want to check out all available functions/classes of the module mmap , or try the search function .
Example #1
Source File: compile_and_test.py    From wb_contest_submission_server with GNU General Public License v3.0 6 votes vote down vote up
def preprocess(source):
    with open(source, 'rb', 0) as f, \
            mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) as contents:
        for string in forbidden_strings:
            if contents.find(string) != -1:
                error_message = "Forbidden string '%s' is found." % string.decode()
                print(error_message, flush=True)
                post_data = {"error_message": error_message}
                exit_after_notifying_launcher(
                    ERR_CODE_CONTAININT_FORBIDDEN_STRING, post_data
                )

        for pattern in forbidden_pattern:
            match = re.search(pattern, contents)
            if match is not None:
                error_message = "The string '%s' in the source code matches forbidden pattern '%s'." % (
                    match.group(0).decode(), pattern.pattern.decode())
                print(error_message, flush=True)
                post_data = {"error_message": error_message}
                exit_after_notifying_launcher(
                    ERR_CODE_CONTAININT_FORBIDDEN_STRING, post_data) 
Example #2
Source File: embeddings.py    From mead-baseline with Apache License 2.0 6 votes vote down vote up
def _read_word2vec_mmap(self, filename, idx, known_vocab, keep_unused):
        import mmap

        word_vectors = []
        with io.open(filename, "rb") as f:
            with contextlib.closing(mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)) as m:
                header_end = m[:50].find(b"\n")
                vsz, dsz = map(int, (m[:header_end]).split(b" "))
                width = 4 * dsz
                current = header_end + 1
                for i in range(vsz):
                    word, vec, current = self._read_word2vec_line_mmap(m, width, current)
                    if word in self.vocab:
                        continue
                    if keep_unused is False and word not in known_vocab:
                        continue
                    if known_vocab and word in known_vocab:
                        known_vocab[word] = 0

                    word_vectors.append(vec)
                    self.vocab[word] = idx
                    idx += 1
                return word_vectors, dsz, known_vocab, idx 
Example #3
Source File: test_regressions.py    From ironpython3 with Apache License 2.0 6 votes vote down vote up
def test_main_gh1081(self):
        """https://github.com/IronLanguages/main/issues/1081"""
        import io
        import mmap

        test_file_name = os.path.join(self.temporary_dir, "test_main_gh1081.bin")

        with open(test_file_name, "wb") as f:
            f.write(bytearray(range(256)))

        try:
            with io.open(test_file_name, "rb") as f:
                mm = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
                try:
                    self.assertEqual(mm[:], bytearray(range(256)))
                finally:
                    mm.close()
        finally:
            os.remove(test_file_name) 
Example #4
Source File: common.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_file_handles(self):
        # GH 14418 - don't close user provided file handles

        fh = StringIO('a,b\n1,2')
        self.read_csv(fh)
        assert not fh.closed

        with open(self.csv1, 'r') as f:
            self.read_csv(f)
            assert not f.closed

        # mmap not working with python engine
        if self.engine != 'python':

            import mmap
            with open(self.csv1, 'r') as f:
                m = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
                self.read_csv(m)
                # closed attribute new in python 3.2
                if PY3:
                    assert not m.closed
                m.close() 
Example #5
Source File: thrift_file.py    From thrift-tools with Apache License 2.0 6 votes vote down vote up
def __init__(self, file_name='-', read_values=False, debug=False):
        self._debug = debug
        self._read_values = read_values
        if file_name == '-':
            fh = sys.stdin
        else:
            try:
                fh = open(file_name)
            except IOError as ex:
                raise ThriftFile.Error('Could not open %s: %s' % (file_name, ex))

        if HAS_MMAP and file_name != '-':
            self._data = mmap.mmap(fh.fileno(), 0, access=mmap.ACCESS_READ)
            self._view = None
        else:
            # this might hurt...
            self._data = fh.read()
            self._view = memoryview(self._data) 
Example #6
Source File: test_regressions.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test_main_gh1081(self):
        """https://github.com/IronLanguages/main/issues/1081"""
        import io
        import mmap

        test_file_name = os.path.join(self.temporary_dir, "test_main_gh1081.bin")

        with open(test_file_name, "wb") as f:
            f.write(bytearray(range(256)))

        try:
            with io.open(test_file_name, "rb") as f:
                mm = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
                try:
                    self.assertEqual(mm[:], bytearray(range(256)))
                finally:
                    mm.close()
        finally:
            os.remove(test_file_name) 
Example #7
Source File: irsdk.py    From pyirsdk with MIT License 6 votes vote down vote up
def startup(self, test_file=None, dump_to=None):
        if test_file is None and not self._check_sim_status():
            return False

        if self._shared_mem is None:
            if test_file:
                f = open(test_file, 'rb')
                self._shared_mem = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
                self.__is_using_test_file = True
            else:
                self._shared_mem = mmap.mmap(0, MEMMAPFILESIZE, MEMMAPFILE, access=mmap.ACCESS_READ)

        if self._shared_mem:
            if dump_to:
                with open(dump_to, 'wb') as f:
                    f.write(self._shared_mem)
            self._header = Header(self._shared_mem)
            self.is_initialized = self._header.version >= 1 and len(self._header.var_buf) > 0

        return self.is_initialized 
Example #8
Source File: mp_module.py    From macro_pack with Apache License 2.0 6 votes vote down vote up
def getMainVBAFile(self):
        """ return main vba file (the one containing macro entry point) """
        result = ""
        vbaFiles = self.getVBAFiles()
        if len(vbaFiles)==1:
            result = vbaFiles[0]
        else:
            if self.startFunction is not None:
                for vbaFile in vbaFiles:
                    if  os.stat(vbaFile).st_size != 0:  
                        with open(vbaFile, 'rb', 0) as file, mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as s:
                            if s.find(self.startFunction.encode()) != -1:
                                result  = vbaFile
                                break
                            
        return result 
Example #9
Source File: mp_module.py    From macro_pack with Apache License 2.0 6 votes vote down vote up
def startFunction(self):
        """ Return start function, attempt to find it in vba files if _startFunction is not set """
        result = None
        if self._startFunction is not None:
            result =  self._startFunction
        else:
             
            vbaFiles = self.getVBAFiles()
            for vbaFile in vbaFiles:
                if  os.stat(vbaFile).st_size != 0:  
                    with open(vbaFile, 'rb', 0) as file, mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as s:
                        for potentialStartFunction in self.potentialStartFunctions:
                            if s.find(potentialStartFunction.encode()) != -1:
                                self._startFunction = potentialStartFunction
                                if self._startFunction not in self.reservedFunctions:
                                    self.reservedFunctions.append(self._startFunction)
                                result = potentialStartFunction
                                break                
        return result 
Example #10
Source File: mmap_dict.py    From client_python with Apache License 2.0 6 votes vote down vote up
def __init__(self, filename, read_mode=False):
        self._f = open(filename, 'rb' if read_mode else 'a+b')
        self._fname = filename
        capacity = os.fstat(self._f.fileno()).st_size
        if capacity == 0:
            self._f.truncate(_INITIAL_MMAP_SIZE)
            capacity = _INITIAL_MMAP_SIZE
        self._capacity = capacity
        self._m = mmap.mmap(self._f.fileno(), self._capacity,
                            access=mmap.ACCESS_READ if read_mode else mmap.ACCESS_WRITE)

        self._positions = {}
        self._used = _unpack_integer(self._m, 0)[0]
        if self._used == 0:
            self._used = 8
            _pack_integer(self._m, 0, self._used)
        else:
            if not read_mode:
                for key, _, pos in self._read_all_values():
                    self._positions[key] = pos 
Example #11
Source File: cache.py    From vergeml with MIT License 6 votes vote down vote up
def __init__(self, path, mode):
        assert mode in ("r", "w")

        self.path = path
        self.file = open(self.path, mode + "b")
        self.mmfile = None
        self.mode = mode
        self.cnt = _CacheFileContent()

        if mode == "r":
            # Read the last part of the file which contains the contents of the
            # cache.
            self.cnt.read(self.file, self.path)
            self.mmfile = mmap.mmap(self.file.fileno(), 0, access=mmap.ACCESS_READ)
        else:
            # The 8 bytes header contain the position of the content index.
            # We fill this header with zeroes and write the actual position
            # once all samples have been written to the cache
            self.file.write(struct.pack('<Q', 0)) 
Example #12
Source File: core.py    From DumpsterDiver with MIT License 5 votes vote down vote up
def bad_expression_verifier(_file):
    try:

        with open(_file, 'rb', 0) as f, \
        mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) as string_object:

            for search_expression in BAD_EXPRESSIONS:

                if string_object.find(search_expression.encode()) != -1:
                    return True          

    except Exception as e:
        logger.error("while trying to open " + str(_file) + " file. Details:\n" + str(e)) 
Example #13
Source File: ttf.py    From flappy-bird-py with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, filename):
        """Read the given TrueType file.

        :Parameters:
            `filename`
                The name of any Windows, OS2 or Macintosh Truetype file.

        The object must be closed (see `close`) after use.

        An exception will be raised if the file does not exist or cannot
        be read.
        """
        if not filename: filename = ''
        len = os.stat(filename).st_size
        self._fileno = os.open(filename, os.O_RDONLY)
        if hasattr(mmap, 'MAP_SHARED'):
            self._data = mmap.mmap(self._fileno, len, mmap.MAP_SHARED,
                mmap.PROT_READ)
        else:
            self._data = mmap.mmap(self._fileno, len, None, mmap.ACCESS_READ)

        offsets = _read_offset_table(self._data, 0)
        self._tables = {}
        for table in _read_table_directory_entry.array(self._data,
            offsets.size, offsets.num_tables):
            self._tables[table.tag] = table

        self._names = None
        self._horizontal_metrics = None
        self._character_advances = None
        self._character_kernings = None
        self._glyph_kernings = None
        self._character_map = None
        self._glyph_map = None
        self._font_selection_flags = None

        self.header = \
            _read_head_table(self._data, self._tables['head'].offset)
        self.horizontal_header = \
            _read_horizontal_header(self._data, self._tables['hhea'].offset) 
Example #14
Source File: common.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self, f):
        self.mmap = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) 
Example #15
Source File: irsdk.py    From pyirsdk with MIT License 5 votes vote down vote up
def open(self, ibt_file):
        self._ibt_file = open(ibt_file, 'rb')
        self._shared_mem = mmap.mmap(self._ibt_file.fileno(), 0, access=mmap.ACCESS_READ)
        self._header = Header(self._shared_mem)
        self._disk_header = DiskSubHeader(self._shared_mem, 112) 
Example #16
Source File: ioctl.py    From OSEE with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __get_unicode_device_names(self):
		""" Returns all Unicode strings within the binary """

		min_length = 4
		possible_names = set()
		with open(self._driver, "rb") as f:
			b = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)

			for s in self.__extract_unicode_strings(b, n=min_length):
				s_str = str(s.s)
				if s_str.startswith('\\Device\\'):
					possible_names.add(str(s.s))
		return possible_names 
Example #17
Source File: uds.py    From uds with GNU Affero General Public License v3.0 5 votes vote down vote up
def ext_upload_chunked_part(chunk):
    """Upload a chunked part to drive and return the size of the chunk"""
    _api = GoogleAPI()
    # print("Chunk %s, bytes %s to %s" % (chunk.part, chunk.range_start, chunk.range_end))

    with open(chunk.path) as fd:
        mm = mmap.mmap(fd.fileno(), 0, access=mmap.ACCESS_READ)
        chunk_bytes = mm[chunk.range_start:chunk.range_end]

    encoded_chunk = encoder.encode(chunk_bytes)

    file_metadata = {
        'name': chunk.media.name + str(chunk.part),
        'mimeType': 'application/vnd.google-apps.document',
        'parents': [chunk.parent],
        'properties': {
            'part': str(chunk.part)
        }
    }

    mediaio_file = MediaIoBaseUpload(io.StringIO(encoded_chunk),
                                     mimetype='text/plain')

    _api.upload_single_file(mediaio_file, file_metadata)

    return len(chunk_bytes) 
Example #18
Source File: uds.py    From uds with GNU Affero General Public License v3.0 5 votes vote down vote up
def upload_chunked_part(self, chunk, api=None):
        """Upload a chunked part to drive and return the size of the chunk
        :param chunk: 
        :param api: 
        :return: 
        """
        if not api:
            api = self.api

        with open(chunk.path) as fd:
            mm = mmap.mmap(fd.fileno(), 0, access=mmap.ACCESS_READ)
            chunk_bytes = mm[chunk.range_start:chunk.range_end]

        encoded_chunk = encoder.encode(chunk_bytes)

        file_metadata = {
            'name': chunk.media.name + str(chunk.part),
            'mimeType': 'application/vnd.google-apps.document',
            'parents': [chunk.parent],
            'properties': {
                'part': str(chunk.part)
            }
        }

        mediaio_file = MediaIoBaseUpload(io.StringIO(encoded_chunk),
                                         mimetype='text/plain')

        self.api.upload_single_file(mediaio_file, file_metadata)

        return len(chunk_bytes) 
Example #19
Source File: __init__.py    From dexparser with MIT License 5 votes vote down vote up
def __init__(self, filedir=None, fileobj=None):
        if not filedir and not fileobj:
            raise InsufficientParameterError('fileobj or filedir parameter required.')

        if filedir:
            if not os.path.isfile(filedir):
                raise FileNotFoundError
 
            with open(filedir, 'rb') as f:
                self.data = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)

        if fileobj:
            self.data = fileobj

        self.header_data = {
            'magic': self.data[0:8],
            'checksum': struct.unpack('<L', self.data[8:0xC])[0],
            'signiture': self.data[0xC:0x20],
            'file_size': struct.unpack('<L', self.data[0x20:0x24])[0],
            'header_size': struct.unpack('<L', self.data[0x24:0x28])[0],
            'endian_tag': struct.unpack('<L', self.data[0x28:0x2C])[0],
            'link_size': struct.unpack('<L', self.data[0x2C:0x30])[0],
            'link_off': struct.unpack('<L', self.data[0x30:0x34])[0],
            'map_off': struct.unpack('<L', self.data[0x34:0x38])[0],
            'string_ids_size': struct.unpack('<L', self.data[0x38:0x3C])[0],
            'string_ids_off': struct.unpack('<L', self.data[0x3C:0x40])[0],
            'type_ids_size': struct.unpack('<L', self.data[0x40:0x44])[0],
            'type_ids_off': struct.unpack('<L', self.data[0x44:0x48])[0],
            'proto_ids_size': struct.unpack('<L', self.data[0x48:0x4C])[0],
            'proto_ids_off': struct.unpack('<L', self.data[0x4C:0x50])[0],
            'field_ids_size': struct.unpack('<L', self.data[0x50:0x54])[0],
            'field_ids_off': struct.unpack('<L', self.data[0x54:0x58])[0],
            'method_ids_size': struct.unpack('<L', self.data[0x58:0x5C])[0],
            'method_ids_off': struct.unpack('<L', self.data[0x5C:0x60])[0],
            'class_defs_size': struct.unpack('<L', self.data[0x60:0x64])[0],
            'class_defs_off': struct.unpack('<L', self.data[0x64:0x68])[0],
            'data_size': struct.unpack('<L', self.data[0x68:0x6C])[0],
            'data_off': struct.unpack('<L', self.data[0x6C:0x70])[0]
        } 
Example #20
Source File: io.py    From rally with Apache License 2.0 5 votes vote down vote up
def open(self):
        self.f = open(self.file_name, mode="r+b")
        self.mm = mmap.mmap(self.f.fileno(), 0, access=mmap.ACCESS_READ)
        # madvise is available in Python 3.8+
        with suppress(AttributeError):
            self.mm.madvise(mmap.MADV_SEQUENTIAL)

        # allow for chaining
        return self 
Example #21
Source File: xdot.py    From NoobSec-Toolkit with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, buf = None, pos = 0, filename = None, fp = None):
        if fp is not None:
            try:
                fileno = fp.fileno()
                length = os.path.getsize(fp.name)
                import mmap
            except:
                # read whole file into memory
                buf = fp.read()
                pos = 0
            else:
                # map the whole file into memory
                if length:
                    # length must not be zero
                    buf = mmap.mmap(fileno, length, access = mmap.ACCESS_READ)
                    pos = os.lseek(fileno, 0, 1)
                else:
                    buf = ''
                    pos = 0

            if filename is None:
                try:
                    filename = fp.name
                except AttributeError:
                    filename = None

        self.buf = buf
        self.pos = pos
        self.line = 1
        self.col = 1
        self.filename = filename 
Example #22
Source File: xdot.py    From NoobSec-Toolkit with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, buf = None, pos = 0, filename = None, fp = None):
        if fp is not None:
            try:
                fileno = fp.fileno()
                length = os.path.getsize(fp.name)
                import mmap
            except:
                # read whole file into memory
                buf = fp.read()
                pos = 0
            else:
                # map the whole file into memory
                if length:
                    # length must not be zero
                    buf = mmap.mmap(fileno, length, access = mmap.ACCESS_READ)
                    pos = os.lseek(fileno, 0, 1)
                else:
                    buf = ''
                    pos = 0

            if filename is None:
                try:
                    filename = fp.name
                except AttributeError:
                    filename = None

        self.buf = buf
        self.pos = pos
        self.line = 1
        self.col = 1
        self.filename = filename 
Example #23
Source File: xdot.py    From NoobSec-Toolkit with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, buf = None, pos = 0, filename = None, fp = None):
        if fp is not None:
            try:
                fileno = fp.fileno()
                length = os.path.getsize(fp.name)
                import mmap
            except:
                # read whole file into memory
                buf = fp.read()
                pos = 0
            else:
                # map the whole file into memory
                if length:
                    # length must not be zero
                    buf = mmap.mmap(fileno, length, access = mmap.ACCESS_READ)
                    pos = os.lseek(fileno, 0, 1)
                else:
                    buf = ''
                    pos = 0

            if filename is None:
                try:
                    filename = fp.name
                except AttributeError:
                    filename = None

        self.buf = buf
        self.pos = pos
        self.line = 1
        self.col = 1
        self.filename = filename 
Example #24
Source File: xdot.py    From NoobSec-Toolkit with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, buf = None, pos = 0, filename = None, fp = None):
        if fp is not None:
            try:
                fileno = fp.fileno()
                length = os.path.getsize(fp.name)
                import mmap
            except:
                # read whole file into memory
                buf = fp.read()
                pos = 0
            else:
                # map the whole file into memory
                if length:
                    # length must not be zero
                    buf = mmap.mmap(fileno, length, access = mmap.ACCESS_READ)
                    pos = os.lseek(fileno, 0, 1)
                else:
                    buf = ''
                    pos = 0

            if filename is None:
                try:
                    filename = fp.name
                except AttributeError:
                    filename = None

        self.buf = buf
        self.pos = pos
        self.line = 1
        self.col = 1
        self.filename = filename 
Example #25
Source File: game_state.py    From WC3StreamerOverlay with GNU General Public License v3.0 5 votes vote down vote up
def open(self):
    self.shmem = mmap.mmap(-1, ObserverSharedMemory.sizeof(), "War3StatsObserverSharedMemory", access=mmap.ACCESS_READ) 
Example #26
Source File: ttf.py    From flappy-bird-py with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, filename):
        """Read the given TrueType file.

        :Parameters:
            `filename`
                The name of any Windows, OS2 or Macintosh Truetype file.

        The object must be closed (see `close`) after use.

        An exception will be raised if the file does not exist or cannot
        be read.
        """
        if not filename: filename = ''
        len = os.stat(filename).st_size
        self._fileno = os.open(filename, os.O_RDONLY)
        if hasattr(mmap, 'MAP_SHARED'):
            self._data = mmap.mmap(self._fileno, len, mmap.MAP_SHARED,
                mmap.PROT_READ)
        else:
            self._data = mmap.mmap(self._fileno, len, None, mmap.ACCESS_READ)

        offsets = _read_offset_table(self._data, 0)
        self._tables = {}
        for table in _read_table_directory_entry.array(self._data,
            offsets.size, offsets.num_tables):
            self._tables[table.tag] = table

        self._names = None
        self._horizontal_metrics = None
        self._character_advances = None
        self._character_kernings = None
        self._glyph_kernings = None
        self._character_map = None
        self._glyph_map = None
        self._font_selection_flags = None

        self.header = \
            _read_head_table(self._data, self._tables['head'].offset)
        self.horizontal_header = \
            _read_horizontal_header(self._data, self._tables['hhea'].offset) 
Example #27
Source File: ttf.py    From flappy-bird-py with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, filename):
        """Read the given TrueType file.

        :Parameters:
            `filename`
                The name of any Windows, OS2 or Macintosh Truetype file.

        The object must be closed (see `close`) after use.

        An exception will be raised if the file does not exist or cannot
        be read.
        """
        if not filename: filename = ''
        len = os.stat(filename).st_size
        self._fileno = os.open(filename, os.O_RDONLY)
        if hasattr(mmap, 'MAP_SHARED'):
            self._data = mmap.mmap(self._fileno, len, mmap.MAP_SHARED,
                mmap.PROT_READ)
        else:
            self._data = mmap.mmap(self._fileno, len, None, mmap.ACCESS_READ)

        offsets = _read_offset_table(self._data, 0)
        self._tables = {}
        for table in _read_table_directory_entry.array(self._data,
            offsets.size, offsets.num_tables):
            self._tables[table.tag] = table

        self._names = None
        self._horizontal_metrics = None
        self._character_advances = None
        self._character_kernings = None
        self._glyph_kernings = None
        self._character_map = None
        self._glyph_map = None
        self._font_selection_flags = None

        self.header = \
            _read_head_table(self._data, self._tables['head'].offset)
        self.horizontal_header = \
            _read_horizontal_header(self._data, self._tables['hhea'].offset) 
Example #28
Source File: common.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def __init__(self, f):
        self.mmap = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) 
Example #29
Source File: test_c_parser_only.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_file_handles_mmap(c_parser_only, csv1):
    # gh-14418
    #
    # Don't close user provided file handles.
    parser = c_parser_only

    with open(csv1, "r") as f:
        m = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
        parser.read_csv(m)

        if PY3:
            assert not m.closed
        m.close() 
Example #30
Source File: test_simple_2layer.py    From kiplot with GNU General Public License v3.0 5 votes vote down vote up
def get_mmapped_data(filename):

    with open(filename) as fo:
        return mmap.mmap(fo.fileno(), 0, access=mmap.ACCESS_READ)


# content of test_sample.py