def test_offset_more(self): from mmap import mmap, ALLOCATIONGRANULARITY with open(self.tmpname, "w+b") as f: halfsize = ALLOCATIONGRANULARITY f.write("\0" * halfsize) f.write("foo") f.write("\0" * (halfsize - 3)) m = mmap(f.fileno(), 0) m.close() with open(self.tmpname, "r+b") as f: m = mmap(f.fileno(), halfsize, offset=halfsize) assert m[0:3] == "foo" try: m.resize(512) except SystemError: pass else: assert len(m) == 512 raises(ValueError, m.seek, 513, 0) assert m[0:3] == "foo" with open(self.tmpname) as f: f.seek(0, 2) assert f.tell() == halfsize + 512 assert m.size() == halfsize + 512 m.close()
def __init__ (self,host,path,parts,pbegin=0,pend=0,m=None): asyncore.dispatcher.__init__(self) # Initialize class member variables. self.keepalive = False self.done = 0 self.h = [self] self.recvhead = 1 self.bytes = 0 self.ack = 0 self.begin = time() self.path = path self.parts = parts self.host = host self.buffer = "" self.pbegin = pbegin self.pend = pend self.length = 8192 self.f = None # Grab the filename from the end of the URL. self.filename = split(path,"/")[-1] # Check if file exists and if so ask if overwrite necessary. if os.access(self.filename,os.O_RDWR) and self.parts > 0: u = raw_input("File already exists, overwrite? [y/N] ") if u == 'y' or u == 'Y': print "Overwriting..." else: print "Aborting..." return None # Connect to the host with it on port 80. print "Connecting..." self.create_socket(socket.AF_INET, socket.SOCK_STREAM) self.connect((host, 80)) # Parts are greater than 0 so we are the parent, open file, get header. if self.parts > 0: # Open and memory map the file. self.f = open(self.filename,'wb+') self.f.write("\0") self.f.flush() # We have to flush to make the file buffer ready for mmap. # Windows uses 0 for second parameter to auto-size to current file size. # Whereas, on Linux and other platforms, mmap requires a size. if platformName == "Windows": self.m = mmap(self.f.fileno(), 0) else: self.m = mmap(self.f.fileno(), os.fstat(self.f.fileno()).st_size) # Download the header. self.buffer = "HEAD %s HTTP/1.1\r\nHost: %s\r\n\r\n" % (self.path,self.host) print "Downloading http://%s%s" % (self.host,self.path) # Otherwise, we are a child, skip the header and download our segment. elif self.parts == 0: # Set our own mmap to the one given to us by the parent. self.m = m # Prepare ourselves to download the segment. self.bytes = self.pbegin self.length = self.pend self.recvhead = 2 self.buffer = "GET %s HTTP/1.1\r\nHost: %s\r\nRange: bytes=%lu-%lu\r\n\r\n" % (self.path,self.host,self.pbegin,self.pend) print self.buffer
def __init__(self, code, ret_type, *arg_types, raw=False): if not raw: with tempfile.NamedTemporaryFile(suffix='.asm') as tmp: tmp.write(str.encode(code)) tmp.flush() output_path = "{}.bin".format(tmp.name) subprocess.check_call(["nasm", tmp.name, "-o", output_path]) output = open(output_path, 'rb') code = output.read() output.close() os.remove(output_path) if not isinstance(code, list) and isinstance(code, str): code = [ord(c) for c in code] self.length = len(code) mmap.restype = POINTER(ARRAY(c_ubyte, self.length)) mem = mmap(0, self.length, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)[0] getaddr = CFUNCTYPE(c_void_p, c_void_p)(lambda p: p) func = CFUNCTYPE(ret_type, *arg_types)(getaddr(mem)) mem[:] = code self.func = func self.mem = mem
def _parse_from_file(self, path): with open(path, 'rb') as f: st = os.fstat(f.fileno()) if not st.st_size: raise ELFError('empty file') with mmap(f.fileno(), st.st_size, access=ACCESS_READ) as image: self._parse_from_buf(image)
def test_del_item(self): from mmap import mmap f = open(self.tmpname + "t", "w+") f.write("foobar") f.flush() m = mmap(f.fileno(), 6) def fn(): del m["foo"] raises(TypeError, fn) def fn(): del m[1:3] raises(TypeError, fn) def fn(): del m[1] raises(TypeError, fn) m.close() f.close()
def pci_set_value(self, resource, val, offset): fd = open(resource, O_RDWR) mm = mmap(fd, 0) val = self.pci_mem_write(mm, offset, val) mm.close() close(fd) return val
def test_find(self): from mmap import mmap f = open(self.tmpname + "g", "w+") f.write("foobar\0") f.flush() m = mmap(f.fileno(), 7) raises(TypeError, m.find, 123) raises(TypeError, m.find, "foo", "baz") assert m.find("b") == 3 assert m.find("z") == -1 assert m.find("o", 5) == -1 assert m.find("ob") == 2 assert m.find("\0") == 6 assert m.find("ob", 1) == 2 assert m.find("ob", 2) == 2 assert m.find("ob", 3) == -1 assert m.find("ob", -4) == -1 assert m.find("ob", -5) == 2 assert m.find("ob", -999999999) == 2 assert m.find("ob", 1, 3) == -1 assert m.find("ob", 1, 4) == 2 assert m.find("ob", 1, 999999999) == 2 assert m.find("ob", 1, 0) == -1 assert m.find("ob", 1, -1) == 2 assert m.find("ob", 1, -3) == 2 assert m.find("ob", 1, -4) == -1 # data = m.read(2) assert data == "fo" assert m.find("o") == 2 assert m.find("oo") == -1 assert m.find("o", 0) == 1 m.close() f.close()
def test_rfind(self): from mmap import mmap f = open(self.tmpname + "g", "w+") f.write("foobarfoobar\0") f.flush() m = mmap(f.fileno(), 13) raises(TypeError, m.rfind, 123) raises(TypeError, m.rfind, "foo", "baz") assert m.rfind("b") == 9 assert m.rfind("z") == -1 assert m.rfind("o", 11) == -1 assert m.rfind("ob") == 8 assert m.rfind("\0") == 12 assert m.rfind("ob", 7) == 8 assert m.rfind("ob", 8) == 8 assert m.rfind("ob", 9) == -1 assert m.rfind("ob", -4) == -1 assert m.rfind("ob", -5) == 8 assert m.rfind("ob", -999999999) == 8 assert m.rfind("ob", 1, 3) == -1 assert m.rfind("ob", 1, 4) == 2 assert m.rfind("ob", 1, 999999999) == 8 assert m.rfind("ob", 1, 0) == -1 assert m.rfind("ob", 1, -1) == 8 assert m.rfind("ob", 1, -3) == 8 assert m.rfind("ob", 1, -4) == 2 # data = m.read(8) assert data == "foobarfo" assert m.rfind("o") == 8 assert m.rfind("oo") == -1 assert m.rfind("o", 0) == 8 m.close() f.close()
def pci_get_value(self, resource, offset): fd = open(resource, O_RDWR) mm = mmap(fd, 0) val = self.pci_mem_read(mm, offset) mm.close() close(fd) return val
def test_repeatition(self): from mmap import mmap f = open(self.tmpname + "v", "w+") f.write("foobar") f.flush() m = mmap(f.fileno(), 6) def fn(): m * 1 raises((SystemError, TypeError), fn) # SystemError is in CPython, def fn(m): m *= 1 # but it raises((SystemError, TypeError), fn, m) # doesn't def fn(): 1 * m # make much sense raises((SystemError, TypeError), fn) m.close() f.close()
def __init__(self, sysfsPath, barNum, winsize): """ Create a mapping """ self.mm_base = 0 # Phys. addr. self.mm_winsize = 0 # Mapped memory window size self.mm_devf = None # File handle self.mm = None # mmap handle self.map_va = 0 self.ptr4 = None self.ptr2 = None self.ptr1 = None bars = mypci.getPCIdeviceBars(sysfsPath) self.mm_base, barsize = bars[barNum] assert (barsize != 0) and (self.mm_base != 0) assert barsize >= winsize # barsize is size from the resource list self.mm_devf, self.mm_winsize = mypci.openDeviceBar(sysfsPath, bar=barNum, access='w') assert self.mm_winsize >= winsize # mm_winsize is actually mapped size self.mm = mmap(self.mm_devf.fileno(), winsize, MAP_SHARED, PROT_READ | PROT_WRITE) self.ptr4 = POINTER(c_uint32)(c_uint32.from_buffer(self.mm)) self.map_va = cast(self.ptr4, c_void_p).value self.ptr2 = cast(self.ptr4, POINTER(c_uint16)) self.ptr1 = cast(self.ptr4, POINTER(c_uint8)) self.memr = self.read32 self.memw = self.write32
def test_concatenation(self): from mmap import mmap f = open(self.tmpname + "u", "w+") f.write("foobar") f.flush() m = mmap(f.fileno(), 6) def fn(): m + 1 raises((SystemError, TypeError), fn) # SystemError is in CPython, def fn(m): m += 1 # but it doesn't make much raises((SystemError, TypeError), fn, m) # sense def fn(): 1 + m raises(TypeError, fn) m.close() f.close()
def main(): global uartfile uartfile=os.open(UART_DEVICE_NAME,os.O_RDWR) mm=mmap(fileno=uartfile, length=sizeof(UART),flags=MAP_SHARED,prot=PROT_READ | PROT_WRITE, offset=0) global devcon devcon=lms2012.DEVCON() global uart uart=UART.from_buffer(mm) resetAll() print "reset all" time.sleep(2) global port port=3 setUartMode(2) timeout=datetime.datetime.now()+datetime.timedelta(seconds=10) while (datetime.datetime.now()<timeout): index = uart.Actual[port] print index print [str(b) for b in uart.Raw[port][index]]
def getData(stationID, filename): with open("venv/" + str(stationID) + "/" + str(filename) + ".csv", 'w', newline='') as wd: writer = csv.writer(wd) writer.writerow(["Time", "Temperature", "Humidity"]) with open("venv/" + str(stationID) + "/" + str(filename) + ".wd", "rb", 0) as f, mmap(f.fileno(), 0, access=ACCESS_READ) as s: mDate = datetime(1970, 1, 1, 0, 0) + timedelta(bytesToInt( s[12:20])) index = 20 while index + 23 <= s.size(): temperature = bytesToInt(s[index + 4:index + 6]) / 100 dewPoint = round((bytesToInt(s[index + 4:index + 6]) / 100 + bytesToInt(s[index + 6:index + 7]) / 10), 1) fullDate = mDate + timedelta( seconds=(bytesToInt(s[index:index + 4]))) timeData = fullDate.strftime("%d-%m-%Y %H:%M:%S") writer.writerow( [timeData, temperature, humidty(temperature, dewPoint)]) index += 23 f.close() wd.close()
def test_read_all(self): from mmap import mmap f = open(self.tmpname + "f", "wb+") f.write(b"foobar") f.flush() m = mmap(f.fileno(), 6) assert m.read(None) == b"foobar"
def test_get_crash(self): import sys from mmap import mmap s = b'hallo!!!' m = mmap(-1, len(s)) m[:] = s assert m[1:None:sys.maxsize] == b'a' m.close()
def init(): global isInitialized if not isInitialized: analogfile=os.open(lms2012.ANALOG_DEVICE_NAME,os.O_RDWR) global analogmm analogmm=mmap(fileno=analogfile, length=sizeof(lms2012.ANALOG),flags=MAP_SHARED,prot=PROT_READ | PROT_WRITE, offset=0) global analog analog=lms2012.ANALOG.from_buffer(analogmm) isInitialized=True
def test_sequence_type(self): from mmap import mmap f = open(self.tmpname + "x", "w+") f.write("foobar") f.flush() m = mmap(f.fileno(), 6) import operator assert operator.isSequenceType(m) assert not operator.isMappingType(m)
def test_close(self): from mmap import mmap f = open(self.tmpname + "c", "w+") f.write("c") f.flush() m = mmap(f.fileno(), 1) m.close() raises(ValueError, m.read, 1)
def docalculation(popsout, popsin, operation, count): gimp.progress_init("Sending data...") file = os.open("/home/cybertron/source/.dummymmap", os.O_RDWR) shmem = mmap(file, 20000000) for x in range(0, width): gimp.progress_update(float(x) / float(width)) gimp.displays_flush() for y in range(0, height): shmem.write(popsin[x, y][0:count]) shmem.close() os.close(file) gimp.progress_init("Starting threads...") childplist = [] for i in range(0, numthreads): childplist.append(Popen("/home/cybertron/bin/bumphelper.py", stdin=PIPE, stdout=PIPE)) childplist[i].stdin.write(operation + "\n") childplist[i].stdin.write(str(width) + "\n") childplist[i].stdin.write(str(height) + "\n") childplist[i].stdin.write(str((width / numthreads + 1) * i) + "\n") childplist[i].stdin.write(str(width / numthreads + 1) + "\n") childplist[i].stdin.write(str(bias) + "\n") childplist[i].stdin.write(str(invertx) + "\n") childplist[i].stdin.write(str(inverty) + "\n") childplist[i].stdin.write(str(wrap) + "\n") gimp.progress_init("Waiting for threads...") gimp.displays_flush() # Keep threads from blocking threadouts = ["" for i in range(0, numthreads)] alldone = False while not alldone: alldone = True for i in range(0, numthreads): if threadouts[i].find("EOD") == -1: threadouts[i] += childplist[i].stdout.read(50000) alldone = False gimp.progress_init("Retrieving results...") for i in range(0, numthreads): gimp.progress_update(float(i) / float(numthreads)) gimp.displays_flush() output = threadouts[i] start = (width / numthreads + 1) * i calc = width / numthreads + 1 if (start + calc > width): calc = width - start currpos = 0 for x in range(start, start + calc): for y in range(0, height): pixelval = output[currpos] pixelval += output[currpos + 1] pixelval += output[currpos + 2] pixelval += chr(255) popsout[x, y] = pixelval currpos += 3
def __loadToMemory(self, printOn=False): alignment = self.peHeader.ntHeader.OptionalHeader.SectionAlignment baseAddr = self.peHeader.ntHeader.OptionalHeader.ImageBase mapSize = self.peHeader.ntHeader.OptionalHeader.SizeOfImage #entryPoint = self.peHeader.ntHeader.OptionalHeader.AddressOfEntryPoint if printOn: print("[Caution] loadToMemory is not COMPLETED!!!!!!!!!!\n") print("[LOAD PE FORMAT FILE]") print("BaseAddress: ", hex(baseAddr)) print("Alignment: ", hex(alignment)) #print("EntryPoint: ", hex(entryPoint)) self.mapData = mmap(-1, mapSize, None, ACCESS_WRITE) if printOn: print("[Sections]") for section in self.peHeader.sectionTable: if not section.isAlloced: continue name = section.name vRva = section.VirtualAddress size = section.SizeOfRawData if printOn: print("\tName: ", name) print("\tSize: ", hex(size)) print("\tVirtualAddress(RVA): ", hex(vRva)) # Padding (alignment) """ if self.mapData.tell() % alignment != 0: cnt = alignment - (self.mapData.tell() % alignment) for i in range(cnt): self.mapData.write_byte(0) """ curPosB = self.mapData.tell() if vRva > curPosB: self.mapData.write(b"\x00" * (vRva - curPosB)) if printOn: curPosA = self.mapData.tell() print("\tHead Position of MemoryMap: ", hex(curPosA)) rawData = section.getRawData() res = self.mapData.write(rawData) self.mapData.flush() if printOn: print("\tRes from write(): ", hex(res)) print("") #self.isLoaded = True self.mapData.seek(0)
def init(): global isInitialized if not isInitialized: global uifile uifile=os.open(lms2012.UI_DEVICE_NAME,os.O_RDWR | os.O_SYNC) global uimm uimm=mmap(fileno=uifile, length=sizeof(lms2012.UI),flags=MAP_SHARED,prot=PROT_READ | PROT_WRITE, offset=0) global ui ui=lms2012.UI.from_buffer(uimm) isInitialized=True
def test_tell(self): from mmap import mmap f = open(self.tmpname + "m", "w+") f.write("c") f.flush() m = mmap(f.fileno(), 1) assert m.tell() >= 0 m.close() f.close()
def test_create(self): from mmap import mmap f = open(self.tmpname + "b", "w+") f.write("c") f.flush() m = mmap(f.fileno(), 1) assert m.read(99) == "c" f.close()
def test_buffer(self): from mmap import mmap f = open(self.tmpname + "y", "w+") f.write("foobar") f.flush() m = mmap(f.fileno(), 6) b = buffer(m) assert len(b) == 6 assert b[3] == "b" assert b[:] == "foobar"
def test_size(self): from mmap import mmap f = open(self.tmpname + "l", "w+") f.write("foobar") f.flush() m = mmap(f.fileno(), 5) assert m.size() == 6 # size of the underline file, not the mmap m.close() f.close()
def init(): global isInitialized if not isInitialized: global uartfile uartfile=os.open(lms2012.UART_DEVICE_NAME,os.O_RDWR) print uartfile global uartmm uartmm=mmap(fileno=uartfile, length=sizeof(lms2012.UART),flags=MAP_SHARED,prot=PROT_READ | PROT_WRITE, offset=0) global uart uart=lms2012.UART.from_buffer(uartmm) isInitialized=True
def test_read_byte(self): from mmap import mmap f = open(self.tmpname + "d", "w+") f.write("c") f.flush() m = mmap(f.fileno(), 1) assert m.read_byte() == "c" raises(ValueError, m.read_byte) m.close() f.close()
def test_memoryview(self): from mmap import mmap f = open(self.tmpname + "y", "w+") f.write("foobar") f.flush() m = mmap(f.fileno(), 6) m[5] = '?' exc = raises(TypeError, memoryview, m) assert 'buffer interface' in str(exc.value) m.close() f.close()
def test_len(self): from mmap import mmap f = open(self.tmpname + "q", "w+") f.write("foobar") f.flush() m = mmap(f.fileno(), 6) assert len(m) == 6 m.close() f.close()
def test_slicing(self): from mmap import mmap f = open(self.tmpname + "v", "w+") f.write("foobar") f.flush() f.seek(0) m = mmap(f.fileno(), 6) assert m[-3:7] == "bar" f.close()
def test_offset(self): from mmap import mmap, ALLOCATIONGRANULARITY f = open(self.tmpname + "y", "w+") f.write("foobar" * ALLOCATIONGRANULARITY) f.flush() size = ALLOCATIONGRANULARITY offset = 2 * ALLOCATIONGRANULARITY m = mmap(f.fileno(), size, offset=offset) assert m[:] == ("foobar" * ALLOCATIONGRANULARITY)[offset:offset+size] assert len(m) == size m.close() f.close()
def test_offset(self): from mmap import mmap, ALLOCATIONGRANULARITY f = open(self.tmpname + "y", "w+") f.write("foobar" * ALLOCATIONGRANULARITY) f.flush() size = ALLOCATIONGRANULARITY offset = 2 * ALLOCATIONGRANULARITY m = mmap(f.fileno(), size, offset=offset) assert m[:] == ("foobar" * ALLOCATIONGRANULARITY)[offset:offset + size] assert len(m) == size m.close() f.close()
def init(): global isInitialized if not isInitialized: global lcdfile lcdfile=os.open(lms2012.LCD_DEVICE_NAME,os.O_RDWR) global lcdmm lcdmm=mmap(fileno=lcdfile, length=MEM_WIDTH * SCREEN_HEIGHT,flags=MAP_SHARED,prot=PROT_READ | PROT_WRITE, offset=0) global membuffer membuffer=array.array('c',['\0']*(MEM_WIDTH * SCREEN_HEIGHT)) global imgbuffer imgbuffer=array.array('B',[0]*((SCREEN_WIDTH+7)/8 * SCREEN_HEIGHT)) isInitialized=True
def pci_get_value(self, resource, offset): status = True result = "" try: fd = os.open(resource, os.O_RDWR) mm = mmap(fd, 0) mm.seek(int(offset)) read_data_stream = mm.read(4) result = struct.unpack('I', read_data_stream) except: status = False return status, result
def test_flush(self): from mmap import mmap f = open(self.tmpname + "n", "w+") f.write("foobar") f.flush() m = mmap(f.fileno(), 6) raises(TypeError, m.flush, 1, 2, 3) raises(TypeError, m.flush, 1, "a") raises(ValueError, m.flush, 0, 99) m.flush() # return value is a bit meaningless, platform-dependent m.close() f.close()
def test_read(self): from mmap import mmap f = open(self.tmpname + "f", "w+") f.write("foobar") f.flush() m = mmap(f.fileno(), 6) raises(TypeError, m.read, "foo") assert m.read(1) == "f" assert m.read(6) == "oobar" assert m.read(1) == "" m.close() f.close()
def init(): global isInitialized if not isInitialized: global pwmfile pwmfile=os.open(lms2012.PWM_DEVICE_NAME,os.O_RDWR) global motorfile motorfile=os.open(lms2012.MOTOR_DEVICE_NAME,os.O_RDWR) MOTORDATAArrray=lms2012.MOTORDATA * 4 global motormm motormm=mmap(fileno=motorfile, length=sizeof(MOTORDATAArrray),flags=MAP_SHARED,prot=PROT_READ | PROT_WRITE, offset=0) global motodata motodata=MOTORDATAArrray.from_buffer(motormm) isInitialized=True
def __init__(self, path_or_fd, ofs, size, flags=0): """Initialize a region, allocate the memory map :param path_or_fd: path to the file to map, or the opened file descriptor :param ofs: **aligned** offset into the file to be mapped :param size: if size is larger then the file on disk, the whole file will be allocated the the size automatically adjusted :param flags: additional flags to be given when opening the file. :raise Exception: if no memory can be allocated""" self._b = ofs self._size = 0 self._uc = 0 if isinstance(path_or_fd, int): fd = path_or_fd else: fd = os.open(path_or_fd, os.O_RDONLY | getattr(os, 'O_BINARY', 0) | flags) # END handle fd try: kwargs = dict(access=ACCESS_READ, offset=ofs) corrected_size = size sizeofs = ofs if self._need_compat_layer: del(kwargs['offset']) corrected_size += ofs sizeofs = 0 # END handle python not supporting offset ! Arg # have to correct size, otherwise (instead of the c version) it will # bark that the size is too large ... many extra file accesses because # if this ... argh ! actual_size = min(os.fstat(fd).st_size - sizeofs, corrected_size) if self._test_read_into_memory: self._mf = self._read_into_memory(fd, ofs, actual_size) else: self._mf = mmap(fd, actual_size, **kwargs) # END handle memory mode self._size = len(self._mf) if self._need_compat_layer: self._mfb = buffer(self._mf, ofs, self._size) # END handle buffer wrapping finally: if isinstance(path_or_fd, string_types()): os.close(fd) # END only close it if we opened it # END close file handle # We assume the first one to use us keeps us around self.increment_client_count()
def run(self): data = ETSData() map_file = mmap(-1, 16 * 1024, "Local\SimTelemetryETS2", ACCESS_READ) while 1: try: map_file.seek(0) data.load_from_mmap(map_file) json_data = dumps(data, cls=TruckEncoder) self.connection.send(struct.pack("!i", len(json_data))) self.connection.send(json_data) except Exception as e: print("Failed to send. Client probably disconnected.[{0}]".format(e.message)) break sleep(0.02)
def test_memoryview(self): from mmap import mmap f = open(self.tmpname + "y", "bw+") f.write(b"foobar") f.flush() m = mmap(f.fileno(), 6) b = memoryview(m) assert len(b) == 6 assert b.readonly is False assert b[3] == b"b" assert b[:] == b"foobar" del b # For CPython: "exported pointers exist" m.close() f.close()